Resque/Background jobs Flashcards
What is Resque?
Redis-backed library for creating background jobs, placing those jobs on multiple queues, and processing them later.
Background jobs can be any Ruby class or module that responds to perform. Your existing classes can easily be converted to background jobs or you can create new classes specifically to do work. Or, you can do both.
Resque is inspired by ____ and is comprised of 3 parts
DelayedJob
1. A Ruby library for creating, querying, and processing jobs 2. A Rake task for starting a worker which processes jobs 3. A Sinatra app for monitoring queues, jobs, and workers.
Using Resque
Resque allows you to create jobs and place them on a queue, then, later, pull those jobs off the queue and process them.
Resque jobs are Ruby classes (or modules) which respond to the perform method. Here’s an example:
class Archive @queue = :file_serve
def self.perform(repo_id, branch = 'master') repo = Repository.find(repo_id) repo.create_archive(branch) end end
The @queue class instance variable determines which queue Archive jobs will be placed in. Queues are arbitrary and created on the fly - you can name them whatever you want and have as many as you want.
To place an Archive job on the file_serve queue, we might add this to our application’s pre-existing Repository class:
class Repository def async_create_archive(branch) Resque.enqueue(Archive, self.id, branch) end end