Skip to content

Instantly share code, notes, and snippets.

@wallace
Created June 15, 2012 18:44
Show Gist options
  • Save wallace/2938118 to your computer and use it in GitHub Desktop.
Save wallace/2938118 to your computer and use it in GitHub Desktop.
Sample Lonely Job implementation
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