#!/usr/bin/env jruby
require 'rubygems'
require 'spoon'
APPNAME = 'fileserver'
WORK_PATH = Dir.pwd
EXEC = "#{ENV['HOME']}/.rbenv/shims/puma"
PID_PATH = "#{WORK_PATH}/log/#{APPNAME}.pid"
OUT_PATH = "#{WORK_PATH}/log/#{APPNAME}.out.log"
def create_pid(pid)
begin
open(PID_PATH, 'w') do |f|
f.puts pid
end
rescue => e
STDERR.puts "Error: Unable to open #{PID_PATH} for writing:\n\t" +
"(#{e.class}) #{e.message}"
exit!
end
end
def get_pid
pid = false
begin
open(PID_PATH, 'r') do |f|
pid = f.readline
pid = pid.to_s.gsub(/[^0-9]/,'')
end
rescue => e
STDERR.puts "Error: Unable to open #{PID_PATH} for reading:\n\t" +
"(#{e.class}) #{e.message}"
exit
end
pid.to_i
end
def remove_pidfile
begin
File.unlink(PID_PATH)
rescue => e
STDERR.puts "ERROR: Unable to unlink #{path}:\n\t" +
"(#{e.class}) #{e.message}"
exit
end
end
def process_exists?
begin
pid = get_pid
return false unless pid
Process.kill(0, pid)
true
rescue Errno::ESRCH, TypeError
STDERR.puts "PID #{pid} is NOT running or is zombied!";
false
rescue Errno::EPERM
STDERR.puts "No permission to query #{pid}!";
rescue => e
STDERR.puts "(#{e.class}) #{e.message}:\n\t" +
"Unable to determine status for #{pid}."
false
end
end
def stop
begin
pid = get_pid
STDERR.puts "pid : #{pid}"
while true do
Process.kill("TERM", pid)
Process.wait(pid)
sleep(0.1)
end
puts "here"
rescue Errno::ESRCH, Errno::ECHILD # no more process to kill
remove_pidfile
STDOUT.puts 'Stopped the process'
rescue => e
STDERR.puts "unable to terminate process: (#{e.class}) #{e.message}"
end
end
def restart
if process_exists?
STDERR.puts "The process #{EXEC} already running. Restarting the process"
stop
end
start
end
def start
Dir::chdir(WORK_PATH)
file_actions = Spoon::FileActions.new
file_actions.close(1)
file_actions.open(1, OUT_PATH, File::WRONLY | File::TRUNC | File::CREAT, 0644)
spawn_attr = Spoon::SpawnAttributes.new
pid = Spoon.posix_spawn EXEC, file_actions, spawn_attr, %w(puma -b tcp://127.0.0.1:9090)
create_pid(pid)
Process.setsid
end
if ARGV[0] == 'start'
start
elsif ARGV[0] == 'stop'
stop
elsif ARGV[0] == 'restart'
stop
start
else
STDERR.puts "Usage: daemon.rb <start|stop|restart>"
exit!
end