-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.py
executable file
·114 lines (82 loc) · 2.9 KB
/
main.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import threading
import traceback
import signal
import daemon
import atexit
import os
import time
from models.initialize import logger, thread_status, config
from models.event_process import EventProcess
from models.event_loop import vir_event_loop_poll_register, vir_event_loop_poll_run
from models import Host
from models import Utils
from models import PidFile
__author__ = 'James Iter'
__date__ = '2017/3/12'
__contact__ = '[email protected]'
__copyright__ = '(c) 2017 by James Iter.'
def main():
pidfile = PidFile(file_name=config['pidfile'])
pidfile.create(pid=os.getpid())
atexit.register(pidfile.unlink)
threads = []
signal.signal(signal.SIGTERM, Utils.signal_handle)
signal.signal(signal.SIGINT, Utils.signal_handle)
Host.init_guestfish()
guest_creating_progress_report_engine_engine = Host()
t_ = threading.Thread(
target=guest_creating_progress_report_engine_engine.guest_creating_progress_report_engine, args=())
threads.append(t_)
host_use_for_guest_operate_engine = Host()
t_ = threading.Thread(target=host_use_for_guest_operate_engine.guest_operate_engine, args=())
threads.append(t_)
host_use_for_host_state_report_engine = Host()
t_ = threading.Thread(target=host_use_for_host_state_report_engine.state_report_engine, args=())
threads.append(t_)
host_use_for_collection_performance_process_engine = Host()
t_ = threading.Thread(
target=host_use_for_collection_performance_process_engine.collection_performance_process_engine,
args=())
threads.append(t_)
host_use_for_host_collection_performance_process_engine = Host()
t_ = threading.Thread(
target=
host_use_for_host_collection_performance_process_engine.host_collection_performance_process_engine,
args=())
threads.append(t_)
vir_event_loop_poll_register()
t_ = threading.Thread(target=vir_event_loop_poll_run, name="libvirtEventLoop")
threads.append(t_)
host_use_for_refresh_guest_state_engine = Host()
host_use_for_refresh_guest_state_engine.refresh_guest_state()
for t in threads:
t.setDaemon(True)
t.start()
EventProcess.guest_event_register()
while True:
if Utils.exit_flag:
# 主线程即将结束
EventProcess.guest_event_deregister()
break
if config['DEBUG']:
print thread_status
time.sleep(1)
# 等待子线程结束
for t in threads:
t.join()
msg = 'Main say bye-bye!'
print msg
logger.info(msg=msg)
if __name__ == '__main__':
# noinspection PyBroadException
try:
if config['daemon']:
with daemon.DaemonContext(files_preserve=[logger.handlers[0].stream.fileno()]):
main()
else:
main()
except:
logger.error(traceback.format_exc())
exit(-1)