Class: Creeper::Session
- Inherits:
-
Object
- Object
- Creeper::Session
- Defined in:
- lib/creeper/session.rb
Defined Under Namespace
Classes: BadURL
Instance Attribute Summary collapse
-
#beanstalk ⇒ Object
Returns the value of attribute beanstalk.
-
#beanstalk_url ⇒ Object
Returns the value of attribute beanstalk_url.
-
#before_handlers ⇒ Object
Returns the value of attribute before_handlers.
-
#error_handler ⇒ Object
Returns the value of attribute error_handler.
-
#handlers ⇒ Object
Returns the value of attribute handlers.
Class Method Summary collapse
-
.beanstalk_addresses(beanstalk_url) ⇒ Object
class methods ##.
- .beanstalk_host_and_port(uri_string) ⇒ Object
- .beanstalk_url ⇒ Object
Instance Method Summary collapse
- #all_jobs ⇒ Object
- #beanstalk_addresses ⇒ Object
- #before(&block) ⇒ Object
- #clear! ⇒ Object
- #connect ⇒ Object
- #disconnect ⇒ Object
-
#enqueue(job, args = {}, opts = {}) ⇒ Object
queue ##.
- #error(&block) ⇒ Object
-
#initialize(parent_session = nil) ⇒ Session
constructor
A new instance of Session.
-
#job(name, &block) ⇒ Object
handlers ##.
- #reconnect ⇒ Object
Constructor Details
#initialize(parent_session = nil) ⇒ Session
Returns a new instance of Session.
7 8 9 10 11 12 13 14 15 16 17 |
# File 'lib/creeper/session.rb', line 7 def initialize(parent_session = nil) if parent_session @beanstalk_url = parent_session.beanstalk_url @before_handlers = parent_session.before_handlers @error_handler = parent_session.error_handler @handlers = parent_session.handlers end @before_handlers ||= [] @handlers ||= {} end |
Instance Attribute Details
#beanstalk ⇒ Object
Returns the value of attribute beanstalk.
4 5 6 |
# File 'lib/creeper/session.rb', line 4 def beanstalk @beanstalk end |
#beanstalk_url ⇒ Object
Returns the value of attribute beanstalk_url.
4 5 6 |
# File 'lib/creeper/session.rb', line 4 def beanstalk_url @beanstalk_url end |
#before_handlers ⇒ Object
Returns the value of attribute before_handlers.
5 6 7 |
# File 'lib/creeper/session.rb', line 5 def before_handlers @before_handlers end |
#error_handler ⇒ Object
Returns the value of attribute error_handler.
5 6 7 |
# File 'lib/creeper/session.rb', line 5 def error_handler @error_handler end |
#handlers ⇒ Object
Returns the value of attribute handlers.
5 6 7 |
# File 'lib/creeper/session.rb', line 5 def handlers @handlers end |
Class Method Details
.beanstalk_addresses(beanstalk_url) ⇒ Object
class methods ##
27 28 29 30 31 32 |
# File 'lib/creeper/session.rb', line 27 def self.beanstalk_addresses(beanstalk_url) uris = beanstalk_url.split(/[\s,]+/) uris.map do |uri| beanstalk_host_and_port(uri) end end |
.beanstalk_host_and_port(uri_string) ⇒ Object
34 35 36 37 38 |
# File 'lib/creeper/session.rb', line 34 def self.beanstalk_host_and_port(uri_string) uri = URI.parse(uri_string) raise(BadURL, uri_string) if uri.scheme != 'beanstalk' "#{uri.host}:#{uri.port || 11300}" end |
.beanstalk_url ⇒ Object
40 41 42 |
# File 'lib/creeper/session.rb', line 40 def self.beanstalk_url ENV['BEANSTALK_URL'] || 'beanstalk://localhost/' end |
Instance Method Details
#all_jobs ⇒ Object
93 94 95 |
# File 'lib/creeper/session.rb', line 93 def all_jobs handlers.keys end |
#beanstalk_addresses ⇒ Object
73 74 75 |
# File 'lib/creeper/session.rb', line 73 def beanstalk_addresses singleton_class.beanstalk_addresses(beanstalk_url) end |
#before(&block) ⇒ Object
85 86 87 |
# File 'lib/creeper/session.rb', line 85 def before(&block) before_handlers << block end |
#clear! ⇒ Object
19 20 21 22 23 |
# File 'lib/creeper/session.rb', line 19 def clear! handlers.clear before_handlers.clear self.error_handler = nil end |
#connect ⇒ Object
50 51 52 |
# File 'lib/creeper/session.rb', line 50 def connect Beanstalk::Pool.new(beanstalk_addresses) end |
#disconnect ⇒ Object
54 55 56 57 58 |
# File 'lib/creeper/session.rb', line 54 def disconnect beanstalk.close.tap do @beanstalk = nil end end |
#enqueue(job, args = {}, opts = {}) ⇒ Object
queue ##
101 102 103 104 105 106 107 108 109 |
# File 'lib/creeper/session.rb', line 101 def enqueue(job, args = {}, opts = {}) pri = opts[:pri] || 65536 delay = [0, opts[:delay].to_i].max ttr = opts[:ttr] || 120 beanstalk.use job beanstalk.put [ job, args ].to_json, pri, delay, ttr rescue Beanstalk::NotConnected => e failed_connection(e) end |
#error(&block) ⇒ Object
89 90 91 |
# File 'lib/creeper/session.rb', line 89 def error(&block) self.error_handler = block end |
#job(name, &block) ⇒ Object
handlers ##
81 82 83 |
# File 'lib/creeper/session.rb', line 81 def job(name, &block) handlers[name] = block end |
#reconnect ⇒ Object
60 61 62 63 |
# File 'lib/creeper/session.rb', line 60 def reconnect disconnect rescue nil beanstalk end |