Elegant and fun? These phrases are not something you would associate with a programming language. Right? But this is exactly what Ruby on Rails is. Ruby on Rails, more commonly called as just Rails, is a pure object-oriented programming language with simple and user-friendly clean syntax. And by meaning simple and user-friendly, it means that you can get a web application ready much sooner than with any other programming language. To go live, all you need is web server and database. And of course, some gems! One of the best part about Ruby is the huge list of categorized, reusable codes suitably termed as “gems”. The beauty of Ruby on Rails lies in the fact that with the help of these gems, you can create applications easily and with considerably less efforts as compared to other technologies. Read on to know about Rasque which is a gem to create background jobs.
Resque creates background jobs using Redis. Since it supports multiple queue, we can place them on multiple queues and process them later as well. And it comes with an administration interface for monitoring and managing the queues.
Resque is intended to be used in situations where high volume of job entries are required, and provides mechanisms to ensure reflectiveness and consistency of behaviour while providing statistics using a web dashboard.
When we have a long processing block of code which we don’t need to execute it inside our web application request cycle, a background processing queuing system is the only good solution. Hence we get faster performing ruby application.
Though Resque is inspired by DelayedJob to a great extent, if we need multiple queues in our APP, Resque is probably a better choice.
Choose Resque If:
- You require multiple queues
- You don’t particularly fancy numeric priorities
- You work with huge queues
- You work with Redis
- You’re not going to run short on RAM
Since there are a number of open source queuing systems available (delayed_job, beanstalk) we don’t need to write our own!
Here, it will go over how to setup the resque queuing system in a Ruby on Rails application.
Resque Setup:
bash:
brew install redis
redis-server /usr/local/etc/redis.conf
resque-web
rake resque:work QUEUE=’*’
Gemfile:
gem ‘resque’, :require => “resque/server”
Install the new gem:
bundle install
Create a redis config file called redis.yml in config:
defaults: &defaults
host: localhost
port: 3002development:
<<: *defaultstest:
<<: *defaultsstaging:
<<: *defaultsproduction:
<<: *defaults
Add an initializer file called resque.rb in config/initializers:
Dir[File.join(Rails.root, ‘app’, ‘jobs’, ‘*.rb’)].each { |file| require file }
config = YAML::load(File.open(“#{Rails.root}/config/redis.yml”))[Rails.env]
Resque.redis = Redis.new(:host => config[‘host’], :port => config[‘port’])
Add resque.rake to lib/tasks:
require ‘resque/tasks’
task “resque:setup” => :environment
Running Resque:
start redis:
redis-server
start resque
COUNT=5 QUEUE=* rake resque:workers
see web UI:
resque-web
How To Add Resque Jobs:
Create a job class:
class FeedsCollectionJob
@queue = :feeds_collection_jobdef self.perform(start_date, end_date)
puts “from #{start_date} to #{end_date}”
#TODO your long running process here
end
end
Run it using:
Resque.enqueue(FeedsCollectionJob, start_date, end_date)
This command will not block so you can embed this code in a model.
Workers:
It can be distributed between various machines, support the core priorities, are resilient to memory “leaks” and are optimized.
Resque workers are rake tasks that run forever.
start
loop do
if job = reserve
job.process
else
sleep 5 # frequency = 5
end
end
shutdown
To start a worker:
$ QUEUE=file_serve rake resque:work
We will need to load our application into memory.
Once installing Resque as Rails plugin, we may run this command from our RAILS_ROOT:
$ QUEUE=file_serve rake environment resque:work
Hence, it loads the environment now you can start a Worker.
Other than this we can write a resque:setup task with a dependency on the environment rake task:
task “resque:setup” => :environment
Introspection:
There should be a number of ways to introspect Resque’s behavior.
I would recommend all the best place to do this, the in-built Resque web dashboard.
http://localhost:3000/resque
Here, we would allow to inspect queues, workers, current working jobs, failed jobs and stack traces, and useful redis stats.
We can do the same by using the console;
Resque.info
Resque.queues
Resque.redis
Resque.size(queue_name)
Resque.peek(queue_name, start=1, count=1)
Resque.workers
Resque.working
Well there you have it! This article hope to serve about resque, how to configure it, how this gem is beneficial helping you build faster performing ruby App.
Do you have a great idea for an app and want to get it developed as fast as possible? Go for Ruby on Rails and while you are at it, know that RailsCarma is a great choice for your app development. Railscarma has been implementing Ruby on Rails from its nascent stages for development, training, deploying and contributing back to the Rails Community and provide best Ruby on Rails development services. RailsCarma provide end to end Ruby on Rails services including, consulting, architecture, building, management and extension to companies around the globe. You can also hire Ruby on Rails developers with an easy to hire process. Contact Us to know more.
Read more similar articles :