Class: Roundhouse::Processor

Inherits:
Object
  • Object
show all
Includes:
Actor, Util
Defined in:
lib/roundhouse/processor.rb

Overview

The Processor receives a message from the Manager and actually processes it. It instantiates the worker, runs the middleware chain and then calls Roundhouse::Worker#perform.

Constant Summary collapse

STATS_TIMEOUT =

To prevent a memory leak, ensure that stats expire. However, they should take up a minimal amount of storage so keep them around for a long time

24 * 60 * 60 * 365 * 5

Constants included from Util

Util::EXPIRY

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Actor

included

Methods included from Util

#fire_event, #hostname, #identity, #logger, #process_nonce, #redis, #want_a_hertz_donut?, #watchdog

Methods included from ExceptionHandler

#handle_exception

Constructor Details

#initialize(boss) ⇒ Processor

Returns a new instance of Processor.



34
35
36
# File 'lib/roundhouse/processor.rb', line 34

def initialize(boss)
  @boss = boss
end

Instance Attribute Details

#proxy_idObject

Returns the value of attribute proxy_id.



32
33
34
# File 'lib/roundhouse/processor.rb', line 32

def proxy_id
  @proxy_id
end

Class Method Details

.default_middlewareObject



21
22
23
24
25
26
27
28
29
30
# File 'lib/roundhouse/processor.rb', line 21

def self.default_middleware
  Middleware::Chain.new do |m|
    m.add Middleware::Server::Logging
    m.add Middleware::Server::RetryJobs
    if defined?(::ActiveRecord::Base)
      require 'roundhouse/middleware/server/active_record'
      m.add Roundhouse::Middleware::Server::ActiveRecord
    end
  end
end

Instance Method Details

#execute_job(worker, cloned_args) ⇒ Object



77
78
79
# File 'lib/roundhouse/processor.rb', line 77

def execute_job(worker, cloned_args)
  worker.perform(*cloned_args)
end

#inspectObject



73
74
75
# File 'lib/roundhouse/processor.rb', line 73

def inspect
  "<Processor##{object_id.to_s(16)}>"
end

#process(work) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/roundhouse/processor.rb', line 38

def process(work)
  msgstr = work.message
  queue = work.queue_id

  @boss.async.real_thread(proxy_id, Thread.current)

  ack = true
  begin
    msg = Roundhouse.load_json(msgstr)
    klass  = msg['class'].constantize
    worker = klass.new
    worker.jid = msg['jid']

    stats(worker, msg, queue) do
      Roundhouse.server_middleware.invoke(worker, msg, queue) do
        execute_job(worker, cloned(msg['args']))
      end
    end
  rescue Roundhouse::Shutdown
    # Had to force kill this job because it didn't finish
    # within the timeout.  Don't acknowledge the work since
    # we didn't properly finish it.
    ack = false
  rescue Exception => ex
    handle_exception(ex, msg || { :message => msgstr })
    raise
  ensure
    # Put the queue back into rotation
    Roundhouse.redis { |conn| Roundhouse::Monitor.push(conn, queue) }
    work.acknowledge if ack
  end

  @boss.async.processor_done(current_actor)
end