Class: Writefully::Tools::Dispatcher

Inherits:
Object
  • Object
show all
Includes:
Celluloid
Defined in:
lib/writefully/tools/dispatcher.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDispatcher

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

#jobObject (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

#dispatchObject



32
33
34
# File 'lib/writefully/tools/dispatcher.rb', line 32

def dispatch
  Celluloid::Actor[job[:worker]].perform(job[:message])
end

#get_job_dataObject



14
15
16
# File 'lib/writefully/tools/dispatcher.rb', line 14

def get_job_data
  Writefully.redis.with { |c| c.lpop 'jobs' }
end

#heartbeatObject



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

Returns:

  • (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

Returns:

  • (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_jobObject



36
37
38
# File 'lib/writefully/tools/dispatcher.rb', line 36

def retry_job
  Celluloid::Actor[:retryer].retry(job)
end

#retry_valid?Boolean

Returns:

  • (Boolean)


48
49
50
# File 'lib/writefully/tools/dispatcher.rb', line 48

def retry_valid?
  is_retry? and (job[:message][:run] == false)
end

#run_jobObject



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