Generate Code

Let’s generate some code and review it.

❯ jets generate:event cool --trigger scheduled
      create  app/events/application_event.rb
      create  app/events/cool_event.rb

The generate code looks like this:

app/events/cool_event.rb

class CoolEvent < ApplicationEvent
  rate "10 hours"
  def handle
    puts "Do something with event #{JSON.dump(event)}"
  end
end

Note: The method name does not have to be handle, 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 handle method below it on a schedule. Basically, the CoolEvent#handle 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 Triggers

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

jets generate:event cool --trigger sns

Here are some other supported event triggers:

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

Next, we’ll test the job locally.