process_bot 0.1.14 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7faeef8de7bcedd0fb66cc0fcf161de87d94ee3d4e9cceda30086afc12fc09fb
4
- data.tar.gz: 79370a6f54b1f58ee5df361bdf99637e206310d126bbd9da2a8bbeae509561bb
3
+ metadata.gz: d3abcda452c729ce0582b53ff117aa7ec8658d16ad52f05f7ff1be5e383e2422
4
+ data.tar.gz: 7499b97fa9e2ed082b739b241549baa4e511a5d0db0f024e921b4d174d842c20
5
5
  SHA512:
6
- metadata.gz: 01ab0ae7b02f813fd00313750924b3162167506cc546b49c6d03dc38fd702b3a5d1082aa45cdc7267800575acdb517381b5243937214319b630ee99d765ee6a2
7
- data.tar.gz: 0df83d2af7a0180834e6bfb50d3fcd6e9834abb3a60d23a78cd5d80bc5a6c8441ad0f242da3cccb4b8eb4e49fa9ce12d041b87bd69be7d1e729e151f349d1937
6
+ metadata.gz: 3680d58298af7cb511b3e5007abfafadfd3457b22230936e36f0925d6f8245b07de3116356f28b6beb18bf5cb36b86604b5bfeb323aa357adb2313d9ac20d733
7
+ data.tar.gz: 4bbbafc629c0b3af2f47f1c82d3aab2bceeb003dd7820df85c5764427e654e96f08e232ee4e237f1df12e0c9a28fd807b0e9902ea1520c673902c79a95b0b3c8
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- process_bot (0.1.14)
4
+ process_bot (0.1.15)
5
5
  knjrbfw (>= 0.0.116)
6
6
  pry
7
7
  rake
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 graceful --wait-for-gracefully-stopped false
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
- tries ||= 0
25
- tries += 1
26
- @server = actually_start_tcp_server("localhost", @port)
27
- rescue Errno::EADDRINUSE, Errno::EADDRNOTAVAIL => e
28
- if tries <= 100
29
- @port += 1
30
- retry
31
- else
32
- raise e
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
 
@@ -100,18 +112,30 @@ class ProcessBot::ControlSocket
100
112
  end
101
113
 
102
114
  def run_command(command_type, command_options, client)
103
- command_type, command_options = normalize_command(command_type, command_options)
104
115
  logger.logs "Command #{command_type} with options #{command_options}"
105
116
 
106
117
  process.__send__(command_type, **command_options)
107
118
  client.puts(JSON.generate(type: "success"))
108
119
  end
109
120
 
110
- def normalize_command(command_type, command_options)
111
- return [command_type, command_options] unless command_type == "graceful_no_wait"
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
112
138
 
113
- command_type = "graceful"
114
- command_options[:wait_for_gracefully_stopped] = false
115
- [command_type, command_options]
139
+ ports.uniq
116
140
  end
117
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(**args)
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
- handle_graceful_wait(wait_for_gracefully_stopped)
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)
@@ -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
@@ -1,3 +1,3 @@
1
1
  module ProcessBot
2
- VERSION = "0.1.14".freeze
2
+ VERSION = "0.1.15".freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: process_bot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.14
4
+ version: 0.1.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaspernj