Skip to content

Commit 5459abb

Browse files
CamJNFooBarWidget
andauthored
General code readability improvements (#20)
Signed-off-by: Hongli Lai <hongli@hongli.nl> Co-authored-by: Hongli Lai <hongli@hongli.nl>
1 parent 9b4285f commit 5459abb

1 file changed

Lines changed: 45 additions & 62 deletions

File tree

lib/daemon_controller.rb

Lines changed: 45 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ def initialize(identifier:, start_command:, ping_command:, pid_file:, log_file:,
7474
lock_file: nil, stop_command: nil, restart_command: nil, before_start: nil,
7575
start_timeout: 30, start_abort_timeout: 10, stop_timeout: 30,
7676
log_file_activity_timeout: 10, ping_interval: 0.1, stop_graceful_signal: "TERM", dont_stop_if_pid_file_invalid: false,
77-
daemonize_for_me: false, keep_ios: nil, env: nil, logger: nil)
77+
daemonize_for_me: false, keep_ios: nil, env: {}, logger: nil)
7878
@identifier = identifier
7979
@start_command = start_command
8080
@ping_command = ping_command
@@ -140,7 +140,7 @@ def connect
140140
end
141141
if connection.nil?
142142
@lock_file.exclusive_lock do
143-
if !daemon_is_running?
143+
unless daemon_is_running?
144144
start_without_locking
145145
end
146146
connect_exception = nil
@@ -335,9 +335,7 @@ def spawn_daemon
335335

336336
def kill_daemon
337337
if @stop_command
338-
if @dont_stop_if_pid_file_invalid && read_pid_file.nil?
339-
return
340-
end
338+
return if @dont_stop_if_pid_file_invalid && read_pid_file.nil?
341339

342340
result = run_command(@stop_command)
343341
case result
@@ -356,8 +354,7 @@ def kill_daemon
356354
end
357355

358356
def kill_daemon_with_signal(force: false)
359-
pid = read_pid_file
360-
if pid
357+
if (pid = read_pid_file)
361358
if force
362359
Process.kill("SIGKILL", pid)
363360
else
@@ -380,14 +377,11 @@ def daemon_is_running?
380377
end
381378

382379
def read_pid_file
383-
begin
384-
pid = File.read(@pid_file).strip
385-
rescue Errno::ENOENT
386-
return nil
387-
end
380+
pid = File.read(@pid_file).strip
388381
if /\A\d+\Z/.match?(pid)
389382
pid.to_i
390383
end
384+
rescue Errno::ENOENT
391385
end
392386

393387
def delete_pid_file
@@ -438,7 +432,7 @@ def no_activity?(seconds)
438432
end
439433

440434
def pid_file_available?
441-
File.exist?(@pid_file) && File.stat(@pid_file).size != 0
435+
File.exist?(@pid_file) && !File.zero?(@pid_file)
442436
end
443437

444438
# This method does nothing and only serves as a hook for the unit test.
@@ -582,10 +576,10 @@ def run_command(command)
582576
end
583577

584578
pid = if @daemonize_for_me
585-
Process.spawn(@env || {}, ruby_interpreter, SPAWNER_FILE,
579+
Process.spawn(@env, ruby_interpreter, SPAWNER_FILE,
586580
command, spawn_options)
587581
else
588-
Process.spawn(@env || {}, command, spawn_options)
582+
Process.spawn(@env, command, spawn_options)
589583
end
590584

591585
# run_command might be running in a timeout block (like
@@ -599,9 +593,9 @@ def run_command(command)
599593
# it started successfully; if it didn't we'll know
600594
# that later by checking the PID file and by pinging
601595
# it.
602-
return InternalCommandOkResult.new(pid, tempfile_path ? File.read(tempfile_path).strip : nil)
596+
return InternalCommandOkResult.new(pid, tempfile_path && File.read(tempfile_path).strip)
603597
rescue Timeout::Error
604-
return InternalCommandTimeoutResult.new(pid, tempfile_path ? File.read(tempfile_path).strip : nil)
598+
return InternalCommandTimeoutResult.new(pid, tempfile_path && File.read(tempfile_path).strip)
605599
end
606600

607601
child_status = $?
@@ -690,43 +684,7 @@ def run_ping_command
690684
end
691685
end
692686

693-
if !can_ping_unix_sockets?
694-
require "java"
695-
696-
def ping_socket(host_name, port)
697-
channel = java.nio.channels.SocketChannel.open
698-
begin
699-
address = java.net.InetSocketAddress.new(host_name, port)
700-
channel.configure_blocking(false)
701-
if channel.connect(address)
702-
return true
703-
end
704-
705-
deadline = Time.now.to_f + 0.1
706-
while true
707-
begin
708-
if channel.finish_connect
709-
return true
710-
end
711-
rescue java.net.ConnectException => e
712-
if /Connection refused/i.match?(e.message)
713-
return false
714-
else
715-
throw e
716-
end
717-
end
718-
719-
# Not done connecting and no error.
720-
sleep 0.01
721-
if Time.now.to_f >= deadline
722-
return false
723-
end
724-
end
725-
ensure
726-
channel.close
727-
end
728-
end
729-
else
687+
if can_ping_unix_sockets?
730688
def ping_socket(socket_domain, sockaddr)
731689
socket = Socket.new(socket_domain, Socket::Constants::SOCK_STREAM, 0)
732690
begin
@@ -759,18 +717,43 @@ def ping_tcp_socket(sockaddr)
759717
rescue Errno::EAFNOSUPPORT
760718
ping_socket(Socket::Constants::AF_INET6, sockaddr)
761719
end
720+
else
721+
require "java"
722+
723+
def ping_socket(host_name, port)
724+
channel = java.nio.channels.SocketChannel.open
725+
begin
726+
address = java.net.InetSocketAddress.new(host_name, port)
727+
channel.configure_blocking(false)
728+
return true if channel.connect(address)
729+
730+
deadline = Time.now.to_f + 0.1
731+
loop do
732+
begin
733+
return true if channel.finish_connect
734+
rescue java.net.ConnectException => e
735+
if /Connection refused/i.match?(e.message)
736+
return false
737+
else
738+
throw e
739+
end
740+
end
741+
742+
# Not done connecting and no error.
743+
sleep 0.01
744+
return false if Time.now.to_f >= deadline
745+
end
746+
ensure
747+
channel.close
748+
end
749+
end
762750
end
763751

764752
def ruby_interpreter
765-
rb_config = if defined?(RbConfig)
766-
RbConfig::CONFIG
767-
else
768-
Config::CONFIG
769-
end
770753
File.join(
771-
rb_config["bindir"],
772-
rb_config["RUBY_INSTALL_NAME"]
773-
) + rb_config["EXEEXT"]
754+
RbConfig::CONFIG["bindir"],
755+
RbConfig::CONFIG.values_at("RUBY_INSTALL_NAME", "EXEEXT").join
756+
)
774757
end
775758

776759
def timeoutable(amount, &block)

0 commit comments

Comments
 (0)