Resque - ruby-library for creating background tasks, queuing such tasks and their subsequent execution. Tasks can be any ruby class or module containing the perform method. In the ruby-community Resque replaced the Delayed Job (by the way, I don’t know why the project stopped developing, it was a very convenient thing in my opinion) and has many different advantages, such as the separation of tasks across different machines, the priorities of tasks, resistance to different memory leaks and more, and more, and more. On this introduction for those who can not independently translate the first paragraph of the README please consider complete.
This article will show you how to use resque and resque-scheduler in a rails application.
We connect resque to the rails project
To use resque, you must install Redis, a key-value type data store. There should be no problems with the installation - Linux and Mac users can easily find both source code and ready-made packages, Windows users
themselves chose the way of masochists should also cope. In addition, you can install Redis using the Resque itself, more about this on the
project github page . After installation, it all starts with a simple redis-server command.
Next, add to the Gemfile:
')
gem 'resque' gem 'resue-scheduler'
Then bundle install and go!
In the config / initializers add the file resque.rb with about this content:
uri = URI.parse("redis://localhost:6379/") Resque.redis = Redis.new(:host => uri.host, :port => uri.port, :password => uri.password)
where 6379 is the port on which redis runs by default.
In order to have access to our background tasks via the web-interface, we add the following lines in config.ru:
require 'resque/server' run Rack::URLMap.new "/" => AppName::Application, "/resque" => Resque::Server.new
and at
localhost : 3000 / resque you can see all the necessary information about queues, tasks, the meaning of life, etc.
Add rake-task (sorry for such a transliteration, but the “task” is occupied by the job) - create the file lib / tasks / resque.rake with the following contents:
require 'resque/tasks' task "resque:setup" => :environment
and run it:
rake resque:work QUEUE=*
Now in the terminal window you can see the output of running tasks.
Basic functions
To begin with, let's create a class that will be the task that we need to perform. Before this, create a directory for storing task classes, for example app / jobs, and add this folder to config / initializers / resque.rb:
Dir["#{Rails.root}/app/jobs/*.rb"].each { |file| require file }
The contents of the class should be something like this:
class SimpleJob @queue = :simple def self.perform
Pay attention to the queue name - simple, this is mandatory, as is the perform method. You can add this task to the queue like this: Resque.enqueue (SimpleJob). The result of the execution, the string “Job is done” will be displayed in the resque console, besides, at the address
localhost : 3000 / resque you can see the new queue added and the result of the work of the worker.
Passing arguments in perform is also not difficult. For example, we change our class as follows:
def self.perform(str)
And add the task to the queue as follows: Resque.enqueue (SimpleJob, “Yahoo!”). After rake resque is restarted: work QUEUE = * in the console you will see the expected: “Job is done! Yahoo! It should be noted that all the arguments passed through enqueue are serialized into JSON, because of which Symbol objects become regular strings, and complex objects, such as ActiveRecord entities, havehes. Accordingly, if you want to use the methods and associations of your models, it makes sense to transfer just the id of the objects and select them already inside perform.
Task Manager
Resque-scheduler - Resque extension for tasks that must be completed in the future. It supports two types of tasks - delayed and scheduled tasks. Add a scheduler to our project. Add the scheduler configuration to resque.rake, now it looks like this:
require 'resque/tasks' require 'resque_scheduler/tasks' task "resque:setup" => :environment namespace :resque do task :setup do require 'resque' require 'resque_scheduler' require 'resque/scheduler' Resque.redis = 'localhost:6379' Resque.schedule = YAML.load_file("#{Rails.root}/config/rescue_schedule.yml") end end
As you can see from this section of the code, we load the schedule from the config / rescue_schedule.yml file:
every_two_minute_job: cron: "*/2 * * * *" class: SimpleTask args: some arg description: “every two minute job”
To use the scheduler in a rails application, you must also add in the config / initializers / resque.rb the line require 'resque_scheduler'
Now we run rake resque: scheduler in the console and see how our task is performed every 2 minutes. Adding delayed tasks is even easier:
Resque.enqueue_at(2.minutes.from_now, SimpleTask, 'some arg')
to complete a task at a specific time,
Resque.enqueue_in(2.minutes, SimpleTask, 'some arg')
to complete a task after a certain time.
You can also delete added delayed tasks:
Resque.remove_delayed(SimpleTask, 'some arg')
Of course, the work of Resque can and should be tested, for this there are appropriate gems:
github.com/leshill/resque_specgithub.com/justinweiss/resque_unitAdditional links
Resque code on githab
github.com/defunkt/resqueResque-scheduler there
github.com/bvandenbos/resque-schedulerOnline documentation
defunkt.io/resqueScreencast by Ryan Bates
railscasts.com/episodes/271-resqueGuest Dave Hoover article for RubyLearning Blog
rubylearning.com/blog/2010/11/08/do-you-know-resqueJon Homan - Background Jobs in Rails with Resque
blog.jonhoman.com/background-jobs-in-rails-with-resqueAllen Fair - Understanding how Resque works
girders.org/resque-internals-for-heterogenous-systems