Skip to content

Instantly share code, notes, and snippets.

@rasmar
Last active January 1, 2018 19:55
Show Gist options
  • Select an option

  • Save rasmar/56c49d58df0950947366eddaeca9ade7 to your computer and use it in GitHub Desktop.

Select an option

Save rasmar/56c49d58df0950947366eddaeca9ade7 to your computer and use it in GitHub Desktop.
Rails Jobs

Important notes

  1. Use ActiveJob
  2. Use different adapters: Sidekiq, Resque
  3. Backend adapters depend on workers to execute jobs
  4. Use queues for different kind of jobs / tasks: mailing, maintenance, etc.
  5. Job perform callbacks are available as well
  6. ActionMailer can work as an ActiveJob
  7. Watch out for Thread safety - pay attention to instance variables and how they are accessed and changed in concurrent processes. One option is to use Mutex
  8. Sidekiq > Resque > DelayedJob (But it depends on the needs and available resources)

Links to read

ActiveJob Basics Testing ActiveJob

Creating ActiveJob in Rails

rails generate job job_name --queue job_queue

Queue job

TestJob.perform_later(args)
TestJob.set(wait_until: Date.tomorrow.noon).perform_later(args)
TestJob.set(wait: 1.week).perform_later(args)
TestJob.perform_later(arg1, arg2, filter: 'some_filter')

ActionMailer

ActionMailer can work as ActiveJob. To send mails as a job (user won't have to wait):

UserMailer.welcome(@user).deliver_now
 vs
UserMailer.welcome(@user).deliver_later
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment