Skip to content

Instantly share code, notes, and snippets.

@samlown
Created July 26, 2013 10:04
Show Gist options
  • Save samlown/6087758 to your computer and use it in GitHub Desktop.
Save samlown/6087758 to your computer and use it in GitHub Desktop.
Simple script to queue Resque worker tasks from the console or command line. Especially useful for cron tasks.
#!/usr/bin/env ruby
#
# Resque Runner
#
# Quickly queue a resque task using the command line. Fast alternative to running
# rake from the crontab, and prevent loading too many rake tasks.
#
# Usage:
#
# resque queue WorkerClass [arguments]
#
# Example:
#
# resque standard JourneyWorker send_notifications
#
# Each argument will be passed to the resque task. Currently, there is no support
# for sending option hashes.
#
# This is a bit of a hack as it does not load Resque. Although unlikely, if the Resque
# project changes, this file will need to be updated.
#
# The script also makes the assumption that you have a "redis.yml" file in a config
# folder to provide the redis server connection details.
#
require 'redis'
require 'yaml'
require 'json'
path = File.join(File.dirname(__FILE__), '../')
env = ENV['RAILS_ENV'] || 'development'
host, port, db = YAML.load_file(File.join(path, 'config/redis.yml'))[env].split(':')
# Prepare the redis connection
$redis = Redis.new(:host => host, :port => port, :db => db, :thread_safe => true)
queue = ARGV.shift
worker = ARGV.shift
args = ARGV.to_a
if queue.to_s.empty? || worker.to_s.empty?
puts "Please provide at least the queue and worker!"
puts
puts "Usage:"
puts
puts " resque queue WorkerClass [arguments]"
puts
puts "Example:"
puts
puts " resque express JourneyWorker send_notifications"
puts
end
payload = {
'class' => worker,
'args' => args
}
$redis.sadd "resque:queues", queue
$redis.rpush "resque:queue:#{queue}", JSON.dump(payload)
# Whenever gem example
job_type :resque, "cd :path && RAILS_ENV=:environment bundle exec ./script/resque :task :output"
every 10.minutes do
resque "standard JourneyWorker send_reservation_notifications"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment