Quantcast
Channel: Planet Python
Viewing all articles
Browse latest Browse all 22848

Zato Blog: File notifications in Zato 2.0

$
0
0

One of many great things about Zato is the fact how easy it is to plug into it new data sources and input methods triggering one's SOA/API services.

For instance, Zato 2.0 does not have a web-admin GUI for file notifications but it is still possible to listen for new or updated files in directories of choice and invoke services each time a new event arrives, e.g. when a new file is dropped into a directory, effectively creating a new channel type in addition to what Zato comes with out of the box.

This is exactly what the script below does - it uses watchmedo, part of the watchdog package, to listen for events in a given directory. Each time anything of interest happens in that directory (here - in /tmp/data) a Zato service defined below is invoked with a path to that item of interest provided on input.

#!/bin/bash

watchmedo shell-command \
    --patterns="*"\
    --command='curl localhost:11223/file.notifications?path=${watch_src_path}; echo'\
    /tmp/data

It's entirely up to that service to decide how to handle the data in each file - one can decide to deliver it to any of outgoing connections in Zato using, for instance, AMQP, SMTP, FTP, ElasticSearch, Solr or do anything else that is required in a given integration scenario.

Screenshots

Just to exemplify the idea, in this blog post the data is simply stored in server.log:

# -*- coding: utf-8 -*-from__future__importabsolute_import,division,print_function,unicode_literals# Zatofromzato.server.serviceimportServiceclassFileNotifications(Service):name='file.notifications'classSimpleIO(object):input_required=('path',)defhandle(self):# Read data indata=open(self.request.input.path).read().strip()# Since this is just an example, only log the contents of what was read inself.logger.info('Data from %s is `%s`',self.request.input.path,data)

Such a service needs to be mounted on an HTTP channel, as below.

Screenshots

If you have not done it yet, create a directory /tmp/data and start the watchmedo script above before continuing with the next steps.

Now we can create a new file and observe in server.log what happens, how the service reacts to it:

Screenshots

Sure enough, server.log confirms that everything works as expected:

Screenshots

Viewing all articles
Browse latest Browse all 22848

Trending Articles