Skip to content

Instantly share code, notes, and snippets.

@chrismealy
Created July 17, 2012 13:27
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 chrismealy/3129390 to your computer and use it in GitHub Desktop.
Save chrismealy/3129390 to your computer and use it in GitHub Desktop.
A simple module to add async method to your activerecord or other ruby objects
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
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"
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