Class: Magent::Processor

Inherits:
Object
  • Object
show all
Defined in:
lib/magent/processor.rb

Instance Method Summary collapse

Constructor Details

#initialize(channel) ⇒ Processor

Returns a new instance of Processor.



3
4
5
6
7
8
9
10
11
12
# File 'lib/magent/processor.rb', line 3

def initialize(channel)
  @channel = channel
  @shutdown = false

#       @actor.class.actions.each do |action|
#         if [email protected]_to?(action)
#           raise ArgumentError, "action '#{action}' is not defined"
#         end
#       end
end

Instance Method Details

#run!Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/magent/processor.rb', line 14

def run!
  processed_messages = 0
  delay = 0

  trap('TERM') { shutdown!; exit 0 }
  trap('SIGINT') { shutdown!; exit 0 }

  loop do
    break if @shutdown

    message = @channel.dequeue
    begin
      if message && @channel.process!(message)
        puts "Processed #{message.inspect}"
        delay = 0
        processed_messages += 1
        if processed_messages > 20
          processed_messages = 0
          GC.start
        end
      else
        delay += 0.1 if delay <= 5
      end
    rescue SystemExit
    rescue Exception => e
      $stderr.puts "Error processing #{message.inspect} => #{e.message}"
      @channel.failed(:error => e.message, :message => message, :backtrace => e.backtrace, :date => Time.now.utc)
    ensure
    end

    sleep (delay*100.0).to_i/100.0
  end
end

#shutdown!Object



48
49
50
51
52
# File 'lib/magent/processor.rb', line 48

def shutdown!
  @shutdown = true
  @channel.on_shutdown if @channel.respond_to?(:on_shutdown)
  $stderr.puts "Shutting down..."
end