Search code examples
ruby-on-railsrakedelayed-job

Where to put the loop? On the rake task or on delayed job?


I'm wondering what is most efficient to send a bunch of emails.

  • should I put the loop on the rake tasks with delayed job only doing the sending?

    task :publish => :environment do
      # insert loop here do
        # insert delayed job here
      end
    end
    
  • should I put the loop inside the delayed job?

    task :publish => :environment do
      # insert delayed job here
    end
    
    # and on the job:
    def perform
      # insert loop here
    end
    

Solution

  • It depends on how many background workers you have. If you have more than one worker, then the first option (creating each job separately, with the loop inside the rake task) is far better, as it allows those tasks to be run in parallel.

    It also makes it easier to write your worker method, as you don't need to worry about rerunning the worker over the entire list if it happens to fall over or be terminated. (although it's still good practice to ensure that your workers are idempotent where practical!)