Review Project

Let’s explore the project code and write some app code.

ApplicationJob

The starter project comes with an ApplicationJob. It looks like this:

app/jobs/application_job.rb

class ApplicationJob < Jets::Job::Base
  # Adjust to increase the default timeout for all Job classes
  class_timeout 60
end

Generally, your jobs will inherit from this ApplicationJob.

Create a Job

Let’s create some code that does some work. We’ll use a generator again.

❯ jets generate job hard
    create  app/jobs/hard_job.rb

The generate code looks like this:

app/jobs/hard_job.rb

class HardJob < ApplicationJob
  rate "10 hours"
  def perform
    puts "Do something with event #{event}"
  end
end

Note: The method name does not have to be perform, it can be anything. For Jobs, Jets will create a distinct Lambda function for each public method.

The rate macro-like method tells Jets to create an Amazon EventBridge rule (formerly called CloudWatch Event Rules and Scheduled Expressions) that runs the perform method below it on a schedule. Basically, the HardJob#perform Lambda function will run every 10 hours.

You can use cron or rate expressions. Examples:

cron "0 */12 * * ? *" # every 12 hours
rate "10 hours" # every 10 hours

Other Event Types

Tip: You can use the --type option to generate Jobs with supported event types. Example:

jets generate job hard --type sns

Here are some other supported event types:

For more info: jets generate job -h. Also, see the Events Docs for more info.

Next, we’ll test the job locally.