Created
January 4, 2009 22:05
-
-
Save mikewadhera/43187 to your computer and use it in GitHub Desktop.
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 LongRunningTasks | |
include Queueable | |
queue 'myqueue' | |
def download(url) | |
require 'open-uri' | |
open("#{RAILS_ROOT}/tmp/#{Time.now.to_i}", 'w+') { |f| f.write(open(url).read) } | |
end | |
end | |
# From client code... | |
LongRunningTasks.fire_download 'http://www.google.com/' |
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 Queueable | |
def self.included(target) | |
target.extend(ClassMethods) | |
end | |
# Called-back by queue manager with deserialized message | |
def call(task) | |
begin | |
send(task[:method], *task[:args]) | |
rescue => e | |
# Hook for exception notification | |
end | |
end | |
module ClassMethods | |
def queue(value=nil) | |
if value | |
@queue = value | |
register! | |
else | |
@queue | |
end | |
end | |
def register! | |
JRuby::Rack::Queues.register_listener(self.queue, self) | |
end | |
def method_missing(method, *args) | |
super unless method.to_s.match /^fire_/ | |
task = { | |
:method => method.to_s.sub(/^fire_/, ''), | |
:args => args | |
} | |
fire(task) | |
end | |
def fire(task) | |
JRuby::Rack::Queues.send_message(self.queue, task) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment