class BotConsole::BotProcess
Constants
- DEFAULT_FORCE_STOP_TIMEOUT_SEC
- DEFAULT_GRACEFUL_STOP_TIMEOUT_SEC
- STATES
Attributes
Public Class Methods
Source
# File lib/bot_console/bot_process.rb, line 20 def initialize(entry:, root:, env: {}) @entry = entry @root = root @env = env log_path = File.join(root, "data", "bot_console", "logs", "#{entry[:id]}.log") @buffer = LogBuffer.new(log_path: log_path) @mutex = Mutex.new @pid = nil @exit_code = nil @state = :stopped @enabled = true @reader_thread = nil @io = nil end
Public Instance Methods
Source
# File lib/bot_console/bot_process.rb, line 63 def disable! stop @mutex.synchronize { @enabled = false; @state = :disabled } end
Source
# File lib/bot_console/bot_process.rb, line 68 def enable! @mutex.synchronize { @enabled = true; @state = :stopped unless @state == :running } end
Source
# File lib/bot_console/bot_process.rb, line 43 def level_rank RabbotDb.level_rank(level) end
Source
# File lib/bot_console/bot_process.rb, line 145 def reload_plugins pid = @mutex.synchronize { @pid } return false unless pid signal_pid(pid, "USR1") @buffer.append("[reload plugins signaled]") true rescue StandardError => e @buffer.append("[reload plugins failed: #{e.message}]") false end
Source
# File lib/bot_console/bot_process.rb, line 51 def running? @mutex.synchronize { @state == :running } end
Source
# File lib/bot_console/bot_process.rb, line 157 def shutdown stop stop_reader end
Source
# File lib/bot_console/bot_process.rb, line 72 def start return false unless @enabled return true if running? stop_reader @mutex.synchronize { @exit_code = nil } reader_io, writer_io = IO.pipe @io = writer_io pid = Process.spawn( @env, "bundle", "exec", "ruby", "rabbot.rb", @entry[:rel_config], chdir: @root, out: writer_io, err: writer_io, pgroup: true ) writer_io.close @mutex.synchronize do @pid = pid @state = :running end @buffer.append("[started pid #{pid}]") start_reader(reader_io, pid) true rescue StandardError => e @buffer.append("[start failed: #{e.message}]") @mutex.synchronize { @state = :crashed } false end
Source
# File lib/bot_console/bot_process.rb, line 106 def stop pid = @mutex.synchronize { @pid } return unless pid terminate_pid(pid, signal: "TERM") status = wait_for_exit(pid, timeout: graceful_stop_timeout_sec) unless status if process_alive?(pid) @buffer.append("[graceful stop timed out — sending KILL]") terminate_pid(pid, signal: "KILL") wait_for_exit(pid, timeout: force_stop_timeout_sec) end end @mutex.synchronize do @pid = nil @state = :stopped unless @state == :disabled end stop_reader end
Source
# File lib/bot_console/bot_process.rb, line 126 def terminate pid = @mutex.synchronize { @pid } return unless pid terminate_pid(pid, signal: "KILL") wait_for_exit(pid) @mutex.synchronize do @pid = nil @state = :stopped unless @state == :disabled end @buffer.append("[terminated]") stop_reader end