Class: Daemon::Supervision

Inherits:
Object
  • Object
show all
Includes:
Utils::Loggable
Defined in:
lib/onapp/engine/supervision.rb,
lib/onapp/engine/supervision/timers.rb,
lib/onapp/engine/supervision/registry.rb,
lib/onapp/engine/supervision/definition.rb,
lib/onapp/engine/supervision/messages_manager.rb,
lib/onapp/engine/supervision/process_supervisor.rb,
lib/onapp/engine/supervision/definition/handler.rb

Overview

Timers and Supervisors management

Defined Under Namespace

Classes: Definition, MessagesManager, ProcessSupervisor, Registry, Timers

Constant Summary

PROCEED =

This command being sent to supervisors from timers

'proceed'

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from Utils::Loggable

#debug, #error, #fatal, #info, #lwarn

Constructor Details

- (Supervision) initialize

Returns a new instance of Supervision



21
22
23
# File 'lib/onapp/engine/supervision.rb', line 21

def initialize
  @facility = 'Supervision' # this is for Daemon::Utils::Loggable
end

Instance Attribute Details

- (Daemon::Supervision::Definition) handlers

Returns All available handlers

Returns:



89
90
91
# File 'lib/onapp/engine/supervision.rb', line 89

def handlers
  @handlers ||= Daemon::Supervision::Definition.new
end

- (AMQP::Session) messaging_connection

Returns rabbitmq connection which is different for each system process

Returns:

  • (AMQP::Session)

    RabbitMQ connection



123
124
125
126
# File 'lib/onapp/engine/supervision.rb', line 123

def messaging_connection
  @messaging_connection ||= Daemon::Utils::ProcessCache.new(proc { Daemon::Messaging.connect })
  @messaging_connection.access
end

- (Daemon::Supervision::Timers) timers

Returns Timers instance

Returns:



72
73
74
# File 'lib/onapp/engine/supervision.rb', line 72

def timers
  @timers ||= Timers.new
end

Instance Method Details

- (Boolean) allow_timer?(timer_name)

Parameters:

  • timer_name (String)

    Timer name

Returns:

  • (Boolean)


108
109
110
# File 'lib/onapp/engine/supervision.rb', line 108

def allow_timer?(timer_name)
  @timer_guard.nil? || @timer_guard.call(timer_name)
end

- (Object) clear_timers

Stops and deletes all timers

See Also:



66
67
68
69
# File 'lib/onapp/engine/supervision.rb', line 66

def clear_timers
  debug { "Clearing timers" }
  timers.clear_timers
end

- (Object) define_supervisors(&block)

Provides DSL for adding supervisors, and grouping them by processes

Examples:

Daemon.supervision.define_supervisors do
  add MySupervisor, MyWorker, period: 5
  add MySupervisor2, MyWorker2
end


83
84
85
86
# File 'lib/onapp/engine/supervision.rb', line 83

def define_supervisors(&block)
  debug { "Defining supervisors" }
  @handlers = Daemon::Supervision::Definition.new(&block)
end

- (Daemon::Logger) logger

logger for Daemon::Utils::Loggable

Returns:

See Also:



30
31
32
# File 'lib/onapp/engine/supervision.rb', line 30

def logger
  @logger ||= Daemon.logger
end

- (Object) setup_timers

Bootstraps timers

See Also:



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/onapp/engine/supervision.rb', line 37

def setup_timers
  debug { "Setup timers" }
  handlers.each do |handler|
    if handler.period
      timers.set_timer(handler.name, handler.period) do
        begin
          handler.call if allow_timer?(handler.name)
        rescue => ex
          error {
            "Exception in #{handler.name} timer\n" <<
              "#{ex.class}: #{ex.message}\n" <<
              ex.backtrace.join("\n")
          }
        end
      end
    end
  end

  info {
    "Setup timers:\n" <<
      timers.map do |name, timer|
        "#{name} -> #{timer.interval}"
      end.join("\n")
  }
end

- (Object) subscribe

Convenience method

Causes each supervisor to subscribe to rabbitmq



115
116
117
118
# File 'lib/onapp/engine/supervision.rb', line 115

def subscribe
  debug { "Subscribing" }
  handlers.each(&:subscribe)
end

- (Object) timer_guard(&block) {|timer_name| ... }

Accepts block, which should return true or false When false - timer will not fire

Parameters:

  • block

Yields:

  • (timer_name)

Yield Parameters:

  • timer_name (String)

See Also:



102
103
104
# File 'lib/onapp/engine/supervision.rb', line 102

def timer_guard(&block)
  @timer_guard = block
end