Skip to content

Backend Pattern Job

Matt Roth edited this page Oct 7, 2022 · 17 revisions

[Pattern] Job

Description

Jobs are responsible for doing asynchronous work in Caseflow.

Location

  • app/jobs

Also, jobs that are triggered on an interval should be declared in:

https://github.com/department-of-veterans-affairs/appeals-lambdas/blob/master/async-jobs-trigger/serverless.yml

Best Practices

Checklist:

Prior to Deployment

  • Inherit from ApplicationJob
  • Ensure application_attr and queue_with_priority are set. These are needed for the async job trigger to start your job, and are also used to tag Datadog metrics
  • Define a perform method
  • Ensure you set the RequestStore[:current_user] if parts of your job depend on that being set
  • Ensure that you are handling errors and retry if necessary

After Deployment

Choose one of the following options or both of the options below to enable the job in production:

  • For jobs triggered on an interval (Option 1):
    • Add job to serverless.yml for jobs triggered on an interval. Note: You have to add to the end of the list here otherwise the job won't be detected
    • Add job to SUPPORTED_JOBS
    • The new lambda will be deployed upon merging the new job to master in serverless.yml
  • For jobs triggered on-demand (Option 2):
    • The class that is triggering your job should inherit from RunAsyncable
    • Use the perform_later_or_now helper method. This method will launch async jobs inline in dev.

Tradeoffs

Jobs are good for things that take a long time to do or for when we need to interact with an unreliable external service (Asyncable models can help with this problem). There is some overhead to setting them up, so don't rely on them too much to do really simple tasks.

Resources

Examples in Caseflow

Additional Reading

Related Patterns

Clone this wiki locally