Class: CloudReactorWrapperIO::StatusUpdater

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudreactor_wrapper_io/status_updater.rb

Constant Summary collapse

DEFAULT_STATUS_UPDATE_PORT =
2373

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(enabled: nil, port: nil, logger: nil) ⇒ StatusUpdater

Returns a new instance of StatusUpdater.



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/cloudreactor_wrapper_io/status_updater.rb', line 12

def initialize(enabled: nil, port: nil, logger: nil)
  @logger = logger

  unless @logger
    if defined?(Rails)
      @logger = Rails.logger
    else
      @logger = Logger.new(STDOUT)
    end
  end

  @socket = nil
  @port = nil

  if enabled.nil?
    @enabled = (ENV['PROC_WRAPPER_ENABLE_STATUS_UPDATE_LISTENER'] ||
      'FALSE').upcase == 'TRUE'
  else
    @enabled = enabled
  end

  if @enabled
    @logger.info('ProcessStatusUpdater is enabled')
  else
    @logger.info('ProcessStatusUpdater is disabled')
    return
  end

  if port.nil?
    @port = (ENV['PROC_WRAPPER_STATUS_UPDATE_SOCKET_PORT'] ||
      DEFAULT_STATUS_UPDATE_PORT).to_i
  else
    @port = port
  end

  at_exit do
    @logger.info('Shutting down status update socket ...')
    begin
      if @socket
        @socket.shutdown
        @socket = nil
        @logger.info('Done shutting down status update socket.')
      else
        @logger.info('No socket to close.')
      end
    rescue => ex
      @logger.info('Error shutting down status update socket.')
    end
  end
end

Instance Attribute Details

#enabledObject (readonly)

Returns the value of attribute enabled.



9
10
11
# File 'lib/cloudreactor_wrapper_io/status_updater.rb', line 9

def enabled
  @enabled
end

#portObject (readonly)

Returns the value of attribute port.



10
11
12
# File 'lib/cloudreactor_wrapper_io/status_updater.rb', line 10

def port
  @port
end

Instance Method Details

#closeObject



111
112
113
114
115
116
# File 'lib/cloudreactor_wrapper_io/status_updater.rb', line 111

def close
  if @socket
    @socket.shutdown
    @socket = nil
  end
end

#send_update(success_count: nil, error_count: nil, skipped_count: nil, expected_count: nil, last_status_message: nil, extra_props: nil) ⇒ Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/cloudreactor_wrapper_io/status_updater.rb', line 63

def send_update(success_count: nil, error_count: nil, skipped_count: nil,
  expected_count: nil, last_status_message: nil, extra_props: nil)
  unless @enabled
    return false
  end

  status_hash = {}

  unless success_count.nil?
    status_hash[:success_count] = success_count
  end

  unless error_count.nil?
    status_hash[:error_count] = error_count
  end

  unless skipped_count.nil?
    status_hash[:skipped_count] = skipped_count
  end

  unless expected_count.nil?
    status_hash[:expected_count] = expected_count
  end

  unless last_status_message.nil?
    status_hash[:last_status_message] = last_status_message
  end

  if extra_props
    status_hash.merge!(extra_props)
  end

  if status_hash.empty?
    return false
  end

  message = status_hash.to_json + "\n"

  begin
    socket.send(message, 0, '127.0.0.1', @port)
    true
  rescue => ex
    @logger.debug("Can't send status update, resetting socket")
    @socket = nil
    false
  end
end