Class: Salemove::ProcessHandler::PivotProcess

Inherits:
Object
  • Object
show all
Defined in:
lib/salemove/process_handler/pivot_process.rb

Defined Under Namespace

Classes: Benchmarker, ServiceSpawner, TapServiceSpawner

Constant Summary collapse

DEFAULT_FULFILLABLE_TIMEOUT =
3
DEFAULT_EXECUTION_TIME_KEY =
'service.execution_time'.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(freddy:, logger:, statsd:, notifier: nil, notifier_factory: NotifierFactory, process_monitor: ProcessMonitor.new, process_name: 'Unknown process', log_error_as_string: false, execution_time_key: DEFAULT_EXECUTION_TIME_KEY, exit_enforcer: nil) ⇒ PivotProcess

Returns a new instance of PivotProcess.



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/salemove/process_handler/pivot_process.rb', line 29

def initialize(freddy:,
               logger:,
               statsd:,
               notifier: nil,
               notifier_factory: NotifierFactory,
               process_monitor: ProcessMonitor.new,
               process_name: 'Unknown process',
               log_error_as_string: false,
               execution_time_key: DEFAULT_EXECUTION_TIME_KEY,
               exit_enforcer: nil)
  @freddy = freddy
  @logger = logger
  @benchmarker = Benchmarker.new(
    statsd: statsd,
    application: process_name,
    execution_time_key: execution_time_key
  )
  @process_monitor = process_monitor
  @exception_notifier = notifier_factory.get_notifier(process_name, notifier)
  # Needed for forcing exit from jruby with exit(0)
  @exit_enforcer = exit_enforcer || Proc.new {}
  @log_error_as_string = log_error_as_string
end

Instance Attribute Details

#exception_notifierObject (readonly)

Returns the value of attribute exception_notifier.



13
14
15
# File 'lib/salemove/process_handler/pivot_process.rb', line 13

def exception_notifier
  @exception_notifier
end

#process_monitorObject (readonly)

Returns the value of attribute process_monitor.



13
14
15
# File 'lib/salemove/process_handler/pivot_process.rb', line 13

def process_monitor
  @process_monitor
end

Class Method Details

.trace_informationObject



21
22
23
24
25
26
27
# File 'lib/salemove/process_handler/pivot_process.rb', line 21

def self.trace_information
  if tracing_supported?
    {trace: Freddy.trace.context.to_h}
  else
    {}
  end
end

.tracing_supported?Boolean

Returns:

  • (Boolean)


15
16
17
18
19
# File 'lib/salemove/process_handler/pivot_process.rb', line 15

def self.tracing_supported?
  defined?(Freddy) &&
    Freddy.respond_to?(:trace) &&
    Freddy.trace.context.respond_to?(:to_h)
end

Instance Method Details

#spawn(service, blocking: true) ⇒ Object



53
54
55
56
57
58
# File 'lib/salemove/process_handler/pivot_process.rb', line 53

def spawn(service, blocking: true)
  @process_monitor.start

  @service_threads = spawn_queue_threads(service).concat(spawn_tap_threads(service))
  blocking ? wait_for_monitor : Thread.new { wait_for_monitor }
end

#spawn_queue_threads(service) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/salemove/process_handler/pivot_process.rb', line 60

def spawn_queue_threads(service)
  if service.class.const_defined?(:QUEUE)
    [
      ServiceSpawner.new(
        service,
        freddy: @freddy,
        logger: @logger,
        benchmarker: @benchmarker,
        exception_notifier: @exception_notifier,
        log_error_as_string: @log_error_as_string
      ).spawn
    ]
  else
    []
  end
end

#spawn_tap_threads(service) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/salemove/process_handler/pivot_process.rb', line 77

def spawn_tap_threads(service)
  if service.class.const_defined?(:TAPPED_QUEUES)
    service.class::TAPPED_QUEUES.map do |queue|
      spawner = TapServiceSpawner.new(
        service,
        freddy: @freddy,
        logger: @logger,
        benchmarker: @benchmarker,
        exception_notifier: @exception_notifier
      )
      spawner.spawn(queue)
    end
  else
    []
  end
end