Run services on a schedule

Run services on an interval, at a specific time or on a cron expression.

The scheduler invokes your services on an interval, at a specific time or on a cron expression. Jobs are created in the Dashboard or from Python and no service code changes are needed for a service to become a scheduled one.

For a step-by-step introduction, see the Python scheduler tutorial.

Invoke services on an interval

To create a job, go to Scheduler > Config in the Dashboard, click Create a new job and fill in the form:

  1. Name: Nightly Sync
  2. Service: the service the scheduler invokes on each run
  3. Every: how often to run it, e.g. every 30 minutes
  4. Click OK
New interval-based job

The service is invoked each time the job fires, and any extra data provided in the job's definition is 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)

Schedule one-time jobs in Python

To run a service once, at a specific time, call self.schedule.onetime. The example below schedules another service for a specific moment - the prefix parameter makes the job easy to find in the 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)

Schedule interval-based and cron-style jobs in Python

To create interval-based or cron-style jobs from code, invoke the built-in zato.scheduler.job.create service:

# 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 = {
            '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)

See also

FeatureWhat it does
IMAPPoll mailboxes on a schedule with an auto-created job
Config filesKeep a job's settings outside code
SMTPSend email from scheduled services

Learn more