Skip to content

Instantly share code, notes, and snippets.

@edubkendo
Forked from nandosola/daemon.rb
Created May 16, 2014 19:25
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save edubkendo/8216f7655d8d4ef8a1ac to your computer and use it in GitHub Desktop.
Save edubkendo/8216f7655d8d4ef8a1ac to your computer and use it in GitHub Desktop.
#!/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
# Upstart script for FileServer
# Copy to /etc/init/ (root:root, 644)
# usage : (start|stop) trantor-fileserver
# logfile : /var/log/upstart/trantor-fileserver.log
#
description "FileServer web app: Puma+Sinatra+Rack"
author "Nando Sola<nando@abstra.cc>"
start on started runsvdir
stop on runlevel [!5]
respawn
pre-start script
rm -rf /var/trantor/tmp/*
end script
script
. /etc/profile.d/puma.sh
APP_NAME="trantor-fileserver"
APP_ROOT="/apps/$APP_NAME/current"
LISTEN="tcp://127.0.0.1:9090"
THREAD_POOL="1:16"
TRANTOR_FILESERVER_CONFIG_DIR="/apps/$APP_NAME/shared/config"
TMPDIR="/var/trantor/tmp"
JAVA_OPTS=$JAVA_OPTS
export TRANTOR_FILESERVER_CONFIG_DIR TMPDIR JAVA_OPTS
EXEC_CMD="puma -b $LISTEN -t $THREAD_POOL"
sudo -u puma -s -- <<EOF
cd $APP_ROOT
$JRUBY_HOME/bin/jruby -S bundle exec puma -b $LISTEN -t $THREAD_POOL
EOF
end script
#post-stop script
#end script
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment