Class: ProcessBot::Process::Runner
- Inherits:
-
Object
- Object
- ProcessBot::Process::Runner
- Defined in:
- lib/process_bot/process/runner.rb
Instance Attribute Summary collapse
-
#command ⇒ Object
readonly
Returns the value of attribute command.
-
#exit_status ⇒ Object
readonly
Returns the value of attribute exit_status.
-
#handler_instance ⇒ Object
readonly
Returns the value of attribute handler_instance.
-
#handler_name ⇒ Object
readonly
Returns the value of attribute handler_name.
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#monitor ⇒ Object
readonly
Returns the value of attribute monitor.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#pid ⇒ Object
readonly
Returns the value of attribute pid.
-
#stop_time ⇒ Object
readonly
Returns the value of attribute stop_time.
-
#subprocess_pid ⇒ Object
readonly
Returns the value of attribute subprocess_pid.
Instance Method Summary collapse
- #find_sidekiq_pid ⇒ Object
-
#initialize(command:, handler_instance:, handler_name:, logger:, options:) ⇒ Runner
constructor
A new instance of Runner.
- #output(output:, type:) ⇒ Object
- #related_processes ⇒ Object
- #related_sidekiq_processes ⇒ Object
-
#run ⇒ Object
rubocop:disable Metrics/AbcSize.
- #running? ⇒ Boolean
- #sidekiq_app_name ⇒ Object
- #stop_related_processes ⇒ Object
- #subprocess_pgid ⇒ Object
Constructor Details
#initialize(command:, handler_instance:, handler_name:, logger:, options:) ⇒ Runner
Returns a new instance of Runner.
6 7 8 9 10 11 12 13 |
# File 'lib/process_bot/process/runner.rb', line 6 def initialize(command:, handler_instance:, handler_name:, logger:, options:) @command = command @handler_instance = handler_instance @handler_name = handler_name @logger = logger @monitor = Monitor.new @options = end |
Instance Attribute Details
#command ⇒ Object (readonly)
Returns the value of attribute command.
4 5 6 |
# File 'lib/process_bot/process/runner.rb', line 4 def command @command end |
#exit_status ⇒ Object (readonly)
Returns the value of attribute exit_status.
4 5 6 |
# File 'lib/process_bot/process/runner.rb', line 4 def exit_status @exit_status end |
#handler_instance ⇒ Object (readonly)
Returns the value of attribute handler_instance.
4 5 6 |
# File 'lib/process_bot/process/runner.rb', line 4 def handler_instance @handler_instance end |
#handler_name ⇒ Object (readonly)
Returns the value of attribute handler_name.
4 5 6 |
# File 'lib/process_bot/process/runner.rb', line 4 def handler_name @handler_name end |
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
4 5 6 |
# File 'lib/process_bot/process/runner.rb', line 4 def logger @logger end |
#monitor ⇒ Object (readonly)
Returns the value of attribute monitor.
4 5 6 |
# File 'lib/process_bot/process/runner.rb', line 4 def monitor @monitor end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
4 5 6 |
# File 'lib/process_bot/process/runner.rb', line 4 def @options end |
#pid ⇒ Object (readonly)
Returns the value of attribute pid.
4 5 6 |
# File 'lib/process_bot/process/runner.rb', line 4 def pid @pid end |
#stop_time ⇒ Object (readonly)
Returns the value of attribute stop_time.
4 5 6 |
# File 'lib/process_bot/process/runner.rb', line 4 def stop_time @stop_time end |
#subprocess_pid ⇒ Object (readonly)
Returns the value of attribute subprocess_pid.
4 5 6 |
# File 'lib/process_bot/process/runner.rb', line 4 def subprocess_pid @subprocess_pid end |
Instance Method Details
#find_sidekiq_pid ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/process_bot/process/runner.rb', line 131 def find_sidekiq_pid Thread.new do while running? && !pid .each do || # rubocop:disable Lint/UnreachableLoop puts "FOUND PID: #{.pid}" @pid = .pid .events.call(:on_process_started, pid: .pid) break end unless pid puts "Waiting 1 second before trying to find Sidekiq PID again" sleep 1 end end end end |
#output(output:, type:) ⇒ Object
15 16 17 |
# File 'lib/process_bot/process/runner.rb', line 15 def output(output:, type:) logger.log(output, type: type) end |
#related_processes ⇒ Object
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
# File 'lib/process_bot/process/runner.rb', line 75 def = [] Knj::Unix_proc.list do |process| begin process_pgid = Process.getpgid(process.pid) rescue Errno::ESRCH # Process no longer running end << process if subprocess_pgid == process_pgid end end |
#related_sidekiq_processes ⇒ Object
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 |
# File 'lib/process_bot/process/runner.rb', line 91 def = [] Knj::Unix_proc.list("grep" => "sidekiq") do |process| cmd = process.data.fetch("cmd") if /sidekiq ([0-9]+\.[0-9]+\.[0-9]+) (#{.possible_process_titles_joined_regex})/.match?(cmd) sidekiq_pid = process.data.fetch("pid").to_i begin sidekiq_pgid = Process.getpgid(sidekiq_pid) rescue Errno::ESRCH # Process no longer running end << process if subprocess_pgid == sidekiq_pgid end end end |
#run ⇒ Object
rubocop:disable Metrics/AbcSize
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 62 63 64 65 |
# File 'lib/process_bot/process/runner.rb', line 23 def run # rubocop:disable Metrics/AbcSize @start_time = Time.new stderr_reader, stderr_writer = IO.pipe require "pty" PTY.spawn(command, err: stderr_writer.fileno) do |stdout, _stdin, pid| @subprocess_pid = pid logger.logs "Command running with PID #{pid}: #{command}" stdout_reader_thread = Thread.new do stdout.each_char do |chunk| monitor.synchronize do output(type: :stdout, output: chunk) end end rescue Errno::EIO # Process done ensure status = Process::Status.wait(subprocess_pid, 0) @exit_status = status.exitstatus stderr_writer.close end stderr_reader_thread = Thread.new do stderr_reader.each_char do |chunk| monitor.synchronize do output(type: :stderr, output: chunk) end end end find_sidekiq_pid if handler_name == "sidekiq" stdout_reader_thread.join stderr_reader_thread.join logger.logs "Process stopped" @stop_time = Time.new end end |
#running? ⇒ Boolean
19 20 21 |
# File 'lib/process_bot/process/runner.rb', line 19 def running? !stop_time end |
#sidekiq_app_name ⇒ Object
71 72 73 |
# File 'lib/process_bot/process/runner.rb', line 71 def sidekiq_app_name .fetch(:application) end |
#stop_related_processes ⇒ Object
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/process_bot/process/runner.rb', line 113 def loop do processes = if processes.length <= 0 logger.logs "No related processes could be found" break else processes.each do |process| logger.logs "Terminating PID #{process.pid}: #{process.data.fetch("cmd")}" Process.kill("TERM", process.pid) end sleep 0.5 end end end |
#subprocess_pgid ⇒ Object
67 68 69 |
# File 'lib/process_bot/process/runner.rb', line 67 def subprocess_pgid @subprocess_pgid ||= Process.getpgid(subprocess_pid) end |