Created
March 30, 2011 09:32
-
-
Save sbusso/894121 to your computer and use it in GitHub Desktop.
A simple module to add async method to your activerecord or other ruby objects
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
module Resque | |
module AsyncJob | |
@@_queue= :default | |
def self.included(base) | |
base.extend(ClassMethods) | |
end | |
module ClassMethods | |
# Set the default queue. chain method | |
def queue(new_queue = nil) | |
if new_queue | |
@@_queue = new_queue | |
return self | |
else | |
@@_queue | |
end | |
end | |
def perform(method, id=nil, *args) | |
if id && id != 0 | |
find(id).send(method, *args) | |
else | |
if !id or id == 0 | |
self.send(method, *args) | |
else | |
self.send(method, id, *args) | |
end | |
end | |
end | |
def async(method, *args) | |
Job.create(@@queue, self, method, 0, *args) | |
Plugin.after_enqueue_hooks(self).each do |hook| | |
klass.send(hook, *args) | |
end | |
end | |
end | |
def queue(new_queue = nil) | |
if new_queue | |
@@_queue = new_queue | |
return self | |
else | |
@@_queue | |
end | |
end | |
def async(method, *args) | |
Job.create(@@_queue, self.class, method, self.id, *args) | |
Plugin.after_enqueue_hooks(self.class).each do |hook| | |
klass.send(hook, *args) | |
end | |
end | |
end | |
end | |
if defined? ActiveRecord | |
class ActiveRecord::Base | |
include Resque::AsyncJob | |
end | |
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
require 'yaml' | |
require 'resque_scheduler' # include the resque_scheduler | |
Resque.schedule = YAML.load_file(File.join(File.dirname(__FILE__), '../../config/resque_schedule.yml')) # load the schedule | |
Resque::Server.use Rack::Auth::Basic do |username, password| | |
username == 'USERNAME' && password == 'PASSWORD' | |
end | |
Resque.redis.namespace = "resque:site_name" |
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
daily: | |
cron: "0 0 * * * " | |
queue: cron | |
class: CronJob | |
args: daily |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment