-
Notifications
You must be signed in to change notification settings - Fork 0
/
daemon_test.py
executable file
·106 lines (89 loc) · 2.62 KB
/
daemon_test.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
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
#!/home/evan/anaconda3/bin/python
from daemon import DaemonContext
import os
import time
import sys
import argparse
import fnmatch
import signal
import subprocess
PID_DIR = '/home/evan/daemon_pids'
class TestApp(object):
def __init__(self):
pass
def run(self):
self.pid = str(os.getpid())
with open(os.path.join(PID_DIR, str(self.pid) + '.pid'), 'w') as pidfile:
pidfile.write(self.pid)
while True:
print('Running process ID: {0}'.format(self.pid))
time.sleep(3)
class DaemonRunner(object):
def __init__(self, app, outpath=None):
self.daemon_context = DaemonContext()
self.app = app
if outpath:
self.daemon_context.stdout = open(outpath, 'a')
def start(self):
self.daemon_context.open()
self.app.run()
status()
def pid_exists(pid):
pid = int(pid)
try:
os.kill(pid, 0)
except:
return False
return True
def main():
parser = argparse.ArgumentParser()
parser.add_argument('cmd', choices=['start', 'stop', 'status'])
parser.add_argument('-n', type=int, dest='num_daemons', default=1)
args = parser.parse_args()
if args.cmd == 'start':
start(args.num_daemons)
if args.cmd == 'stop':
stop()
if args.cmd == 'status':
status()
def start(num_daemons=1):
print('Starting {0} daemons'.format(num_daemons))
for i in range(num_daemons):
subprocess.call('/home/evan/code/daemon_starter.py')
while count_active() != num_daemons:
time.sleep(0.1)
status()
def stop():
pid_files = get_pid_files()
if len(pid_files) == 0:
print('No running daemons')
return
for pidfile in pid_files:
pid = pidfile.split('.')[0]
if pid_exists(pid):
os.kill(int(pid), signal.SIGTERM)
print('Process terminated: ' + str(pid))
else:
print('Cleaning up stale process: ' + str(pid))
os.remove(os.path.join(PID_DIR, pidfile))
def count_active():
active = 0
for pid in get_pid_files():
if pid_exists(pid.split('.')[0]):
active = active + 1
return active
def status():
pid_files = get_pid_files()
if len(pid_files) == 0:
print('No running daemons')
return
for pidfile in pid_files:
pid = pidfile.split('.')[0]
if pid_exists(pid):
print('Running : ' + str(pid))
else:
print('Stale process: ' + str(pid))
def get_pid_files():
return [f for f in filter(lambda x: fnmatch.fnmatch(x, '*.pid'), os.listdir(PID_DIR))]
if __name__ == '__main__':
main()