parm530
3/29/2018 - 7:42 PM

Resque

Setting up and Using Resque

What is Resque?

  • Handles background jobs by placing them on queues and handling them at a later time
  • Useful for code that is being executed on an external web service that can be moved into a background task
    • Why?
      • The service could take up time and cause delay in your app's loading time
      • You want to move them into a background process
      • Also prevents the blocking of other requests from one that is currently being held

Setting up Resque

  • install or have installed redis!!!
  • start the redis process!
  • In Gemfile: add gem 'resque', then run bundle install
  • Add a file within lib/tasks, called resque.rake
require 'resque/tasks'

#load the environment when the worker starts up
task "resque:setup" => :environment
  • run this in the terminal after all changes have been made to the project: bundle exec rake resque:work QUEUE='*'
  • The QUEUE='' takes in the name of the queue to work on or use * to load all queues

Steps

  • To add a job to a queue:

    • Use Resque.enqueue(workerClassName, additional_arguments) in your code
    • Additional arguments could be @object.id (can be use to refetch the object without submitting a complex object that is going to be turned into JSON).
  • Create a new directory within apps called workers, for ex: /lib/workers

  • Create a file (worker) in the workers directory and name it using the worker_class_name.rb used in the enqueue line above.

    • That worker file is basically a ruby class!
    class WorkerClassName
      # 1st requirement
      @queue = :name_of_queue       #can be named anything
      
      # 2nd requirement
      def self.perform(object.id)      #takes the additional arguments passed in to enqueue
        object = Object.find(abject.id) #refetch the id that was before to complex to send 
                                        #through
        ... #code referencing your API call!
      end
      
    end
    
  • Complete!

  • If your worker needs to reference a rake task: Rake::Task['file:name_of_task'].invoke


Resque Web Interface

  • from terminal, execute resque-web
  • You can check the queues and failed jobs!
  • May need to restart rake task if new code was added after running the task initially

Imbedding the Web Interface into your App?

  • You can add a route to also visit the resque web interface instead of launching it from terminal!
  • In config/routes: mount Resque::Server, :at => "/resque"
  • Need to edit the Gemfile again: gem 'resque', :require => "resque/server"

Add authentication to the Resque Web Interface

  • If using Devise:
#routes.rb
authenticate :admin do
  mount Resque::Server, :at => "/resque"
end
  • If not using Devise, an alternative way is to create a new initializer in config/initializers called resque_auth.rb
Resque::Server.use(Rack:;Auth::Basic) do |user, password|
  user == "rtrw"
  password == "jhtrw"
end
  • A resque job is any Ruby class or module with a perform class method.

  • Resque can maintain multiple queues for different job types: by setting the @queue instance var, this worker will only look for jobs on the :sleep queue!

  • Next, queue the job: Resque.enque(Sleeper, 5) (in your controller, for example)

    • The parameters will be serialized as JSON and appended to the Redis queue specified in the job class:
    {
      'class': 'Sleeper',
      'args': [5]
    }
    
  • Jobs should only need access to your models!

  • When a job is created, it gets appended to a list data structure in Redis.

    • A resque worker will then try to process the job.

Resque Web Backend

Overview

  • Contains list of all the queues and workers.
  • Each queue shows a count of pending jobs.
  • The list of workers displays what queue the worker is working on and the job currently being processed on.

Failed

  • Shows a list of errors of the failed jobs

Starting up the Workers

  • At the top of the your Rakefile, add the following: require 'resque/rake'
    • Run bundle exec rake -T resque
  • Environment Variables:
    • QUEUE: controls WHICH queue is monitored
    • COUNT: sets the number of workers (only resque:workers)
  • Start the task by running: bundle exec rake environment resque:work QUEUE=sleep
    • resque:work starts a rescue worker (more info run the rake command above)