Skip to content

Instantly share code, notes, and snippets.

@Radagaisus
Created October 27, 2013 05:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Radagaisus/7178284 to your computer and use it in GitHub Desktop.
Save Radagaisus/7178284 to your computer and use it in GitHub Desktop.
# Load the environment configuration options
config = require '../../config/environments'
# Require Redis
redis = require 'redis'
# Create a Redis client
client = redis.createClient()
# `queue` handles queueing a background job to Resque. We directly use
# the commands and arguments the Resque gem uses to store information
# in Redis.
# - options - an options object:
# - queue - {String} the name of the queue for the job
# - class - {String} The name of the Ruby class used to run the job
# - parameters - {Array} of parameters that will be serialized and
# passed to the Ruby class that handles the job.
# - callback - callback function, receives (error, results) directly
# from the Redis driver
#
# Usage:
#
# jobs.queue
# queue: 'facebook'
# class: 'Game::FacebookFriends'
# parameters: [player_id, facebook.id]
# callback: next
#
#
module.exports.queue = (options = {}) ->
# Stringify the job information
job_data = JSON.stringify
class: options.class
args: options.parameters
# And simply dispatch it to Redis
client
.multi()
# Resque uses a set to store all the available queues, make sure the
# queue we use is in that set
.sadd("#{config.jobs.prefix}:tasks:queues", options.queue)
# Resque uses a lists to store JSON-serialized jobs information, each
# queue is stored in a separate list, and when a Resque process is
# available it simply pops the first task from a queue.
.rpush("#{config.jobs.prefix}:tasks:queue:#{options.queue}", job_data)
# Call the callback on completion
.exec(options.callback)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment