【问题标题】:Clockwork to queue ActiveJob background jobs将 ActiveJob 后台作业排队的发条
【发布时间】:2015-03-16 22:59:28
【问题描述】:

正在运行 Rails 4.2.0,所以我正在使用带有 Sidekiq 后端的 ActiveJob。我需要调用定期安排的后台作业,所以我希望使用 Clockwork,但我还没有看到任何关于如何将它与 ActiveJob 一起使用的示例。

这是我基于 Clockwork Github 示例的 lib/clock.rb:

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

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

end

更新:

我能够让它适用于我的情况,但我对解决方案并不完全满意,因为我必须加载整个环境。我只想要求最低限度的运行 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

【问题讨论】:

    标签: ruby-on-rails clockwork rails-activejob


    【解决方案1】:

    这是一个工作版本

    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
    

    【讨论】:

      【解决方案2】:

      这是可能的,但它不会包含您的 redis/job 后端配置的上下文。您可能总是需要在 config/initializers 中配置 sidekiq 和 redis 的文件。这是您需要的最低要求:

      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
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-25
        • 2023-04-03
        • 2018-04-08
        • 1970-01-01
        • 2016-06-05
        • 2017-03-14
        相关资源
        最近更新 更多