Class: OpenC3::ProcessManager

Inherits:
Object
  • Object
show all
Defined in:
lib/openc3/utilities/process_manager.rb

Overview

Spawns short lived processes and ensures they complete

Constant Summary collapse

MONITOR_CYCLE_SECONDS =
5
CLEANUP_CYCLE_SECONDS =
600
@@instance =
nil

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeProcessManager

Returns a new instance of ProcessManager.



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/openc3/utilities/process_manager.rb', line 66

def initialize
  @processes = []
  @monitor_thread = Thread.new do
    begin
      monitor()
    rescue => e
      Logger.error("ProcessManager unexpectedly died\n#{e.formatted}", scope: 'DEFAULT')
      raise "ProcessManager unexpectedly died\n#{e.formatted}"
    end
  end
end

Class Method Details

.instanceObject



61
62
63
64
# File 'lib/openc3/utilities/process_manager.rb', line 61

def self.instance
  @@instance = ProcessManager.new unless @@instance
  return @@instance
end

Instance Method Details

#monitorObject



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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/openc3/utilities/process_manager.rb', line 85

def monitor
  processes_to_delete = []
  cleanup_time = Time.now
  while true
    current_time = Time.now

    # Monitor Active Processes
    @processes.each do |process|
      # Check if the process is still alive
      if !process.alive?
        output = process.extract_output
        process.status.output = output
        if process.exit_code != 0
          process.status.state = "Crashed"
        elsif output.include?('"level":"ERROR"')
          process.status.state = "Error"
        elsif output.include?('"level":"WARN"')
          process.status.state = "Warning"
        else
          process.status.state = "Complete"
        end
        process.hard_stop
        processes_to_delete << process
      elsif process.expires_at < current_time
        process.status.state = "Expired"
        output = process.extract_output
        process.status.output = output
        process.hard_stop
        processes_to_delete << process
      end

      # Update Process Status
      process.status.update
    end
    processes_to_delete.each do |process|
      message = "Process #{process.status.name}:#{process.process_type}:#{process.detail} completed with state #{process.status.state}\nProcess Output:\n#{process.status.output}"
      if process.status.state != "Complete"
        if process.status.state == "Warning"
          Logger.warn(message, scope: process.scope)
        else
          Logger.error(message, scope: process.scope)
        end
      else
        Logger.info(message, scope: process.scope)
      end

      @processes.delete(process)
    end
    processes_to_delete.clear

    # Cleanup Old Process Status
    if (current_time - cleanup_time) > CLEANUP_CYCLE_SECONDS
      scopes = ScopeModel.names
      scopes.each do |scope|
        statuses = ProcessStatusModel.get_all_models(scope: scope)
        statuses.each do |_status_name, status|
          if (current_time - Time.from_nsec_from_epoch(status.updated_at)) > CLEANUP_CYCLE_SECONDS
            status.destroy
          end
        end
      end
    end

    sleep(MONITOR_CYCLE_SECONDS)
  end
end

#spawn(cmd_array, process_type, detail, expires_at, **kw_args) ⇒ Object



78
79
80
81
82
83
# File 'lib/openc3/utilities/process_manager.rb', line 78

def spawn(cmd_array, process_type, detail, expires_at, **kw_args)
  process = ProcessManagerProcess.new(cmd_array, process_type, detail, expires_at, **kw_args)
  process.start
  @processes << process
  return process
end