Search code examples
rubyresque

Ruby app and Resque: can't start workers


I have this small ruby application, not Ruby on Rails - pure Ruby.

I've followed the instructions and I can queue stuff and see that everything is correctly queued using resque-web.

However, I have a problem to start a worker. The documentation indicates to run bin/resque work to launch a worker.

Doing so triggers the message -bash: bin/resque: No such file or directory

Everywhere on the Internet, people have the same problem, but for Rails app, not pure Ruby. The solution seems to include something in the rakefile, which I don't have.

How can I launch my worker? Thanks a lot!


Solution

  • The key to solving your problem is rake.

    Resque includes three rake tasks. All you need is a rakefile that requires 'resque/tasks' to use them. When you ask Rake for its list of commands you'll see the three tasks that are included with Resque:

    rake resque:failures:sort  # Sort the 'failed' queue for the redis_multi_queue failure backend
    rake resque:work           # Start a Resque worker
    rake resque:workers        # Start multiple Resque workers
    

    The one you're looking for (to start one worker) is resque:work. You tell it which queue to listen to using the QUEUE environment variable. So starting your worker would be something like:

    QUEUE=your_queue_name rake resque:work.

    Alternatively, you can listen to all queues using QUEUES=*.

    EDIT:

    Here's a more complete example. Create a file called rakefile:

    require 'resque/tasks'
    require 'resque'
    
    class Worker
      @queue = :default
    
      def self.perform(my_arg)
        puts my_arg
      end
    end
    
    task :hello do
        Resque.enqueue(Worker, 'hello')
    end
    

    Then in one terminal type TERM_CHILD=1 QUEUE=default rake resque:work. This will start the worker, watching the queue called default. It will print out any argument a job passes to its perform class method.

    In a second terminal type rake hello. This will enqueue a job for the Worker class, passing the string hello as an argument (which will be passed to the perform method in the Worker class). It knows to push to the default queue by looking at the @queue property on Worker.

    You'll see hello printed in the terminal where you started the worker.

    This example doesn't do anything useful, and you wouldn't put all that in your rakefile, but I think it's a good starting point for you to start modifying it and build your own.