Class: ProcessBot::Process
- Inherits:
-
Object
- Object
- ProcessBot::Process
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)) options.events.connect(:on_socket_opened, &method(:on_socket_opened))
logger.logs("ProcessBot 1 - Options: #{options.options}")
end
|
Instance Attribute Details
#current_pid ⇒ Object
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_title ⇒ Object
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
|
#options ⇒ Object
Returns the value of attribute options.
14
15
16
|
# File 'lib/process_bot/process.rb', line 14
def options
@options
end
|
#port ⇒ Object
Returns the value of attribute port.
14
15
16
|
# File 'lib/process_bot/process.rb', line 14
def port
@port
end
|
#stopped ⇒ Object
Returns the value of attribute stopped.
14
15
16
|
# File 'lib/process_bot/process.rb', line 14
def stopped
@stopped
end
|
Instance Method Details
#client ⇒ Object
42
43
44
|
# File 'lib/process_bot/process.rb', line 42
def client
@client ||= ProcessBot::ClientSocket.new(options: options)
end
|
#execute! ⇒ Object
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
|
# File 'lib/process_bot/process.rb', line 26
def execute!
command = options.fetch(:command)
if command == "start"
start
elsif command == "graceful" || command == "stop"
begin
client.send_command(command: command, options: options.options)
rescue Errno::ECONNREFUSED => e
raise e unless options[:ignore_no_process_bot]
end
else
raise "Unknown command: #{command}"
end
end
|
#handler_class ⇒ Object
46
47
48
49
50
51
|
# File 'lib/process_bot/process.rb', line 46
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_instance ⇒ Object
53
54
55
|
# File 'lib/process_bot/process.rb', line 53
def handler_instance
@handler_instance ||= handler_class.new(self)
end
|
#handler_name ⇒ Object
57
58
59
|
# File 'lib/process_bot/process.rb', line 57
def handler_name
@handler_name ||= options.fetch(:handler)
end
|
#logger ⇒ Object
61
62
63
|
# File 'lib/process_bot/process.rb', line 61
def logger
@logger ||= ProcessBot::Logger.new(options: options)
end
|
#on_process_started(_event_name, pid:) ⇒ Object
65
66
67
68
|
# File 'lib/process_bot/process.rb', line 65
def on_process_started(_event_name, pid:)
@current_pid = pid
update_process_title
end
|
#on_socket_opened(_event_name, port:) ⇒ Object
70
71
72
73
|
# File 'lib/process_bot/process.rb', line 70
def on_socket_opened(_event_name, port:)
@port = port
update_process_title
end
|
#run ⇒ Object
105
106
107
|
# File 'lib/process_bot/process.rb', line 105
def run
runner.run
end
|
#runner ⇒ Object
109
110
111
112
113
114
115
116
117
|
# File 'lib/process_bot/process.rb', line 109
def runner
@runner ||= ProcessBot::Process::Runner.new(
command: handler_instance.start_command,
handler_name: handler_name,
handler_instance: handler_instance,
logger: logger,
options: options
)
end
|
#set_stopped ⇒ Object
75
76
77
|
# File 'lib/process_bot/process.rb', line 75
def set_stopped
@stopped = true
end
|
#start ⇒ Object
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
# File 'lib/process_bot/process.rb', line 84
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_socket ⇒ Object
79
80
81
82
|
# File 'lib/process_bot/process.rb', line 79
def start_control_socket
@control_socket = ProcessBot::ControlSocket.new(options: options, process: self)
@control_socket.start
end
|
#stop(**args) ⇒ Object
99
100
101
102
103
|
# File 'lib/process_bot/process.rb', line 99
def stop(**args)
puts "Stop process #{args}"
@stopped = true
handler_instance.stop
end
|
#update_process_title ⇒ Object
119
120
121
122
123
|
# File 'lib/process_bot/process.rb', line 119
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
|