/
dpcollector.py
executable file
·126 lines (90 loc) · 3.17 KB
/
dpcollector.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#!/usr/bin/python
'''
Created on 01/10/2013
@author: csmanioto
'''
#third party libs
from threading import Thread
from daemon import runner
#from lib.setup.Load import *
from lib.collector.Collector import Collector
from lib.setup.Header import CONS_PRODUCT_NAME
from lib.setup.Load import pid_file_path, set_log, log, invoked_sleep_time, continuos_sleep_time
import time, sys
#fancy.logToFile(log_file)
#fancy.setLogLevel(log_level)
#log = fancy.getLogger("main")
handler = set_log()
''' My implementation of reload into runner.DaemonRunner '''
class my_DaemonRunner(runner.DaemonRunner):
# My Custom
def _reload(self):
self.app.reload()
runner.DaemonRunner.action_funcs = {
u'start': runner.DaemonRunner._start,
u'stop': runner.DaemonRunner._stop,
u'restart': runner.DaemonRunner._restart,
u'reload': _reload,
}
# Core Base
def call_invoked():
while True:
# if reload
log.debug("Thread Invoked Started")
collector = Collector()
collector.Invoked()
time.sleep(long(invoked_sleep_time)) # Load module - dpcollector.conf or CONSTANT
# Continuous Base
def call_continuous():
while True:
log.debug("Thread Continuous Started")
collector = Collector()
collector.Continuous()
time.sleep(long(continuos_sleep_time)) # Load module - dpcollector.conf or CONSTANT
# App's is name called by runner
class App():
def __init__(self):
self.stdin_path = '/dev/null'
self.stdout_path = '/dev/tty'
self.stderr_path = '/dev/tty'
self.pidfile_path = pid_file_path
self.pidfile_timeout = 5
def reload(self):
log.debug("Reload Signal - Thread by external call starting")
call_invoked()
def run(self):
log.info("-------------------------------------------")
log.info("%s successfully initialised " % CONS_PRODUCT_NAME)
log.info("-------------------------------------------")
th_invoked = Thread(target=call_invoked, args=())
th_collector = Thread(target=call_continuous, args=())
#th_collector.daemon = True
#th_collector.daemon = True
print "Starting Invoked Thread"
th_invoked.start()
print "Starting Continuous Thread"
th_collector.start()
#th_collector.join()
#th_invoked.join()
DEV = True
if not DEV:
try:
if sys.argv[1] == None or sys.argv[1] == "start" or sys.argv[1] == "stop" or sys.argv[1] == "restart" or sys.argv[1] == "reload":
pass
else:
print "Parameter '%s' is not valid: Valid only [start|stop|restart|reload]" % sys.argv[1]
sys.exit(1)
except:
print "%s: [start|stop|restart|reload]" % CONS_PRODUCT_NAME
sys.exit(1)
if sys.argv[1] == "stop":
log.info("-----------------------------------")
log.info("%s successfully shutdown " % CONS_PRODUCT_NAME)
log.info("-----------------------------------")
app = App()
if not DEV:
daemon_runner = my_DaemonRunner(app)
daemon_runner.daemon_context.files_preserve = [handler.stream]
daemon_runner.do_action()
else:
app.run()