From efde03eb79ee531c89be29d7f25b21fcaafd3f37 Mon Sep 17 00:00:00 2001 From: Luka Vandervelden Date: Sat, 8 Jun 2019 02:50:38 +0200 Subject: [PATCH] Internal improvements related to PID files. --- src/main.cr | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/src/main.cr b/src/main.cr index 27b407b..5f32bcb 100644 --- a/src/main.cr +++ b/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]