Task scheduler

Invoking services at predefined intervals

No programming is needed to receive messages from the built-in scheduler. Create a new job definition in Dashboard and a given service will be invoked each time it's scheduled to be run. Any extra data provided in a job's definition will be available to the service in self.request.raw_request.

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

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):
        self.logger.info('Message received: %s', self.request.raw_request)

Scheduling one-time jobs in Python

Use the convenience API below to schedule one-time jobs, e.g. in this example, we schedule another service to be invoked at a specific time.

The 'prefix' parameter makes it easy to find the job in Dashboard and logs.

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

# Zato
from zato.server.service import Service

class MyTarget(Service):

    def handle(self):
        self.logger.warn('I was invoked with %s', self.request.input)

class Scheduler(Service):

    def handle(self):

        # This can be used to make it easier to understand which scheduled job is which
        prefix = 'user@example.com'

        # This is what the target service is going to receive on input
        data = {
            'my.key': 'my.value'
        }

        # This is when the job should start, note that the time must be in UTC
        start_date = '2027-07-29 11:35:49'

        # This is the call that schedules the job
        self.schedule.onetime(self, MyTarget, prefix=prefix, start_date=start_date, data=data)

Scheduling other jobs in Python

Invoke the built-in zato.scheduler.job.create service to schedule interval-based or cron-style jobs.

# stdlib
from datetime import datetime

# Zato
from zato.server.service import Service

class MyService(Service):

    def handle(self):

        #
        # We schedule a cron-style job here that will run once per hour (@hourly)
        #

        request = {
            'cluster_id': self.server.cluster_id,
            'name': 'my-job-1',
            'is_active': True,
            'service': 'zato.ping',
            'job_type': 'cron_style',
            'start_date': datetime.utcnow().isoformat(), # Always in UTC
            'cron_definition': '@hourly'
        }

        self.invoke('zato.scheduler.job.create', request)