omencat
omencat

Reputation: 1165

Clockwork to queue ActiveJob background jobs

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

Upvotes: 5

Views: 1457

Answers (2)

omencat
omencat

Reputation: 1165

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_async
  end

  every(10.seconds, 'ProductUpdateJob') 

end

Upvotes: 3

Pierre Pretorius
Pierre Pretorius

Reputation: 2909

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

Upvotes: 2

Related Questions