-
Notifications
You must be signed in to change notification settings - Fork 6
/
daemon.lua
220 lines (179 loc) · 4.83 KB
/
daemon.lua
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
--[==[
daemon apps
Written by Cosmin Apreutesei. Public Domain.
daemon(cmdline_args...) -> app
cmd_server cmdline section for server control
exit(app:run(...)) run the daemon app with cmdline args
deploy app deployment name.
machine app machine name.
env app environment ('dev').
log_host log server host.
log_port log server port.
]==]
require'glue'
require'proc'
require'fs'
require'cmdline'
--daemonize (Linux only) -----------------------------------------------------
local pidfile
local run_server
if Linux then
cmd_server = cmdsection'SERVER CONTROL'
local function findpid(pid, cmd)
local s = try_load(_('/proc/%s/cmdline', pid), false, true)
return s and s:find(cmd, 1, true) and true or false
end
local function running()
local pid = tonumber((load(pidfile, false)))
if not pid then return false end
return findpid(pid, arg[0]), pid
end
cmd_server('running', 'Check if the server is running', function()
return running() and 0 or 1
end)
cmd_server('pid', 'Get server PID', function()
local is_running, pid = running()
if is_running then say(pid) end
return is_running and 0 or 1
end)
cmd_server('status', 'Show server status', function()
local is_running, pid = running()
if is_running then
say('Running. PID: %d', pid)
else
say 'Not running.'
rm(pidfile)
end
end)
cmd_server('run', 'Run server in foreground', function()
run_server()
end)
cmd_server('start', 'Start the server', function()
local is_running, pid = running()
if is_running then
say('Already running. PID: %d', pid)
return 1
elseif pid then
rm(pidfile)
say'Stale pid file found and removed.'
end
--step 1..11.
local pid = daemonize()
--12. save pid to pidfile.
save(pidfile, tostring(pid))
local ok = pcall(run_server)
logging:tofile_stop()
rm(pidfile)
exit(ok and 0 or 1)
end)
cmd_server('stop', 'Stop the server', function()
local is_running, pid = running()
if not is_running then
say'Not running.'
return 0
end
sayn('Killing PID %d...', pid)
local p = C.kill(pid, 9)
for i=1,10 do
if not running() then
say'OK.'
rm(pidfile)
return 0
end
sayn'.'
wait(.1)
end
say'Failed.'
return 1
end)
cmd_server('restart', 'Restart the server', function()
if cmd_server.stop.fn() == 0 then
cmd_server.start.fn()
cmd_server.status.fn()
end
end)
cmd_server('tail', 'tail -f the log file', function()
local p = exec(_('tail -f %s', logfile))
p:wait()
p:forget()
end)
else --Linux
cmd('run', 'Run server in foreground', function()
run_server()
end)
end
--init -----------------------------------------------------------------------
function daemon(...)
local app = {before = before, after = after}
local cmd_action, cmd_opt, cmd_args, cmd_run = cmdaction(...) --process cmdline options.
randomseed(clock()) --mainly for resolver.
local conffile
--non-configurable, convention-based things.
pidfile = indir(scriptdir(), scriptname..'.pid')
logfile = indir(scriptdir(), scriptname..'.log')
conffile = indir(scriptdir(), scriptname..'.conf')
--consider this module loaded so that other app submodules that
--require it at runtime don't try to load it again.
package.loaded[scriptname] = app
--make require() see Lua modules from the script dir.
luapath(scriptdir())
sopath(indir(scriptdir(), 'bin', win and 'windows' or 'linux'))
--cd to scriptdir so that we can use relative paths for everything if we want to.
chdir(scriptdir())
function chdir(dir)
error'chdir() not allowed'
end
--load an optional config file.
load_config_file(conffile)
--set up logging.
logging.deploy = config'deploy'
logging.machine = config'machine'
logging.env = config'env'
function run_server() --fw. declared.
server_running = true
env('TZ', ':/etc/localtime')
--^^avoid having os.date() stat /etc/localtime.
logging:tofile(logfile)
logging.autoflush = logging.debug
local logtoserver = config'log_host' and config'log_port'
if logtoserver then
require'sock'
local start_heartbeat, stop_heartbeat do
local stop, sleeper
function start_heartbeat()
resume(thread(function()
sleeper = wait_job()
while not stop do
logging.logvar('live', time())
sleeper:wait(1)
end
end, 'logging-heartbeat'))
end
function stop_heartbeat()
stop = true
if sleeper then
sleeper:resume()
end
end
end
logging:toserver(config'log_host', config'log_port')
start_heartbeat()
app:run_server()
stop_heartbeat()
logging:toserver_stop()
else
app:run_server()
end
logging:tofile_stop()
end
function app:run_cmd(cmd_action, cmd_run, cmd_opt, ...) --stub
return cmd_run(cmd_action, cmd_opt, ...)
end
function app:run()
if cmd_action == scriptname then --caller module loaded with require()
return app
end
return self:run_cmd(cmd_action, cmd_run, cmd_opt, unpack(cmd_args))
end
return app
end