process_bot 0.1.13 → 0.1.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +1 -1
- data/lib/process_bot/capistrano/sidekiq_helpers.rb +0 -11
- data/lib/process_bot/control_socket.rb +59 -21
- data/lib/process_bot/process/handlers/sidekiq.rb +17 -21
- data/lib/process_bot/process.rb +2 -3
- data/lib/process_bot/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: d3abcda452c729ce0582b53ff117aa7ec8658d16ad52f05f7ff1be5e383e2422
|
|
4
|
+
data.tar.gz: 7499b97fa9e2ed082b739b241549baa4e511a5d0db0f024e921b4d174d842c20
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3680d58298af7cb511b3e5007abfafadfd3457b22230936e36f0925d6f8245b07de3116356f28b6beb18bf5cb36b86604b5bfeb323aa357adb2313d9ac20d733
|
|
7
|
+
data.tar.gz: 4bbbafc629c0b3af2f47f1c82d3aab2bceeb003dd7820df85c5764427e654e96f08e232ee4e237f1df12e0c9a28fd807b0e9902ea1520c673902c79a95b0b3c8
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
|
@@ -88,7 +88,7 @@ set :process_bot_log, false
|
|
|
88
88
|
When running ProcessBot directly, you can control graceful waiting and log file output:
|
|
89
89
|
|
|
90
90
|
```bash
|
|
91
|
-
bundle exec process_bot --command
|
|
91
|
+
bundle exec process_bot --command graceful_no_wait
|
|
92
92
|
bundle exec process_bot --command start --log-file-path /var/log/process_bot.log
|
|
93
93
|
```
|
|
94
94
|
|
|
@@ -36,14 +36,11 @@ module ProcessBot::Capistrano::SidekiqHelpers # rubocop:disable Metrics/ModuleLe
|
|
|
36
36
|
raise "No port in process bot data? #{process_bot_data}" unless process_bot_data["port"]
|
|
37
37
|
|
|
38
38
|
mode = "exec"
|
|
39
|
-
wait_for_gracefully_stopped = process_bot_wait_setting(command)
|
|
40
39
|
|
|
41
40
|
if mode == "runner"
|
|
42
41
|
args = {command: command, port: process_bot_data.fetch("port")}
|
|
43
42
|
args["log"] = fetch(:process_bot_log) unless fetch(:process_bot_log).nil?
|
|
44
43
|
|
|
45
|
-
args["wait_for_gracefully_stopped"] = wait_for_gracefully_stopped unless wait_for_gracefully_stopped.nil?
|
|
46
|
-
|
|
47
44
|
escaped_args = JSON.generate(args).gsub("\"", "\\\"")
|
|
48
45
|
rails_runner_command = "require 'process_bot'; ProcessBot::Process.new(ProcessBot::Options.from_args(#{escaped_args})).execute!"
|
|
49
46
|
|
|
@@ -61,7 +58,6 @@ module ProcessBot::Capistrano::SidekiqHelpers # rubocop:disable Metrics/ModuleLe
|
|
|
61
58
|
backend_command << "#{key} #{value}"
|
|
62
59
|
end
|
|
63
60
|
|
|
64
|
-
backend_command << " --wait-for-gracefully-stopped #{wait_for_gracefully_stopped}" unless wait_for_gracefully_stopped.nil?
|
|
65
61
|
else
|
|
66
62
|
raise "Unknown mode: #{mode}"
|
|
67
63
|
end
|
|
@@ -69,13 +65,6 @@ module ProcessBot::Capistrano::SidekiqHelpers # rubocop:disable Metrics/ModuleLe
|
|
|
69
65
|
backend.execute backend_command
|
|
70
66
|
end
|
|
71
67
|
|
|
72
|
-
def process_bot_wait_setting(command)
|
|
73
|
-
return true if command == :graceful
|
|
74
|
-
return false if command == :graceful_no_wait
|
|
75
|
-
|
|
76
|
-
nil
|
|
77
|
-
end
|
|
78
|
-
|
|
79
68
|
def running_process_bot_processes
|
|
80
69
|
sidekiq_app_name = fetch(:sidekiq_app_name, fetch(:application))
|
|
81
70
|
raise "No :sidekiq_app_name was set" unless sidekiq_app_name
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
require "socket"
|
|
2
|
+
require "json"
|
|
3
|
+
require "knjrbfw"
|
|
2
4
|
|
|
3
5
|
class ProcessBot::ControlSocket
|
|
4
6
|
attr_reader :options, :port, :process, :server
|
|
@@ -21,15 +23,25 @@ class ProcessBot::ControlSocket
|
|
|
21
23
|
end
|
|
22
24
|
|
|
23
25
|
def start_tcp_server
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
used_ports = used_process_bot_ports
|
|
27
|
+
attempts = 0
|
|
28
|
+
|
|
29
|
+
loop do
|
|
30
|
+
if used_ports.include?(@port)
|
|
31
|
+
@port += 1
|
|
32
|
+
next
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
attempts += 1
|
|
36
|
+
@server = actually_start_tcp_server("localhost", @port)
|
|
37
|
+
break
|
|
38
|
+
rescue Errno::EADDRINUSE, Errno::EADDRNOTAVAIL => e
|
|
39
|
+
if attempts <= 100
|
|
40
|
+
@port += 1
|
|
41
|
+
next
|
|
42
|
+
else
|
|
43
|
+
raise e
|
|
44
|
+
end
|
|
33
45
|
end
|
|
34
46
|
end
|
|
35
47
|
|
|
@@ -43,10 +55,16 @@ class ProcessBot::ControlSocket
|
|
|
43
55
|
|
|
44
56
|
def run_client_loop
|
|
45
57
|
Thread.new do
|
|
46
|
-
|
|
58
|
+
loop do
|
|
59
|
+
begin
|
|
60
|
+
client = server.accept
|
|
61
|
+
rescue IOError, Errno::EBADF
|
|
62
|
+
break
|
|
63
|
+
end
|
|
47
64
|
|
|
48
|
-
|
|
49
|
-
|
|
65
|
+
Thread.new do
|
|
66
|
+
handle_client(client)
|
|
67
|
+
end
|
|
50
68
|
end
|
|
51
69
|
end
|
|
52
70
|
end
|
|
@@ -67,15 +85,7 @@ class ProcessBot::ControlSocket
|
|
|
67
85
|
{}
|
|
68
86
|
end
|
|
69
87
|
|
|
70
|
-
|
|
71
|
-
command_type = "graceful"
|
|
72
|
-
command_options[:wait_for_gracefully_stopped] = false
|
|
73
|
-
end
|
|
74
|
-
|
|
75
|
-
logger.logs "Command #{command_type} with options #{command_options}"
|
|
76
|
-
|
|
77
|
-
process.__send__(command_type, **command_options)
|
|
78
|
-
client.puts(JSON.generate(type: "success"))
|
|
88
|
+
run_command(command_type, command_options, client)
|
|
79
89
|
rescue => e # rubocop:disable Style/RescueStandardError
|
|
80
90
|
logger.error e.message
|
|
81
91
|
logger.error e.backtrace
|
|
@@ -100,4 +110,32 @@ class ProcessBot::ControlSocket
|
|
|
100
110
|
|
|
101
111
|
new_hash
|
|
102
112
|
end
|
|
113
|
+
|
|
114
|
+
def run_command(command_type, command_options, client)
|
|
115
|
+
logger.logs "Command #{command_type} with options #{command_options}"
|
|
116
|
+
|
|
117
|
+
process.__send__(command_type, **command_options)
|
|
118
|
+
client.puts(JSON.generate(type: "success"))
|
|
119
|
+
end
|
|
120
|
+
|
|
121
|
+
def used_process_bot_ports
|
|
122
|
+
ports = []
|
|
123
|
+
|
|
124
|
+
Knj::Unix_proc.list("grep" => "ProcessBot") do |process|
|
|
125
|
+
process_command = process.data.fetch("cmd")
|
|
126
|
+
match = process_command.match(/ProcessBot (\{.+\})/)
|
|
127
|
+
next unless match
|
|
128
|
+
|
|
129
|
+
begin
|
|
130
|
+
process_data = JSON.parse(match[1])
|
|
131
|
+
rescue JSON::ParserError
|
|
132
|
+
next
|
|
133
|
+
end
|
|
134
|
+
|
|
135
|
+
port = process_data["port"]
|
|
136
|
+
ports << port.to_i if port
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
ports.uniq
|
|
140
|
+
end
|
|
103
141
|
end
|
|
@@ -21,10 +21,6 @@ class ProcessBot::Process::Handlers::Sidekiq
|
|
|
21
21
|
Process.detach(pid) if pid
|
|
22
22
|
end
|
|
23
23
|
|
|
24
|
-
def false_value?(value)
|
|
25
|
-
!value || value == "false"
|
|
26
|
-
end
|
|
27
|
-
|
|
28
24
|
def process_running?(pid)
|
|
29
25
|
return false unless pid
|
|
30
26
|
|
|
@@ -121,20 +117,6 @@ class ProcessBot::Process::Handlers::Sidekiq
|
|
|
121
117
|
end
|
|
122
118
|
end
|
|
123
119
|
|
|
124
|
-
def handle_graceful_wait(wait_for_gracefully_stopped)
|
|
125
|
-
if false_value?(wait_for_gracefully_stopped)
|
|
126
|
-
logger.logs "Dont wait for gracefully stopped - doing that in fork..."
|
|
127
|
-
|
|
128
|
-
daemonize do
|
|
129
|
-
wait_for_no_jobs_and_stop_sidekiq
|
|
130
|
-
exit
|
|
131
|
-
end
|
|
132
|
-
else
|
|
133
|
-
logger.logs "Wait for gracefully stopped..."
|
|
134
|
-
wait_for_no_jobs_and_stop_sidekiq
|
|
135
|
-
end
|
|
136
|
-
end
|
|
137
|
-
|
|
138
120
|
def start_command # rubocop:disable Metrics/AbcSize
|
|
139
121
|
args = []
|
|
140
122
|
|
|
@@ -159,15 +141,29 @@ class ProcessBot::Process::Handlers::Sidekiq
|
|
|
159
141
|
command
|
|
160
142
|
end
|
|
161
143
|
|
|
162
|
-
def graceful(**
|
|
163
|
-
wait_for_gracefully_stopped = args.fetch(:wait_for_gracefully_stopped, true)
|
|
144
|
+
def graceful(**_args)
|
|
164
145
|
process.set_stopped
|
|
165
146
|
|
|
166
147
|
return unless ensure_current_pid?
|
|
167
148
|
|
|
168
149
|
return unless send_tstp_or_return
|
|
169
150
|
|
|
170
|
-
|
|
151
|
+
logger.logs "Wait for gracefully stopped..."
|
|
152
|
+
wait_for_no_jobs_and_stop_sidekiq
|
|
153
|
+
end
|
|
154
|
+
|
|
155
|
+
def graceful_no_wait(**_args)
|
|
156
|
+
process.set_stopped
|
|
157
|
+
|
|
158
|
+
return unless ensure_current_pid?
|
|
159
|
+
|
|
160
|
+
return unless send_tstp_or_return
|
|
161
|
+
|
|
162
|
+
logger.logs "Dont wait for gracefully stopped - doing that in fork..."
|
|
163
|
+
daemonize do
|
|
164
|
+
wait_for_no_jobs_and_stop_sidekiq
|
|
165
|
+
exit
|
|
166
|
+
end
|
|
171
167
|
end
|
|
172
168
|
|
|
173
169
|
def stop(**_args)
|
data/lib/process_bot/process.rb
CHANGED
|
@@ -6,6 +6,7 @@ class ProcessBot::Process
|
|
|
6
6
|
extend Forwardable
|
|
7
7
|
|
|
8
8
|
def_delegator :handler_instance, :graceful
|
|
9
|
+
def_delegator :handler_instance, :graceful_no_wait
|
|
9
10
|
def_delegator :handler_instance, :stop
|
|
10
11
|
|
|
11
12
|
autoload :Handlers, "#{__dir__}/process/handlers"
|
|
@@ -29,10 +30,8 @@ class ProcessBot::Process
|
|
|
29
30
|
if command == "start"
|
|
30
31
|
logger.logs "Starting process"
|
|
31
32
|
start
|
|
32
|
-
elsif command == "graceful" || command == "stop"
|
|
33
|
+
elsif command == "graceful" || command == "graceful_no_wait" || command == "stop"
|
|
33
34
|
send_control_command(command)
|
|
34
|
-
elsif command == "graceful_no_wait"
|
|
35
|
-
send_control_command(command, wait_for_gracefully_stopped: false)
|
|
36
35
|
else
|
|
37
36
|
raise "Unknown command: #{command}"
|
|
38
37
|
end
|
data/lib/process_bot/version.rb
CHANGED