ruby-on-railsresque

Can someone explain the term QUEUE=* rake resque:work


On Ruby on Rails I need to start a 'rescue' task. To let this work the command QUEUE=* rake resque:work must be executed in a new terminal (macOS).

But: Can someone explain what this actually means?


Solution

  • resque worker will keep on polls redis queues for pending jobs. So, we need to pass queue names as args to resque task while starting the task. QUEUE=* is doing the same here.

    So, here your question can be breakdown to 2 parts,

    1. what is the purpose of QUEUE
    2. What is the purpose of *

    QUEUE:

    In ruby we can do this in 4 ways.

    1. Rake args:

      rake resque:work[*]

      For this to work resque task should have the following convention

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

      rake resque:work *

      For this to work resque task should be like

       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. Options:

      rake resque:work --queues=*

      Again task should look like,

       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. ENV variables:

      QUEUE=* rake resque:work

      For this task should be like,

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

    Our resque library using the 4th approach for this. So you are actually setting the env variable here with the QUEUE=*.

    The same we can break into 2 lines using the following code.

    export QUEUE=*
    rake resque:work
    

    Now coming to * part:

    * is a wildcard char that tells resque to listen to all the queues in the redis. But priority will be alphabetical order. If you dont want this we can call task with specific queues also like

    QUEUES="queue1,queue2" rake resque:work

    So, here resque task will pull jobs from only queue1 and queue2, with queue1 having high priority.

    rake resque:work:

    rake: is a command to initiate task

    resque: is namespace to group tasks under a name

    work: is a task name