-
Notifications
You must be signed in to change notification settings - Fork 0
/
o2m.py
executable file
·91 lines (75 loc) · 2.61 KB
/
o2m.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
#!/usr/bin/env python
# # -*- coding: utf-8 -*-
import os
import sys
import time
import unittest
from JDLibs.JDDaemon import JDDaemon as Daemon
from JDLibs.JDKafkaConsumer import Consumer as Consumer
class JDKFKCDaemon(Daemon):
def __init__(self, *args, **kwargs):
super(JDKFKCDaemon, self).__init__(*args, **kwargs)
def run(self, force=False):
while True:
com = Consumer(verbose=2, forceRestart=force)
com.run()
com.close()
force = False # force init only for ONE FUCKING TIME
time.sleep(0.3)
sys.stdout.flush()
def stop(self):
super(JDKFKCDaemon, self).stop()
def status(self):
pid = self.get_pid()
if pid is None:
self.log('Process is stopped')
return False
elif os.path.exists('JDKFKCDaemon.pid'):
self.log('Process (pid %d) is running...' % pid)
return True
else:
self.log('Process (pid %d) is killed' % pid)
return False
class TDaemon(Daemon):
def __init__(self, *args, **kwargs):
super(TDaemon, self).__init__(*args, **kwargs)
testoutput = open('testing_daemon', 'a')
testoutput.write('inited')
testoutput.close()
def run(self):
cnt = 0
while True:
cnt = cnt + 1
testoutput = open('testing_daemon', 'a')
testoutput.write('%s\n' % cnt)
testoutput.close()
time.sleep(0.3)
def status(self):
pid = self.get_pid()
if pid is None:
self.log('Process is stopped')
return False
elif os.path.exists('testing_daemon.pid'):
self.log('Process (pid %d) is running...' % pid)
return True
else:
self.log('Process (pid %d) is killed' % pid)
return False
def main():
if len(sys.argv) == 1:
print('Usage: python filename.py [option]\n'
'\t\033[0;32;40m start \033[0m\t\tstart daemon\n'
'\t\033[0;31;40m stop \033[0m\t\tstop daemon\n'
'\t\033[0;36;40m restart \033[0m\trestart\n'
'\t\033[0;33;40m status \033[0m\tcheck status\n'
'\t\033[0;34;40m forceInit \033[0m\tredo queue')
elif len(sys.argv) == 2:
arg = sys.argv[1]
if arg in ('start', 'stop', 'restart', 'status'):
d = JDKFKCDaemon('JDKFKCDaemon.pid', verbose=2)
getattr(d, arg)()
elif arg == 'forceInit':
d = JDKFKCDaemon('JDKFKCDaemon.pid', verbose=1)
d.start(force=True)
if __name__ == '__main__':
main()