-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_event.py
More file actions
208 lines (191 loc) · 6.59 KB
/
task_event.py
File metadata and controls
208 lines (191 loc) · 6.59 KB
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# **************************************************************************** #
# #
# ::: :::::::: #
# task_event.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: skhatir <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/06/30 10:05:38 by skhatir #+# #+# #
# Updated: 2016/06/30 10:05:40 by skhatir ### ########.fr #
# #
# **************************************************************************** #
import sys
import task_lib
from cmd_event import cmd_event
class task_event:
def __init__(self): ####
self.cmd = {}
try:
data = task_lib.load_conf(sys.argv[1])
cmd = data.get("programs")
for k, v in cmd.iteritems():
i = 1
cmd_class = cmd_event(k, v)
if (cmd_class.numprocs > 0):
self.cmd[k] = cmd_class
while (i < cmd_class.numprocs):
name = k + ":0" + str(i)
self.cmd[name] = task_lib.dup(self.cmd[k])
self.cmd[name].id = name
self.cmd[name].parent = self.cmd[k]
i += 1
except Exception, e:
task_lib.log.warning("ERROR loading yaml's data -> check yourfile.yaml")
def autostart(self):
for k, v in self.cmd.iteritems():
cmd = self.cmd[k]
if (cmd.autostart):
cmd.start(True)
def start(self, line):
find = False
for k, v in self.cmd.iteritems():
cmd = self.cmd[k]
if (line and (line == cmd.id or line == "all")):
find = True
if (cmd.status == "WAITING" or cmd.status == "FAILED" or cmd.status == "STOPPED"):
cmd.start(False);
cmd.show_status()
elif (cmd.status == "RUNNING" or cmd.status == "STARTING" or cmd.status == "STOPPING"): ####
print (cmd.id + " is already actived or stopping")
task_lib.log.warning(cmd.id + ': start failed: status:' + cmd.status)
else:
print ("FATAL_ERROR: " + cmd.id)
task_lib.log.warning(cmd.id + ': start failed: status:' + cmd.status)
if (line != "all"):
break
if (find == False):
print ("task: no process found " + line)
task_lib.log.info("start: no process found " + line)
def restart(self, line):
if (str(line) == "all"):
for k, v in self.cmd.iteritems():
cmd = self.cmd[k]
if (cmd.process != None): ####
cmd.restart()
cmd.show_status()
else:
curr = task_lib.check_process(self.cmd, line)
if (curr != None):
curr.restart()
curr.show_status()
else:
print ("task: no process running found -> check status" + line)
task_lib.log.info("restart: no process running found -> check status" + line)
def stop(self, line):
if (str(line) == "all"):
for k, v in self.cmd.iteritems():
cmd = self.cmd[k]
if (cmd.process != None): ####
cmd.stop()
cmd.show_status()
cmd.time = 0
else:
curr = task_lib.check_process(self.cmd, line)
if (curr != None):
curr.stop()
curr.show_status()
curr.time = 0
else:
print ("task: no process running found -> check status" + line)
task_lib.log.info("task: no process running found -> check status" + line)
def info(self, line):
if (line):
for k, v in self.cmd.iteritems():
cmd = self.cmd[k]
if (cmd.id == str(line)):
task_lib.print_all_info(cmd, line)
task_lib.log.info(cmd.id + ': info has been printed: status:' + cmd.status)
break
else:
print ("info requiere a target")
task_lib.log.info("info requiere a target")
def status(self, line):
find = False
if (line):
for k, v in self.cmd.iteritems():
cmd = self.cmd[k]
if (cmd.id == str(line)):
find = True
cmd.show_status()
break
else:
find = True
for k, v in self.cmd.iteritems():
cmd = self.cmd[k]
cmd.show_status()
if (find == False):
print ("task: no process found " + line)
task_lib.log.info("task: no process found " + line)
def only(self, line):
find = False
for k, v in self.cmd.iteritems():
cmd = self.cmd[k]
if (cmd.status == line):
find = True
cmd.show_status()
if (find == False):
print ("No matched status")
def reload(self):
task_lib.close_fd(self.cmd)
new_task = task_event()
for k, v in self.cmd.iteritems():
i = 0;
cmd = self.cmd[k]
for l, w in new_task.cmd.iteritems():
cmd_comp = new_task.cmd[l]
if (cmd.id == cmd_comp.id):
i = 1
if (cmd.path == cmd_comp.path and cmd.stdout == cmd_comp.stdout and cmd.stderr == cmd_comp.stderr and cmd.workingdir == cmd_comp.workingdir):
if (cmd.status == "RUNNING" or cmd.status == "STARTING"):
new_task.cmd[l].process = cmd.process
new_task.cmd[l].status = cmd.status
new_task.cmd[l].state = cmd.state
new_task.cmd[l].time = cmd.time
new_task.cmd[l].start_timer = cmd.start_timer
new_task.cmd[l].stop_timer = cmd.stop_timer
new_task.cmd[l].start_fail = new_task.cmd[l].start_fail
elif (cmd.status == "WAITING" or cmd.status == "STOPPED"):
if (cmd_comp.autostart == True):
cmd_comp.start(True)
break
elif (cmd.process):
cmd.stop_signal = 9
cmd.stop()
cmd_comp.start(True)
if (i == 0 and cmd.process):
cmd.stop_signal = 9
cmd.stop()
for a, b in new_task.cmd.iteritems():
cmd_comp = new_task.cmd[a]
if (cmd_comp.autostart == True and cmd_comp.status == "WAITING" and cmd_comp.process == None):
cmd_comp.start(True)
self.cmd = new_task.cmd
self.status(None)
task_lib.log.info('Deamon_Master: has been reloaded correctly')
def check_status(self):
for k, v in self.cmd.iteritems():
cmd = self.cmd[k]
if (cmd.process and cmd.process.poll() != None):
sig_num = cmd.process.returncode
if (sig_num < 0):
sig_num *= -1
cmd.finish(sig_num);
elif (cmd.process and cmd.stop_timer >= 0):
if (cmd.stop_timer >= cmd.stoptime):
try:
cmd.process.send_signal(9);
cmd.finish(9);
except OSError, e:
task_lib.log.warning(cmd.id + ': stoptime')
task_lib.log.warning(cmd.id + ': has been kill due to config: stoptime')
else:
cmd.stop_timer += 1
elif (cmd.process and cmd.start_timer >=0):
if (cmd.start_timer >= cmd.starttime):
cmd.start_timer = -1
cmd.status = "RUNNING"
task_lib.log.info(cmd.id + ': has been started: status:' + cmd.status)
else:
cmd.start_timer += 1
elif (cmd.process == None and cmd.status == "RUNNING"):
cmd.finish(0);