class Writefully::Tools::Dispatcher

Attributes

job[R]

Public Class Methods

new() click to toggle source
# File lib/writefully/tools/dispatcher.rb, line 8
def initialize
  every 1.second do 
    async.heartbeat
  end
end

Public Instance Methods

dispatch() click to toggle source
# File lib/writefully/tools/dispatcher.rb, line 32
def dispatch
  Celluloid::Actor[job[:worker]].perform(job[:message])
end
get_job_data() click to toggle source
# File lib/writefully/tools/dispatcher.rb, line 14
def get_job_data
  Writefully.redis.with { |c| c.lpop 'jobs' }
end
heartbeat() click to toggle source
# 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?() click to toggle source
# 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?() click to toggle source
# File lib/writefully/tools/dispatcher.rb, line 44
def job_valid?
  job.has_key?(:worker) and job.has_key?(:message)
end
retry_job() click to toggle source
# File lib/writefully/tools/dispatcher.rb, line 36
def retry_job
  Celluloid::Actor[:retryer].retry(job)
end
retry_valid?() click to toggle source
# File lib/writefully/tools/dispatcher.rb, line 48
def retry_valid?
  is_retry? and (job[:message][:run] == false)
end
run_job() click to toggle source
# File lib/writefully/tools/dispatcher.rb, line 26
def run_job
  if    retry_valid?   then retry_job
  elsif job_valid?     then dispatch
  end
end