【问题标题】:Resque: how can i use specific worker for only one specific queueResque:我如何仅将特定工作人员用于一个特定队列
【发布时间】:2019-10-04 13:10:58
【问题描述】:

在我的 rails 5 应用程序中,我使用 resque 和 resque-scheduler 向我的客户发送消息。为此,我为消息创建了不同的不同队列,并创建了 3 个工作人员用于使用队列发送消息。 所以,我的问题是我怎样才能为一个特定的队列使用一个特定的工作人员。 即我有四个队列,例如生日检查器,提醒消息,约会检查器,确认消息。对于约会检查器,我设置了 cron 它将每 56 秒运行一次。为此,我的所有 3 名工作人员都忙于运行约会检查器队列。和其他正在等待的队列作业。但是我可以在这里为这个约会检查器队列保留一名工作人员。那么有可能吗?

我尝试在堆栈上找到一些相关的问题,但我找不到具体的解决方案。

这是我的 resque.god 文件代码

rails_env   = ENV['RAILS_ENV']
rails_root  = File.dirname(__FILE__) + '/..'
num_workers = rails_env == 'production' ? 3 : 2

num_workers.times do |num|
 God.watch do |w|
   w.dir      = "#{rails_root}"
   w.name     = "resque-#{num}"
   w.group    = 'resque'
   w.interval = 30.seconds
   w.env      = {"QUEUE"=>"*", "RAILS_ENV"=>rails_env}
   w.start    = "bundle exec rake -f #{rails_root}/Rakefile environment resque:work"
   w.log      = "#{rails_root}/log/resque.log"
   w.err_log  = "#{rails_root}/log/resque_error.log"

#    w.uid = 'git'
#    w.gid = 'git'

   # restart if memory gets too high
   w.transition(:up, :restart) do |on|
     on.condition(:memory_usage) do |c|
       c.above = 350.megabytes
       c.times = 2
     end
   end

   # determine the state on startup
   w.transition(:init, { true => :up, false => :start }) do |on|
     on.condition(:process_running) do |c|
       c.running = true
     end
   end

   # determine when process has finished starting
   w.transition([:start, :restart], :up) do |on|
     on.condition(:process_running) do |c|
       c.running = true
       c.interval = 5.seconds
     end

     # failsafe
     on.condition(:tries) do |c|
       c.times = 5
       c.transition = :start
       c.interval = 5.seconds
     end
   end

   # start if process is not running
   w.transition(:up, :start) do |on|
     on.condition(:process_running) do |c|
       c.running = false
     end
   end
 end
end

而这个是lib/tasks/resque_scheduler.rake文件代码

namespace :resque do
  task :setup => :environment do
    require 'resque'
    ENV['QUEUE'] = '*'
  end
  task :setup_schedule => :setup do
    require 'resque-scheduler'
    Resque.schedule = YAML.load_file('config/resque_schedule.yml')
  end
  task :scheduler => :setup_schedule
end

我已尽力在 stackoverflow 上提出我的第一个问题,所以请忽略小错误,并为上述问题提供一些解决方案。 谢谢!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 resque god resque-scheduler


    【解决方案1】:

    ENV['QUEUE'] = '*' 是所有队列的通配符。每个队列都将具有您正在排队的类的名称。只需将 * 替换为您要运行的队列的名称即可。

    【讨论】:

    • 感谢 Marlin,我知道,但在这里我将 * 替换为我的队列名称,然后只运行该特定队列。那么我的其他队列将如何调用?
    猜你喜欢
    • 1970-01-01
    • 2023-03-16
    • 1970-01-01
    • 2012-05-11
    • 2021-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-16
    相关资源
    最近更新 更多