Skip to content

Instantly share code, notes, and snippets.

@aaronvb
Created May 12, 2011 02:37
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 aaronvb/967829 to your computer and use it in GitHub Desktop.
Save aaronvb/967829 to your computer and use it in GitHub Desktop.
recurring resque with cron example
require 'redis'
require 'mysql2'
# assuming redis is running on the default port.
# if not, example: redis = Redis.new(:host => "10.0.1.1", :port => 6380)
redis = Redis.new
# Make sure queue exists, if not create it. When clearing a queue with the resque web interface, resque removes the queue, so here we just check to make sure it exists.
if redis.sismember('resque:queues', 'update_payment') == false
redis.sadd('resque:queues', 'update_payment')
end
# Mysql DB information
client = Mysql2::Client.new(:host => "localhost", :username => "root", :database => 'your_project_development')
# query the db for all payment records
results = client.query("SELECT `payments`.* FROM `payments` ORDER BY id asc")
# create a job in the update_payment queue that will update each payment, pass each payment id
results.each do |row|
redis.rpush('resque:queue:update_payment', "{\"class\":\"UpdatePayment\",\"args\":[#{row['id']}]}")
end
# m h dom mon dow command
0 0 * * * /usr/bin/ruby /path/to/your/file/cron_payment_update.rb
class UpdatePayment
@queue = :update_payment
def self.perform(payment_id)
payment = Payment.find_by_id(payment_id)
if payment
# update payment logic goes here
end
end
end
@aaronvb
Copy link
Author

aaronvb commented May 12, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment