Jets Job Scheduler

Jets supports a Job Scheduler with scheduler.yml that is similar to sidekiq-scheduler.

Enabling

The Jets scheduler is enabled by default.

config/jets/deploy.rb

Jets.deploy.configure do
  config.scheduler.enable = true # default true
  # config.scheduler.translate.on_deploy = true # default true
end
  • You create a config/jets/scheduler.yml with schedule items. Jets will create CloudWatch rules resources that correspond to the scheduled items.
  • Also, if the project has a config/sidekiq.yml with a schedule key, Jets automatically translates that to a config/jets/scheduler.yml upon deployment. See: Scheduler Translate
  • The config/jets/scheduler.yml will take high precedence if both it and config/sidekiq.yml exists.

Example

Here’s an example

config/jets/scheduler.yml

GreetingJob:
  rate: "1 minute"

cleanup_job_desk:
  rate: "12 hours"
  class: CleanupJob
  args: ["desk", "room"]
  splat_args: true

When the config/jets/scheduler.yml exists, Jets uses it to create the scheduler components upon jets deploy, IE: Scheduled Events and Lambda functions.

How It Works

The schedule.yml will create jets/schedule_event.rb code upon deployment to something like this:

app/events/jets/schedule_event.rb

class Jets::ScheduleEvent < Jets::Event::Base
  class_timeout 15.minutes.to_i
  rate "1 minute"
  def greeting_job
    GreetingJob.perform_later
  end
  rate "12 hours"
  def cleanup_job_desk
    CleanupJobDesk.perform_later("desk","room")
  end
end

Each method in the class creates a distinct Lambda function that handles the processing. The rate expressions create AWS Scheduled Events. AWS manages the scheduler for you. It’s “serverless”. The resources are deployed as part of the *JetsScheduleEvent* nested stack.

Expressions Support

Both the rate and cron expressions are supported.

You can use cron-like expressions

cron(0 12 * * ? *)            # runs every day at 12:00pm UTC
cron(5,35 14 * * ? *)         # runs every day, at 2:05pm and 2:35pm UTC
cron(15 10 ? * 6L 2019-2022)  # runs at 10:15am UTC on the last Friday of
                              # each month during the years 2019 to 2022

** Note **: The [AWS Cron] (https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html) syntax is slightly different from the Linux cron syntax. The AWS Cron format has six required fields, which is slightly different from the traditional Linux cron format, which has five fields. There’s also a ? notation, which means any day of the month.

You can also use rate expresions

rate(1 minute)
rate(5 minutes)
rate(1 hour)
rate(1 day)

Note: Notice the singular 1 minute vs plural 5 minutes.

Jets 6 and above also uses fugit internally to allow user-friendly expressions like 5m to 5 minutes.

Notes

  • The only required field is a scheduling expression field, IE: rate or cron.
  • The class name can be inferred by the top-level key or explicitly by the class field. Example: GreetJob (inferred) and class: CleanupJob (explicit).
  • The args field is optional. If you need the args to be splatted, you can use splat_args: true. This removes the [] brackets for Array arguments and {} brackets for Hash arguments.
  • An interesting note: The CloudWatch Schedule Event Rule triggers a Jets ScheduledEvent Lambda function that calls your Job’s perform_later method, which writes to the Jets Queue. Each Jets Queue’s Lambda function handles the processing.