Clockwork and Resque

706 Views Asked by At

I am trying to run a simple task to test out using resque and clockwork together.

My worker: app/workers/logger_helper.rb

class LoggerHelper
  @queue = :log_queue

  def self.before_perform
    Rails.logger = Logger.new(File.open(Rails.root.join('log', 'resque.log')))
    Rails.logger.level = Logger::DEBUG
  end

  def self.perform
    time = Time.now
    Rails.logger.info("testing #{time}")
  end
end    

My clock.rb file lib/clock.rb

  require File.expand_path('../../config/boot',        __FILE__)
  require File.expand_path('../../config/environment', __FILE__)
  require 'clockwork'

  module Clockwork

  handler do |job|
    Resque.enqueue(job)
  end

  every(10.seconds, 'loggerhelper') {LoggerHelper}
end

Rake file:

require File.expand_path('../config/application', __FILE__)
Rails.application.load_tasks
require 'resque/tasks'
task(:default).clear
task default: [:spec]

task "resque:setup" => :environment do
  QUEUE = '*'
end

First I run resque:setup Second I run clockwork lib/clockwork.rb

I get the following output in the terminal

INFO -- : Triggering 'loggerhelper'
INFO -- : Triggering 'loggerhelper'
...

But nothing writes to the log.

I've tried a combination of things but I don't see any output.

I did run

every(10.seconds, 'loggerhelper') {LoggerHelper.perform}

in the clock.rb file and it does work, but I didn't think that you were supposed to call deliver directly. Also I'm not sure if it's actually running off of the queue or just simply executing.

1

There are 1 best solutions below

0
On

First the logger should be configured to:

Rails.logger = Logger.new(Rails.root.join('log', 'rescue.log'))

I also simplified my clock.rb file although I think it works as it was originally:

require File.expand_path('../../config/boot',        __FILE__)
require File.expand_path('../../config/environment', __FILE__)
require 'clockwork'
module Clockwork
  every(5.seconds, 'Running Logger Helper') {Resque.enqueue(LoggerHelper)}

end

Next I created a Proc file with the following:

web: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
resque-web: bundle exec resque-web --foreground
resque: env TERM_CHILD=1 bundle exec rake resque:work QUEUE='*'
clock: bundle exec clockwork lib/clock.rb

If I run

 env TERM_CHILD=1 bundle exec rake resque:work QUEUE='*'

in one terminal and then

bundle exec clockwork lib/clock.rb

in a second terminal it works! I hope this helps out.