Skip to content

Backend Pattern Job

Ferris Tseng edited this page Feb 8, 2021 · 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:

  • 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
  • For jobs triggered on an interval:
  • For jobs triggered on-demand:
    • The class that is triggering your job should inherit from RunAsyncable
    • Use the perform_later_or_now. 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