Search code examples
ruby-on-railsrubybackground-processdelayed-job

Rails delayed job multithreading


Is there any way to run same method parallel in background using delayed job gem? For example i have one method assign_tasks which creates 4K to 6K records in database, when called multiple times same method for different objects is there any way to accomplish this execution faster or execute all calls parallelly? It takes hours to complete 3 to 4 method calls to complete.

def assign_tasks
  #some conditions to find matched users here
  #if matched create new record for a user
  users.each do |u|
    task = Task.new(user_id: u.id)
    task.save
  end
end

@project.delay.assign_tasks

Solution

  • Yes, with DelayedJob you could add a method to your User model like this

    def assign_task
      Task.create(user_id: id)
    end
    

    and process it delayed like this in your original method:

    def assign_tasks
      users.find_each { |user| user.delay.assign_task }
    end
    

    Then the task generation for each user will happen in the background and – when there are enough workers – in parallel.

    Btw there are other tools that support processing jobs in the background – the most common nowerdays is Sidekiq. They all have a slightly different syntax and different dependencies. Depending on your Ruby on Rails version and your requirements you might even want to use ActiveJob which ships with Rails per default.