daemon_controller 2.0.0 → 3.0.1

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
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "shellwords"
4
+ require "logger"
4
5
 
5
6
  root = File.absolute_path(File.join(File.dirname(__FILE__), ".."))
6
7
  Dir.chdir(root)
@@ -39,33 +40,51 @@ trap("SIGQUIT") do
39
40
  end
40
41
 
41
42
  module TestHelper
43
+ def new_logger
44
+ @logger ||= begin
45
+ @log_stream = StringIO.new
46
+ logger = Logger.new(@log_stream)
47
+ logger.level = Logger::DEBUG
48
+ logger
49
+ end
50
+ end
51
+
52
+ def print_logs(example)
53
+ warn "----- LOGS FOR: #{example.full_description} ----"
54
+ warn @log_stream.string
55
+ warn "----- END LOGS -----"
56
+ end
57
+
42
58
  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]}"
59
+ @start_command = String.new("./spec/run_in_mri_ruby echo_server.rb -l spec/echo_server.log")
60
+ if (log_message1 = options.delete(:log_message1))
61
+ @start_command << " --log-message1 #{Shellwords.escape log_message1}"
46
62
  end
47
- if options[:log_message2]
48
- @start_command << " --log-message2 #{Shellwords.escape options[:log_message2]}"
63
+ if (log_message2 = options.delete(:log_message2))
64
+ @start_command << " --log-message2 #{Shellwords.escape log_message2}"
49
65
  end
50
- if options[:wait1]
51
- @start_command << " --wait1 #{options[:wait1]}"
66
+ if (wait1 = options.delete(:wait1))
67
+ @start_command << " --wait1 #{wait1}"
52
68
  end
53
- if options[:wait2]
54
- @start_command << " --wait2 #{options[:wait2]}"
69
+ if (wait2 = options.delete(:wait2))
70
+ @start_command << " --wait2 #{wait2}"
55
71
  end
56
- if options[:stop_time]
57
- @start_command << " --stop-time #{options[:stop_time]}"
72
+ if (stop_time = options.delete(:stop_time))
73
+ @start_command << " --stop-time #{stop_time}"
58
74
  end
59
- if options[:crash_before_bind]
75
+ if options.delete(:crash_before_bind)
60
76
  @start_command << " --crash-before-bind"
61
77
  end
62
- if options[:crash_signal]
63
- @start_command << " --crash-signal #{options[:crash_signal]}"
78
+ if (crash_signal = options.delete(:crash_signal))
79
+ @start_command << " --crash-signal #{crash_signal}"
64
80
  end
65
- if options[:no_daemonize]
81
+ if options.delete(:no_daemonize)
66
82
  @start_command << " --no-daemonize"
67
83
  end
68
- if !options[:no_write_pid_file]
84
+ if options.delete(:ignore_sigterm)
85
+ @start_command << " --ignore-sigterm"
86
+ end
87
+ if !options.delete(:no_write_pid_file)
69
88
  @start_command << " -P spec/echo_server.pid"
70
89
  end
71
90
  new_options = {
@@ -75,9 +94,10 @@ module TestHelper
75
94
  pid_file: "spec/echo_server.pid",
76
95
  log_file: "spec/echo_server.log",
77
96
  start_timeout: 30,
78
- stop_timeout: 30
97
+ stop_timeout: 30,
98
+ logger: new_logger
79
99
  }.merge(options)
80
- @controller = DaemonController.new(new_options)
100
+ @controller = DaemonController.new(**new_options)
81
101
  end
82
102
 
83
103
  def ping_echo_server
@@ -87,6 +107,10 @@ module TestHelper
87
107
  false
88
108
  end
89
109
 
110
+ def monotonic_time
111
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
112
+ end
113
+
90
114
  def write_file(filename, contents)
91
115
  File.write(filename, contents)
92
116
  end
@@ -105,8 +129,8 @@ module TestHelper
105
129
  end
106
130
 
107
131
  def eventually(deadline_duration = 1, check_interval = 0.05)
108
- deadline = Time.now + deadline_duration
109
- while Time.now < deadline
132
+ deadline = monotonic_time + deadline_duration
133
+ while monotonic_time < deadline
110
134
  if yield
111
135
  return
112
136
  else
@@ -115,6 +139,29 @@ module TestHelper
115
139
  end
116
140
  raise "Time limit exceeded"
117
141
  end
142
+
143
+ def wait_until_pid_file_available
144
+ eventually(30) do
145
+ @controller.send(:pid_file_available?)
146
+ end
147
+ end
148
+
149
+ def find_echo_server_pid
150
+ process_line = `ps aux`.lines.grep(/echo_server\.rb/).first
151
+ process_line.split[1].to_i if process_line
152
+ end
153
+
154
+ def kill_and_wait_echo_server
155
+ pid = find_echo_server_pid
156
+ if pid
157
+ Process.kill("SIGTERM", pid)
158
+ Timeout.timeout(5) do
159
+ while find_echo_server_pid
160
+ sleep(0.1)
161
+ end
162
+ end
163
+ end
164
+ end
118
165
  end
119
166
 
120
167
  # 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.1
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: 1980-01-02 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
@@ -47,7 +46,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
46
  - !ruby/object:Gem::Version
48
47
  version: '0'
49
48
  requirements: []
50
- rubygems_version: 3.6.2
49
+ rubygems_version: 3.6.9
51
50
  specification_version: 4
52
51
  summary: A library for implementing daemon management capabilities
53
52
  test_files: []
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