Class: ProcessBot::Process

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/process_bot/process.rb

Defined Under Namespace

Classes: Handlers, Runner

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Process

Returns a new instance of Process.



16
17
18
19
20
21
22
23
24
# File 'lib/process_bot/process.rb', line 16

def initialize(options)
  @options = options
  @stopped = false

  options.events.connect(:on_process_started, &method(:on_process_started)) # rubocop:disable Performance/MethodObjectAsBlock
  options.events.connect(:on_socket_opened, &method(:on_socket_opened)) # rubocop:disable Performance/MethodObjectAsBlock

  logger.logs("ProcessBot 1 - Options: #{options.options}")
end

Instance Attribute Details

#current_pidObject (readonly)

Returns the value of attribute current_pid.



14
15
16
# File 'lib/process_bot/process.rb', line 14

def current_pid
  @current_pid
end

#current_process_titleObject (readonly)

Returns the value of attribute current_process_title.



14
15
16
# File 'lib/process_bot/process.rb', line 14

def current_process_title
  @current_process_title
end

#optionsObject (readonly)

Returns the value of attribute options.



14
15
16
# File 'lib/process_bot/process.rb', line 14

def options
  @options
end

#portObject (readonly)

Returns the value of attribute port.



14
15
16
# File 'lib/process_bot/process.rb', line 14

def port
  @port
end

#stoppedObject (readonly)

Returns the value of attribute stopped.



14
15
16
# File 'lib/process_bot/process.rb', line 14

def stopped
  @stopped
end

Instance Method Details

#clientObject



38
39
40
# File 'lib/process_bot/process.rb', line 38

def client
  @client ||= ProcessBot::ClientSocket.new(options: options)
end

#execute!Object



26
27
28
29
30
31
32
33
34
35
36
# File 'lib/process_bot/process.rb', line 26

def execute!
  command = options.fetch(:command)

  if command == "start"
    start
  elsif command == "graceful" || command == "stop"
    client.send_command(command: command, options: options.options)
  else
    raise "Unknown command: #{command}"
  end
end

#handler_classObject



42
43
44
45
46
47
# File 'lib/process_bot/process.rb', line 42

def handler_class
  @handler_class ||= begin
    require_relative "process/handlers/#{handler_name}"
    ProcessBot::Process::Handlers.const_get(StringCases.snake_to_camel(handler_name))
  end
end

#handler_instanceObject



49
50
51
# File 'lib/process_bot/process.rb', line 49

def handler_instance
  @handler_instance ||= handler_class.new(self)
end

#handler_nameObject



53
54
55
# File 'lib/process_bot/process.rb', line 53

def handler_name
  @handler_name ||= options.fetch(:handler)
end

#loggerObject



57
58
59
# File 'lib/process_bot/process.rb', line 57

def logger
  @logger ||= ProcessBot::Logger.new(options: options)
end

#on_process_started(_event_name, pid:) ⇒ Object



61
62
63
64
# File 'lib/process_bot/process.rb', line 61

def on_process_started(_event_name, pid:)
  @current_pid = pid
  update_process_title
end

#on_socket_opened(_event_name, port:) ⇒ Object



66
67
68
69
# File 'lib/process_bot/process.rb', line 66

def on_socket_opened(_event_name, port:)
  @port = port
  update_process_title
end

#runObject



95
96
97
98
# File 'lib/process_bot/process.rb', line 95

def run
  runner = ProcessBot::Process::Runner.new(command: handler_instance.start_command, logger: logger, options: options)
  runner.run
end

#set_stoppedObject



91
92
93
# File 'lib/process_bot/process.rb', line 91

def set_stopped
  @stopped = true
end

#startObject



76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/process_bot/process.rb', line 76

def start
  start_control_socket

  loop do
    run

    if stopped
      break
    else
      logger.logs "Process stopped - starting again after 1 sec"
      sleep 1
    end
  end
end

#start_control_socketObject



71
72
73
74
# File 'lib/process_bot/process.rb', line 71

def start_control_socket
  @control_socket = ProcessBot::ControlSocket.new(options: options, process: self)
  @control_socket.start
end

#update_process_titleObject



100
101
102
103
104
# File 'lib/process_bot/process.rb', line 100

def update_process_title
  process_args = {application: options[:application], handler: handler_name, id: options[:id], pid: current_pid, port: port}
  @current_process_title = "ProcessBot #{JSON.generate(process_args)}"
  Process.setproctitle(current_process_title)
end