Running Rails 4.2.0 so I am using the ActiveJob w/ Sidekiq backend. I need to invoke regularly scheduled background jobs so I am hoping to use Clockwork, but I haven't seen any examples of how to use it with ActiveJob.

Here is my lib/clock.rb based off the Clockwork Github example:

require 'activejob'
module Clockwork
  handler do |job, time|
    puts "Running #{job}, at #{time}"
    ProductUpdateJob.perform_later
  end

  every(30.seconds, 'ProductUpdateJob.perform_later')

end

Update:

I was able to get it to work for my situation, but I'm not entirely satisfied with the solution since I am having to load the entire environment. I would only like to require the bare minimum to run the ActiveJobs.

Dir["./jobs/*.rb"].each {|file| require file }

require 'clockwork'
require './config/boot'
require './config/environment'
#require 'active_job'
#require 'active_support'

module Clockwork
  handler do |job, time|
    puts "Running #{job}, at #{time}"
    #send("#{job}")
    #puts "#{job}".constantize
    "#{job}".constantize.perform_later
  end


  every(10.seconds, 'ProductUpdateJob') 

end
share|improve this question

Here is a working version

require 'clockwork'
require './config/boot'
require './config/environment'

module Clockwork
  handler do |job, time|
    puts "Running #{job}, at #{time}"
    "#{job}".constantize.perform_later
  end

  every(10.seconds, 'ProductUpdateJob') 

end
share|improve this answer

It is possible, but it will have no context of your redis/job backend configuration. You could always require a file in config/initializers that configures sidekiq and redis. This is the minimum you will need:

require 'clockwork'
require 'active_job'

ActiveJob::Base.queue_adapter = :sidekiq

Dir.glob(File.join(File.expand_path('.'), 'app', 'jobs', '**')).each { |f| require f }

module Clockwork
  every(1.minute, 'Do something') { SomeJob.perform_later }
end
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.