Class: Writefully::Tools::Dispatcher
- Inherits:
-
Object
- Object
- Writefully::Tools::Dispatcher
- Includes:
- Celluloid
- Defined in:
- lib/writefully/tools/dispatcher.rb
Instance Attribute Summary collapse
-
#job ⇒ Object
readonly
Returns the value of attribute job.
Instance Method Summary collapse
- #dispatch ⇒ Object
- #get_job_data ⇒ Object
- #heartbeat ⇒ Object
-
#initialize ⇒ Dispatcher
constructor
A new instance of Dispatcher.
- #is_retry? ⇒ Boolean
- #job_valid? ⇒ Boolean
- #retry_job ⇒ Object
- #retry_valid? ⇒ Boolean
- #run_job ⇒ Object
Constructor Details
#initialize ⇒ Dispatcher
Returns a new instance of Dispatcher.
8 9 10 11 12 |
# File 'lib/writefully/tools/dispatcher.rb', line 8 def initialize every 1.second do async.heartbeat end end |
Instance Attribute Details
#job ⇒ Object (readonly)
Returns the value of attribute job.
6 7 8 |
# File 'lib/writefully/tools/dispatcher.rb', line 6 def job @job end |
Instance Method Details
#dispatch ⇒ Object
32 33 34 |
# File 'lib/writefully/tools/dispatcher.rb', line 32 def dispatch Celluloid::Actor[job[:worker]].perform(job[:message]) end |
#get_job_data ⇒ Object
14 15 16 |
# File 'lib/writefully/tools/dispatcher.rb', line 14 def get_job_data Writefully.redis.with { |c| c.lpop 'jobs' } end |
#heartbeat ⇒ Object
18 19 20 21 22 23 24 |
# File 'lib/writefully/tools/dispatcher.rb', line 18 def heartbeat data = get_job_data if data @job = Marshal.load(data) run_job if job_valid? end end |
#is_retry? ⇒ Boolean
40 41 42 |
# File 'lib/writefully/tools/dispatcher.rb', line 40 def is_retry? job_valid? and job[:message].has_key?(:tries) and job[:message].has_key?(:run) end |
#job_valid? ⇒ Boolean
44 45 46 |
# File 'lib/writefully/tools/dispatcher.rb', line 44 def job_valid? job.has_key?(:worker) and job.has_key?(:message) end |
#retry_job ⇒ Object
36 37 38 |
# File 'lib/writefully/tools/dispatcher.rb', line 36 def retry_job Celluloid::Actor[:retryer].retry(job) end |
#retry_valid? ⇒ Boolean
48 49 50 |
# File 'lib/writefully/tools/dispatcher.rb', line 48 def retry_valid? is_retry? and (job[:message][:run] == false) end |
#run_job ⇒ Object
26 27 28 29 30 |
# File 'lib/writefully/tools/dispatcher.rb', line 26 def run_job if retry_valid? then retry_job elsif job_valid? then dispatch end end |