-
Notifications
You must be signed in to change notification settings - Fork 0
/
server
executable file
·130 lines (121 loc) · 2.97 KB
/
server
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#!/usr/bin/env jruby
require 'rubygems'
require 'spoon'
# ### Review : Fork will not work under JRuby
# require 'daemons'
# Daemons.run('./Services/core.rb')
# require File.expand_path("./Services/core.rb",File.dirname(__FILE__))
# Spoon.spawn File.expand_path("./Services/core.rb",File.dirname(__FILE__)), *ARGV
# ### See : https://gist.github.com/ik5/448884
EXEC = File.expand_path("./Services/core.rb",File.dirname(__FILE__))
PID_PATH = File.expand_path("./System/Temporary/server.pid",File.dirname(__FILE__))
WORK_PATH = File.expand_path("./System/Temporary",File.dirname(__FILE__))
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 = nil
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}"
end
if pid
pid.to_i
else
pid
end
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 # "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
remove_pidfile
STDOUT.puts 'Stopped the process'
rescue Errno::ECHILD
remove_pidfile
STDOUT.puts 'Stopped the process'
#
rescue => e
STDERR.puts "unable to terminate process: (#{e.class}) #{e.message}"
end
end
def start
# if process_exists?
# STDERR.puts "The process #{EXEC} already running. Restarting the process"
# stop
# end
unless process_exists?
pid = Spoon.spawn EXEC, *ARGV
create_pid(pid)
# ### FIX : setsid not permitted EPERM error as www-data/etc
# Process.setsid
# at_exit do
# remove_pidfile
# end
Dir::chdir(WORK_PATH)
File::umask(0)
STDIN.reopen("/dev/null", 'r')
STDOUT.reopen("/dev/null", "w")
STDERR.reopen("/dev/null", "w")
end
end
if ARGV[0] == 'start'
start
elsif ARGV[0] == 'stop'
stop
elsif ARGV[0] == 'restart'
stop
start
elsif ARGV[0] == 'status'
if File.exist?(PID_PATH)
puts "running"
else
puts "stopped"
end
else
STDERR.puts "Usage: server <start|stop|restart | status>"
exit!
end