Internal improvements related to PID files.
This commit is contained in:
parent
c77d931493
commit
efde03eb79
31
src/main.cr
31
src/main.cr
@ -202,7 +202,7 @@ class Service
|
||||
@reference.provides
|
||||
end
|
||||
|
||||
def start(log_dir : String)
|
||||
def start(pid_dir : String, log_dir : String)
|
||||
command, args = split_command command
|
||||
|
||||
process = Process.fork do
|
||||
@ -216,14 +216,15 @@ class Service
|
||||
Process.exec command, args, chdir: @reference.directory
|
||||
end
|
||||
|
||||
self.pid = process.pid
|
||||
self.save_pid pid_dir, process.pid
|
||||
end
|
||||
|
||||
# TODO:
|
||||
# - Custom shutdown commands.
|
||||
# - Should we wait for the process to die?
|
||||
def stop
|
||||
_pid = pid
|
||||
# - Shouldn’t we remove the pid file?
|
||||
def stop(pid_dir : String)
|
||||
_pid = pid pid_dir
|
||||
|
||||
if _pid
|
||||
command = stop_command
|
||||
@ -239,18 +240,18 @@ class Service
|
||||
end
|
||||
end
|
||||
|
||||
def get_pid_file
|
||||
"#{name}.pid"
|
||||
def get_pid_file(pid_dir)
|
||||
"#{pid_dir}/#{name}.pid"
|
||||
end
|
||||
|
||||
def pid
|
||||
File.read(get_pid_file).to_i
|
||||
def pid(pid_dir)
|
||||
File.read(get_pid_file pid_dir).to_i
|
||||
rescue e # pid file missing, corrupted or otherwise not readable
|
||||
nil
|
||||
end
|
||||
|
||||
def pid=(new_pid)
|
||||
File.write get_pid_file, new_pid
|
||||
def save_pid(pid_dir, new_pid)
|
||||
File.write get_pid_file(pid_dir), new_pid
|
||||
end
|
||||
|
||||
enum Status
|
||||
@ -259,8 +260,8 @@ class Service
|
||||
Stopped
|
||||
end
|
||||
|
||||
def status
|
||||
_pid = pid
|
||||
def status(pid_dir)
|
||||
_pid = pid pid_dir
|
||||
|
||||
if _pid
|
||||
if Process.exists? _pid
|
||||
@ -370,11 +371,11 @@ begin
|
||||
elsif args[0] == "del"
|
||||
Service.new(args[1], args[2]?).remove "rc"
|
||||
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"
|
||||
Service.new(args[1], args[2]?).stop
|
||||
Service.new(args[1], args[2]?).stop "pid"
|
||||
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"
|
||||
service = Service.all.find do |service|
|
||||
unless service.name == args[1]
|
||||
|
Loading…
Reference in New Issue
Block a user