Class: Smith::Agent

Inherits:
Object
  • Object
show all
Includes:
Logger, ObjectCount
Defined in:
lib/smith/agent.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from ObjectCount

#object_count

Methods included from Logger

included

Constructor Details

#initialize(uuid) ⇒ Agent

Returns a new instance of Agent.



11
12
13
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/smith/agent.rb', line 11

def initialize(uuid)
  @name = self.class.to_s
  @pid = $$
  @uuid = uuid

  @factory = QueueFactory.new

  @signal_handlers = Hash.new { |h,k| h[k] = Array.new }

  setup_control_queue

  @start_time = Time.now

  @state = :starting

  @on_stopping = proc {|completion| completion.succeed }
  @on_starting = proc {|completion| completion.succeed }
  @on_running = proc {|completion| completion.succeed }

  @on_starting_completion = EM::Completion.new.tap do |c|
    c.completion do |completion|
      acknowledge_start do
        @on_running.call(@on_running_completion)
        logger.info { "Agent started: #{name}:[#{pid}]." }
      end
    end
  end

  @on_running_completion = EM::Completion.new.tap do |c|
    c.completion do |completion|
      start_keep_alive
      setup_stats_queue
      @state = :running
    end
  end

  @on_stopping_completion = EM::Completion.new.tap do |c|
    c.completion do |completion|
      acknowledge_stop do
        @state = :stopping
        Smith.stop do
          logger.info { "Agent stopped: #{name}:[#{pid}]." }
        end
      end
    end
  end

  EM.threadpool_size = 1

  @on_starting.call(@on_starting_completion)
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



9
10
11
# File 'lib/smith/agent.rb', line 9

def name
  @name
end

#pidObject (readonly)

Returns the value of attribute pid.



9
10
11
# File 'lib/smith/agent.rb', line 9

def pid
  @pid
end

#uuidObject (readonly)

Returns the value of attribute uuid.



9
10
11
# File 'lib/smith/agent.rb', line 9

def uuid
  @uuid
end

Class Method Details

.options(opts) ⇒ Object

Options supported: :monitor, the agency will monitor the agent & if dies restart. :singleton, only every have one agent. If this is set to false

multiple agents are allowed.


108
109
110
111
112
# File 'lib/smith/agent.rb', line 108

def options(opts)
  opts.each do |k, v|
    Smith.config.agent.send("#{k}=", v)
  end
end

Instance Method Details

#install_signal_handler(signal, position = :end, &blk) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
83
84
# File 'lib/smith/agent.rb', line 76

def install_signal_handler(signal, position=:end, &blk)
  raise ArgumentError, "Unknown position: #{position}" if ![:beginning, :end].include?(position)

  logger.verbose { "Installing signal handler for #{signal}" }
  @signal_handlers[signal].insert((position == :beginning) ? 0 : -1, blk)
  @signal_handlers.each do |sig, handlers|
    trap(sig, proc { |sig| run_signal_handlers(sig, handlers) })
  end
end

#on_running(&blk) ⇒ Object



67
68
69
# File 'lib/smith/agent.rb', line 67

def on_running(&blk)
  @on_running = blk
end

#on_stopping(&blk) ⇒ Object



63
64
65
# File 'lib/smith/agent.rb', line 63

def on_stopping(&blk)
  @on_stopping = blk
end

#receiver(queue_name, opts = {}, &blk) ⇒ Object



95
96
97
# File 'lib/smith/agent.rb', line 95

def receiver(queue_name, opts={}, &blk)
  queues.receiver(queue_name, opts, &blk)
end

#runObject

Override this method to implement your own agent.

Raises:

  • (ArgumentError)


72
73
74
# File 'lib/smith/agent.rb', line 72

def run
  raise ArgumentError, "You must override this method"
end

#sender(queue_name, opts = {}, &blk) ⇒ Object



99
100
101
# File 'lib/smith/agent.rb', line 99

def sender(queue_name, opts={}, &blk)
  queues.sender(queue_name, opts, &blk)
end

#stateObject



86
87
88
# File 'lib/smith/agent.rb', line 86

def state
  @state
end