-
Notifications
You must be signed in to change notification settings - Fork 0
/
deliverdaemon.py
executable file
·68 lines (58 loc) · 2.05 KB
/
deliverdaemon.py
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
import argparse
import os
from supay import Daemon
from updater import prepare, loop
curdir = None
def init_d():
global curdir
curdir = os.path.abspath(os.path.curdir)
return Daemon(name='deliver', catch_all_log=curdir, pid_dir=curdir)
def run():
daemon = init_d()
daemon.start()
# Undo some of the changes done by start, otherwise it won't work
os.chdir(curdir)
prepare()
loop()
def restart():
'''
Restart only if the process is not already running. To check if
the daemon is running part of the status functionality is
reimplemented.
Using start multiple times is dangerous, as it only checks the
existence of a pid file, which could be left hanging in case of a
crash.
'''
daemon = init_d()
try:
pidfile = open(daemon.pid)
pid = pidfile.readline()
os.kill(int(pid), 0)
except OSError:
if os.path.isfile(daemon.pid):
os.remove(daemon.pid)
run()
def stop():
daemon = init_d()
daemon.stop()
def status():
daemon = init_d()
daemon.status()
actions = {
'start' : run,
'stop' : stop,
'status' : status,
'restart' : restart
}
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Arguments for the deliver daemon')
group = parser.add_mutually_exclusive_group()
group.add_argument('--start', action='store_const', dest='action', const='start', help='Start the daemon [Default]')
group.add_argument('--stop', action='store_const', dest='action', const='stop', help='Stop the daemon')
group.add_argument('--status', action='store_const', dest='action', const='status',
help='Consult the status of the daemon')
group.add_argument('--restart', action='store_const', dest='action', const='restart',
help='Restart the daemon, but only if it is not already running')
# Execute the chosen action
action = parser.parse_args().action or 'start'
actions[action]()