【问题标题】:Can someone explain the term QUEUE=* rake resque:work有人可以解释这个术语 QUEUE=* rake resque:work
【发布时间】:2020-11-18 15:35:20
【问题描述】:

在 Ruby on Rails 上,我需要启动“救援”任务。 要让它工作,必须在新终端 (macOS) 中执行命令 QUEUE=* rake resque:work

但是:有人能解释一下这实际上意味着什么吗?

【问题讨论】:

  • 它将环境变量QUEUE 设置为*,然后运行rake rescue:work(可能会在某个时候查看QUEUE)。
  • 为了连接命令,我确实期望一个 ||或 && ?
  • 这是一个 bash 约定,用于设置只有命令持续时间的环境变量。在每个命令后保存设置和取消设置。

标签: ruby-on-rails resque


【解决方案1】:

resque worker 将继续轮询 redis 队列以获取待处理的作业。因此,我们需要在启动任务时将队列名称作为 args 传递给 resque 任务。 QUEUE=* 在这里做同样的事情。

所以,这里你的问题可以分为两部分,

  1. QUEUE的目的是什么
  2. *的目的是什么

QUEUE:

在 ruby​​ 中,我们可以通过 4 种方式做到这一点。

  1. 耙参数:

    rake resque:work[*]

    为此,resque 任务应具有以下约定

    task :work, [:queues] do |t, args|
     queues = args[:queues]
     #do some thing with queues
    end
    
  2. ARGV:

    rake resque:work *

    为此,resque 任务应该是这样的

     task :work do
       ARGV.each { |a| task a.to_sym do ; end } # to prevent multiple tasks
       queues = ARGV[0]
       # do some thing with the queues
    end
    
  3. 选项:

    rake resque:work --queues=*

    再次任务应该是这样的,

     task :work do
      options = {}
      OptionParser.new do |opts|
        opts.banner = "Usage: rake add [options]"
        opts.on("-q", "--queues ARG", String) { |queues| options[:queues] = queues }
      end.parse!
      # do some thing with options[:queues]
     end
    
  4. 环境变量:

    QUEUE=* rake resque:work

    对于这个任务应该是这样的,

     task :work do
        #use  ENV['queues']
     end
    

我们的 resque 库为此使用了第 4 种方法。因此,您实际上是在此处使用 QUEUE=* 设置 env 变量。

同样,我们可以使用以下代码分成 2 行。

export QUEUE=*
rake resque:work

现在来到 * 部分:

* 是一个通配符,它​​告诉 resque 监听 redis 中的所有队列。但优先级将按字母顺序排列。如果您不想要这个,我们也可以调用具有特定队列的任务,例如

QUEUES="queue1,queue2" rake resque:work

因此,这里 resque 任务将仅从 queue1 和 queue2 中提取作业,其中 queue1 具有高优先级。

rake resque:work

rake:是启动任务的命令

resque: 是将任务分组到一个名称下的命名空间

work: 是一个任务名

【讨论】:

    猜你喜欢
    • 2011-03-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多