Home Python Service-oriented API job scheduling | Zato

Service-oriented API job scheduling | Zato

0
Service-oriented API job scheduling | Zato

[ad_1]

An integral a part of Zato, its scalable, service-oriented scheduler makes it’s potential to execute high-level API integration processes as background duties. The scheduler runs periodic jobs which in flip set off companies and companies are what’s used to combine methods.

Integration course of

On this article we are going to verify tips on how to use the scheduler with three sorts of jobs, one-time, interval-based and Cron-style ones.

What we need to obtain is a pattern but pretty frequent use-case:

  • Periodically seek the advice of a distant REST endpoint for brand new knowledge
  • Retailer knowledge present in Redis
  • Push knowledge discovered as an e-mail attachment

As an alternative of, or along with, Redis or e-mail, we might use SQL and SMS, or MongoDB and AMQP or anything – Redis and e-mail are simply instance applied sciences steadily utilized in knowledge synchronisation processes that we use to focus on the workings of the scheduler.

Regardless of the enter and output channels, the scheduler works at all times the identical – a definition of a job is created and the job’s underlying service is invoked in keeping with the schedule. It’s then as much as the service to carry out all of the actions required in a given integration course of.

Python code

Our integration service will learn as under:

# -*- coding: utf-8 -*-

# Zato
from zato.frequent.api import SMTPMessage
from zato.server.service import Service

class SyncData(Service):
    title = 'api.scheduler.sync'

    def deal with(self):

        # Which REST outgoing connection to make use of
        rest_out_name = 'My Information Supply'

        # Which SMTP connection to ship an electronic mail by way of
        smtp_out_name = 'My SMTP'

        # Who the recipient of the e-mail shall be
        smtp_to = 'hiya@instance.com'

        # Who to placed on CC
        smtp_cc = 'hiya.cc@instance.com'

        # Now, let's get the brand new knowledge from a distant endpoint ..

        # .. get a REST connection by title ..
        rest_conn = self.out.plain_http[rest_out_name].conn

        # .. obtain latest knowledge ..
        knowledge = rest_conn.get(self.cid).textual content

        # .. assemble a brand new e-mail message ..
        message = SMTPMessage()
        message.topic = 'New knowledge'
        message.physique = 'Verify connected knowledge'

        # .. add recipients ..
        message.to = smtp_to
        message.cc = smtp_cc

        # .. connect the brand new knowledge to the message ..
        message.connect('my.knowledge.txt', knowledge)

        # .. get an SMTP connection by title ..
        smtp_conn = self.electronic mail.smtp[smtp_out_name].conn

        # .. ship the e-mail message with latest knowledge ..
        smtp_conn.ship(message)

        # .. and now retailer the information in Redis.
        self.kvdb.conn.set('latest.knowledge', knowledge)

Now, we simply must make it run periodically in background.

Thoughts the timezone

Within the subsequent steps, we are going to use the Zato Dashboard to configure new jobs for the scheduler.

Maintain it thoughts that any date and time that you just enter in web-admin is at all times interepreted to be in your web-admin person’s timezone and this is applicable to the scheduler too – by default the timezone is UTC. You possibly can change it by clicking Settings and selecting the correct timezone to make it possible for the scheduled jobs run as anticipated.

It doesn’t matter what timezone your Zato servers are in – they might be in numerous ones than the person that’s configuring the roles.

Endpoint definitions

First, let’s use web-admin to outline the endpoints that the service makes use of. Notice that Redis doesn’t want an express declaration as a result of it’s at all times out there below “self.kvdb” in every service.

  • Configuring outgoing REST APIs

Now, we are able to transfer on to the precise scheduler jobs.

To cowl totally different integration wants, three varieties of jobs can be found:

  • One-time – fires as soon as solely at a selected date and time after which by no means runs once more
  • Interval-based – for periodic processes, can use any mixture of weeks, days, hours, minutes and seconds for the interval
  • Cron-style – much like interval-based however makes use of the syntax of Cron for its configuration

One-time

Choose one-time if the job shouldn’t be repeated after it runs as soon as.

Interval-based

Choose interval-based if the job must be repeated periodically. Notice that such a job will by default run indefinitely however it’s also possible to specify after what number of occasions it ought to cease, letting you to specific ideas comparable to “Execute as soon as per hour however for the subsequent seven days”.

Cron-style

Choose cron-style in case you are already conversant in the syntax of Cron or when you have some Cron duties that you just want to migrate to Zato.

Operating jobs manually

At occasions, it’s handy to run a job on demand, it doesn’t matter what its schedule is and no matter what kind a specific job is. Internet-admin allows you to at all times execute a job immediately. Merely discover the job within the itemizing, click on “Execute” and it’ll run instantly.

Further context

It is extremely usually helpful to offer further context knowledge to a service that the scheduler runs – to realize it, merely enter any arbitrary worth within the “Further” discipline when creating or an enhancing a job in web-admin.

Afterwards, that info shall be out there as self.request.raw_request within the service’s deal with methodology.

Reusability

There’s nothing else required – all is completed and the service will run in accordance with a job’s schedule.

But, earlier than concluding, observe that our integration service is totally reusable – there’s nothing scheduler-specific in it even if we at present run it from the scheduler.

We might now invoke the service from command line. Or we might mount it on a REST, AMQP, WebSocket or set off it from some other channel – precisely the identical Python code will run in precisely the identical trend, with none new programming effort wanted.

[ad_2]

LEAVE A REPLY

Please enter your comment!
Please enter your name here