Internal improvements related to PID files.

master
Luka Vandervelden 2019-06-08 02:50:38 +02:00
parent c77d931493
commit efde03eb79
1 changed files with 16 additions and 15 deletions

View File

@ -202,7 +202,7 @@ class Service
@reference.provides @reference.provides
end end
def start(log_dir : String) def start(pid_dir : String, log_dir : String)
command, args = split_command command command, args = split_command command
process = Process.fork do process = Process.fork do
@ -216,14 +216,15 @@ class Service
Process.exec command, args, chdir: @reference.directory Process.exec command, args, chdir: @reference.directory
end end
self.pid = process.pid self.save_pid pid_dir, process.pid
end end
# TODO: # TODO:
# - Custom shutdown commands. # - Custom shutdown commands.
# - Should we wait for the process to die? # - Should we wait for the process to die?
def stop # - Shouldnt we remove the pid file?
_pid = pid def stop(pid_dir : String)
_pid = pid pid_dir
if _pid if _pid
command = stop_command command = stop_command
@ -239,18 +240,18 @@ class Service
end end
end end
def get_pid_file def get_pid_file(pid_dir)
"#{name}.pid" "#{pid_dir}/#{name}.pid"
end end
def pid def pid(pid_dir)
File.read(get_pid_file).to_i File.read(get_pid_file pid_dir).to_i
rescue e # pid file missing, corrupted or otherwise not readable rescue e # pid file missing, corrupted or otherwise not readable
nil nil
end end
def pid=(new_pid) def save_pid(pid_dir, new_pid)
File.write get_pid_file, new_pid File.write get_pid_file(pid_dir), new_pid
end end
enum Status enum Status
@ -259,8 +260,8 @@ class Service
Stopped Stopped
end end
def status def status(pid_dir)
_pid = pid _pid = pid pid_dir
if _pid if _pid
if Process.exists? _pid if Process.exists? _pid
@ -370,11 +371,11 @@ begin
elsif args[0] == "del" elsif args[0] == "del"
Service.new(args[1], args[2]?).remove "rc" Service.new(args[1], args[2]?).remove "rc"
elsif args[0] == "start" elsif args[0] == "start"
Service.new(args[1], args[2]?).start "log" Service.new(args[1], args[2]?).start "pid", "log"
elsif args[0] == "stop" elsif args[0] == "stop"
Service.new(args[1], args[2]?).stop Service.new(args[1], args[2]?).stop "pid"
elsif args[0] == "status" elsif args[0] == "status"
puts Service.new(args[1], args[2]?).status puts Service.new(args[1], args[2]?).status "pid"
elsif args[0] == "show" elsif args[0] == "show"
service = Service.all.find do |service| service = Service.all.find do |service|
unless service.name == args[1] unless service.name == args[1]