user5084534
user5084534

Reputation:

Unable to start worker after rake task ruby on rails

I have read number of questions here but no one helped me as of yet.

Scenerio: I am starting rake task using button click and it also starts the rake task but the problem is that it does not activate worker.

Here is my code of button click:

def run_rake_jobs
   Resque.enqueue(ImportJobs)
   puts Resque.info, "**************************************************"
   flash[:notice] = "Importing Jobs Now"
   redirect_to :back
end

Task code:

module Refinery
  module Titans
    class ImportJobs
      @queue = :import_jobs
      def self.perform
        system "rake st:import_jobs"
      end
    end
  end
end

How I can start worker? I have tried all possible tricks but did not work for me.

Upvotes: 1

Views: 569

Answers (1)

Matouš Borák
Matouš Borák

Reputation: 15954

The code in your run_rake_jobs only enqueues the ImportJobs background job into a queue on redis but does not trigger its execution. The code in the controller continues without interruption.

To actually run the queued jobs you need to call a separate command (typically outside rails code), such as rake resque:work, see the resque readme for more info.

This pattern for running background jobs is typical: in your Rails code, you only enqueue the jobs that should be run in the background. Then, there is a separate process that fetches the background jobs from the queue and runs them using one or more workers. This way you can decouple long-runnning tasks from your main Rails code.

Upvotes: 0

Related Questions