Created
June 15, 2012 18:44
-
-
Save wallace/2938118 to your computer and use it in GitHub Desktop.
Sample Lonely Job implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ImportJob | |
extend Resque::Plugins::LonelyJob | |
def self.enqueue(account_id, import_id) | |
# All enqueued resque jobs have the same payload | |
Resque.enqueue(ImportJob, account_id) | |
# but we drop the import_id into an account specific | |
# redis list that we use when actually processing jobs to preserve job order | |
enqueue_payload(account_id, import_id) | |
end | |
def self.enqueue_payload(account_id, import_id) | |
Resque.redis.rpush "ImportJob:#{account_id}", import_id | |
end | |
def self.dequeue_payload(account_id) | |
Resque.redis.lpop "ImportJob:#{account_id}" | |
end | |
def self.redis_key(account_id) | |
"ImportJob:mutex:#{account_id}" | |
end | |
def self.perform(account_id) | |
import = Import.find(dequeue_payload(account_id)) | |
# do the work | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment