2

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
computer_smile
  • 2,117
  • 2
  • 24
  • 42
  • You have a worker started right? Just to test, put a "puts 'anything'" in your cronjob just to make sure it is called every 2 minutes, it will be printed in your console –  Mar 21 '13 at 19:56
  • I added the above and nothing has been printed to the console, my rake task also outputs to the console. Additionally, in the resque admin/interface I have the option to immediately start the job and when I do this the rake task is not invoked and no email received. – computer_smile Mar 21 '13 at 20:06
  • 1
    I believe I found the solution to my question here, I restarted my server with the following `QUEUE=* rake environment resque:work rails s` https://github.com/defunkt/resque/wiki/FAQ#how-do-i-ensure-my-rails-classesenvironment-is-loaded – computer_smile Mar 21 '13 at 20:14
  • Unfortunately, now when I try starting the rails server in dev mode this way I get the following. `Don't know how to build task 'rails'` last line when running with trace: `Execute resque:work` Why? – computer_smile Mar 25 '13 at 23:44

1 Answers1

9

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

sovanlandy
  • 1,700
  • 2
  • 19
  • 24