Created
May 12, 2011 02:37
-
-
Save aaronvb/967829 to your computer and use it in GitHub Desktop.
recurring resque with cron example
This file contains 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
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 |
This file contains 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
# m h dom mon dow command | |
0 0 * * * /usr/bin/ruby /path/to/your/file/cron_payment_update.rb |
This file contains 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 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://aaronvb.com/blog/2011/5/12/recurring-resque-and-redis-with-cron/