Skip to main content

Scheduling

Scheduling allows to create and configure to run jobs at specific times. It can be simple one e.g. running a job every few seconds or more complex e.g. every minute, between 8am and 5pm, every day.

How to create a scheduled service​

In order to create a scheduled job you need to create a ScheduledJob object and use it to decorate a function with the logic of your scheduled task using @job.schedule.

from arconq_sdk import Dependency, Logger, ScheduledJob
from containers.container import Container
from contracts.example_message import ExampleMessage

job = ScheduledJob()

@job.schedule("/example-job", cron_schedule="0/10 * * ? * * *")
async def example_scheduled_job(
logger: Logger = Dependency(Container.logger))
):
logger.info("Scheduled job was executed")

The next step is to register the scheduled job.

@arconq_app
def main(
container: Container,
app_builder: AppBuilder = Dependency(Container.app_builder),
) -> None:
app_builder.configure_arconq(container)

app_builder.add_scheduled_job(container, example_scheduled_job, example_scheduled_job.job)

app = app_builder.build_app()
app.run()

This setup will execute MyScheduledJob every 10 seconds

Defining frequency of a scheduled job using CRON​

Cron-Expressions are used to configure instances of Scheduled Jobs. Cron-Expressions are strings that are actually made up of seven sub-expressions, that describe individual details of the schedule. These sub-expression are separated with white-space, and represent:

  1. Seconds
  2. Minutes
  3. Hours
  4. Day-of-Month
  5. Month
  6. Day-of-Week
  7. Year (optional field)

Examples of using CRON schedule​

Build a Scheduled Job that will fire every 7 seconds:

@job.schedule("/example-job", cron_schedule="0/7 * * ? * * *");

Build a Scheduled Job that will fire every other minute, between 8am and 5pm, every day:

@job.schedule("/example-job", cron_schedule="0 0/2 8-17 * * ?");

Build a Scheduled Job that will fire daily at 9:30 am:

@job.schedule("/example-job", cron_schedule="0 30 9 * * ?");