daemon_controller 2.0.0 → 3.0.0

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.
data/spec/echo_server.rb CHANGED
@@ -55,6 +55,9 @@ parser = OptionParser.new do |opts|
55
55
  opts.on("--no-daemonize", "Don't daemonize.") do
56
56
  options[:daemonize] = false
57
57
  end
58
+ opts.on("--ignore-sigterm", "Ignore SIGTERM.") do
59
+ options[:ignore_sigterm] = true
60
+ end
58
61
  end
59
62
  begin
60
63
  parser.parse!
@@ -77,6 +80,12 @@ if options[:log_message1]
77
80
  $stdout.flush
78
81
  end
79
82
 
83
+ if options[:ignore_sigterm]
84
+ Signal.trap("SIGTERM", "IGNORE")
85
+ end
86
+
87
+ sleep(options[:wait1])
88
+
80
89
  if ENV["ENV_FILE"]
81
90
  options[:env_file] = File.absolute_path(ENV["ENV_FILE"])
82
91
  end
@@ -104,7 +113,6 @@ def main(options)
104
113
  end
105
114
 
106
115
  if options[:pid_file]
107
- sleep(options[:wait1])
108
116
  File.open(options[:pid_file], "w") do |f|
109
117
  f.puts(Process.pid)
110
118
  end
data/spec/test_helper.rb CHANGED
@@ -39,33 +39,51 @@ trap("SIGQUIT") do
39
39
  end
40
40
 
41
41
  module TestHelper
42
+ def new_logger
43
+ @logger ||= begin
44
+ @log_stream = StringIO.new
45
+ logger = Logger.new(@log_stream)
46
+ logger.level = Logger::DEBUG
47
+ logger
48
+ end
49
+ end
50
+
51
+ def print_logs(example)
52
+ warn "----- LOGS FOR: #{example.full_description} ----"
53
+ warn @log_stream.string
54
+ warn "----- END LOGS -----"
55
+ end
56
+
42
57
  def new_controller(options = {})
43
- @start_command = String.new("./spec/run_echo_server -l spec/echo_server.log")
44
- if options[:log_message1]
45
- @start_command << " --log-message1 #{Shellwords.escape options[:log_message1]}"
58
+ @start_command = String.new("./spec/run_in_mri_ruby echo_server.rb -l spec/echo_server.log")
59
+ if (log_message1 = options.delete(:log_message1))
60
+ @start_command << " --log-message1 #{Shellwords.escape log_message1}"
46
61
  end
47
- if options[:log_message2]
48
- @start_command << " --log-message2 #{Shellwords.escape options[:log_message2]}"
62
+ if (log_message2 = options.delete(:log_message2))
63
+ @start_command << " --log-message2 #{Shellwords.escape log_message2}"
49
64
  end
50
- if options[:wait1]
51
- @start_command << " --wait1 #{options[:wait1]}"
65
+ if (wait1 = options.delete(:wait1))
66
+ @start_command << " --wait1 #{wait1}"
52
67
  end
53
- if options[:wait2]
54
- @start_command << " --wait2 #{options[:wait2]}"
68
+ if (wait2 = options.delete(:wait2))
69
+ @start_command << " --wait2 #{wait2}"
55
70
  end
56
- if options[:stop_time]
57
- @start_command << " --stop-time #{options[:stop_time]}"
71
+ if (stop_time = options.delete(:stop_time))
72
+ @start_command << " --stop-time #{stop_time}"
58
73
  end
59
- if options[:crash_before_bind]
74
+ if options.delete(:crash_before_bind)
60
75
  @start_command << " --crash-before-bind"
61
76
  end
62
- if options[:crash_signal]
63
- @start_command << " --crash-signal #{options[:crash_signal]}"
77
+ if (crash_signal = options.delete(:crash_signal))
78
+ @start_command << " --crash-signal #{crash_signal}"
64
79
  end
65
- if options[:no_daemonize]
80
+ if options.delete(:no_daemonize)
66
81
  @start_command << " --no-daemonize"
67
82
  end
68
- if !options[:no_write_pid_file]
83
+ if options.delete(:ignore_sigterm)
84
+ @start_command << " --ignore-sigterm"
85
+ end
86
+ if !options.delete(:no_write_pid_file)
69
87
  @start_command << " -P spec/echo_server.pid"
70
88
  end
71
89
  new_options = {
@@ -75,9 +93,10 @@ module TestHelper
75
93
  pid_file: "spec/echo_server.pid",
76
94
  log_file: "spec/echo_server.log",
77
95
  start_timeout: 30,
78
- stop_timeout: 30
96
+ stop_timeout: 30,
97
+ logger: new_logger
79
98
  }.merge(options)
80
- @controller = DaemonController.new(new_options)
99
+ @controller = DaemonController.new(**new_options)
81
100
  end
82
101
 
83
102
  def ping_echo_server
@@ -87,6 +106,10 @@ module TestHelper
87
106
  false
88
107
  end
89
108
 
109
+ def monotonic_time
110
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
111
+ end
112
+
90
113
  def write_file(filename, contents)
91
114
  File.write(filename, contents)
92
115
  end
@@ -105,8 +128,8 @@ module TestHelper
105
128
  end
106
129
 
107
130
  def eventually(deadline_duration = 1, check_interval = 0.05)
108
- deadline = Time.now + deadline_duration
109
- while Time.now < deadline
131
+ deadline = monotonic_time + deadline_duration
132
+ while monotonic_time < deadline
110
133
  if yield
111
134
  return
112
135
  else
@@ -115,6 +138,29 @@ module TestHelper
115
138
  end
116
139
  raise "Time limit exceeded"
117
140
  end
141
+
142
+ def wait_until_pid_file_available
143
+ eventually(30) do
144
+ @controller.send(:pid_file_available?)
145
+ end
146
+ end
147
+
148
+ def find_echo_server_pid
149
+ process_line = `ps aux`.lines.grep(/echo_server\.rb/).first
150
+ process_line.split[1].to_i if process_line
151
+ end
152
+
153
+ def kill_and_wait_echo_server
154
+ pid = find_echo_server_pid
155
+ if pid
156
+ Process.kill("SIGTERM", pid)
157
+ Timeout.timeout(5) do
158
+ while find_echo_server_pid
159
+ sleep(0.1)
160
+ end
161
+ end
162
+ end
163
+ end
118
164
  end
119
165
 
120
166
  # A thread which doesn't execute its block until the
@@ -3,16 +3,10 @@
3
3
 
4
4
  dir = File.dirname(__FILE__)
5
5
  Dir.chdir(File.dirname(dir))
6
- begin
7
- File.open("spec/echo_server.pid", "w") do |f|
8
- f.puts(Process.pid)
9
- end
10
- sleep 30
11
- rescue SignalException
12
- begin
13
- File.unlink("spec/echo_server.pid")
14
- rescue
15
- nil
16
- end
17
- raise
6
+
7
+ Signal.trap("SIGTERM", "IGNORE") if ARGV.include?("--ignore-sigterm")
8
+
9
+ File.open("spec/echo_server.pid", "w") do |f|
10
+ f.puts(Process.pid)
18
11
  end
12
+ sleep 60
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: daemon_controller
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hongli Lai
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-03-24 00:00:00.000000000 Z
10
+ date: 2025-04-08 00:00:00.000000000 Z
11
11
  dependencies: []
12
12
  description: A library for robust daemon management.
13
13
  email: software-signing@phusion.nl
@@ -26,7 +26,6 @@ files:
26
26
  - lib/daemon_controller/version.rb
27
27
  - spec/daemon_controller_spec.rb
28
28
  - spec/echo_server.rb
29
- - spec/run_echo_server
30
29
  - spec/test_helper.rb
31
30
  - spec/unresponsive_daemon.rb
32
31
  homepage: https://github.com/FooBarWidget/daemon_controller
data/spec/run_echo_server DELETED
@@ -1,9 +0,0 @@
1
- #!/bin/sh
2
- dir=`dirname "$0"`
3
- dir=`cd "$dir"; pwd`
4
- if test "$MRI_RUBY" != ""; then
5
- exec $MRI_RUBY "$dir/echo_server.rb" "$@"
6
- else
7
- echo 'In order to run echo_server, you must set the environment variable $MRI_RUBY.' >&2
8
- exit 1
9
- fi