computer_smile
computer_smile

Reputation: 2227

Rake task not running from within worker

I don't understand why my rake task is not running from within a resque worker. Running

rake :send_this_email

from the console works fine, I just want to run it as a cron job (as follows) but something is not working proplerly while invoking the rake task from within the worker.

My rescue_schedule.yml

send_this_email:
  cron: "*/2 * * * *"
  class: SendThisEmailWorker 
  args:
  description: "Send email when condition defined in rake task is met"

My send_this_email_worker.rb in workers directory, where the problem must be if I can manually call the rake task myself from the console?

require 'rake'

module SendThisEmailWorker
  @queue = :send_this_email

  def self.perform
    Rake::Task["send_this_email"].invoke
  end
end

When I start my dev server this send_this_email rake task should run every 2 minutes correct? It's not and the resque admin panel shows it as a job in the queue. What am I missing here?

Thanks for your attention.

UPDATED from gerep comment

require 'rake'

module SendThisEmailWorker
  @queue = :send_this_email

  def self.perform
    puts "Hi from the console, I'm started"
    Rake::Task["send_this_email"].invoke
  end
end

Upvotes: 2

Views: 3147

Answers (1)

sovanlandy
sovanlandy

Reputation: 1700

Only require 'rake' is not enough. For example if you do Rake::Task.tasks #list down all task

You will get []

You need to tell your worker class to load tasks. Try this

require 'rake'
Rake::Task.clear # necessary to avoid tasks being loaded several times in dev mode
YOUR_APP_NAME::Application.load_tasks
module SendThisEmailWorker
  @queue = :send_this_email

  def self.perform
    puts "Hi from the console, I'm started"
    Rake::Task["send_this_email"].invoke
  end
end

YOUR_APP_NAME is the name of your app and can be found at config/application.rb

Upvotes: 9

Related Questions