-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask_lib.py
More file actions
143 lines (130 loc) · 4.83 KB
/
task_lib.py
File metadata and controls
143 lines (130 loc) · 4.83 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
# **************************************************************************** #
# #
# ::: :::::::: #
# task_lib.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: skhatir <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/06/30 10:05:49 by skhatir #+# #+# #
# Updated: 2016/06/30 10:05:50 by skhatir ### ########.fr #
# #
# **************************************************************************** #
import os
import copy
import yaml
import time
import logging
from signaux import siglib
from task_event import task_event
dup = copy.deepcopy
log = logging
log.basicConfig(filename = '/tmp/logger.task', level=logging.DEBUG)
def color_string(line, s):
color_define = {
"BOLD": "\033[1m" + s + "\033[0m" + "\033[39m",
"RED": "\033[31m" + s + "\033[39m" + " ",
"GREEN": "\033[32m" + s + "\033[39m",
"YELLOW": "\033[33m" + s + "\033[39m",
"BLUE": "\033[34m" + s + "\033[39m",
"MAGENTA": "\033[35m" + s + "\033[39m",
"CYAN": "\033[36m" + s + "\033[39m",
"BACK_YEL" : "\033[43m" + "\033[1m" "\033[30m" + s + "\033[0m" + "\033[39m" + "\t ",
"BACK_RED": "\033[41m" + "\033[1m" + s + "\033[0m" + "\033[39m" + "\t ",
"DEFAULT" : "\033[0m" + "\033[39m" + s,
}
return str(color_define[line])
def format_statut(line):
color_match = {
"WAITING": "DEFAULT",
"STARTING": "CYAN",
"RUNNING": "GREEN",
"STOPPING": "MAGENTA",
"BROKEN": "YELLOW",
"STOPPED": "RED",
"FAILED": "BACK_YEL",
"FATAL": "BACK_RED",
}
return color_string(color_match[line], line + " "*(8 - len(line)))
def Get_max_flow():
return len (color_string("GREEN", "STARTING"))
def line_format(self): ####
timer = time.time()
time_delta = time.gmtime(timer - self.time)
curr_time = time.strftime("uptime: %H:%M:%S", time_delta)
if (self.process):
print('{0:37}{1:28}{2:15}{3:23}{4:15}'.format( \
color_string("BOLD", self.id), \
format_statut(self.status) + " "*(Get_max_flow() - len(format_statut(self.status))), \
" pid ", \
color_string("BOLD", str(self.process.pid)), \
str(curr_time)))
else:
print('{0:24}{1:29}{2:15}'\
.format(self.id, \
format_statut(self.status) + " "*(Get_max_flow() - len(format_statut(self.status))), \
str(self.state)))
def print_all_info(cmd, line):
try:
timer = time.time()
time_delta = time.gmtime(timer - cmd.time)
curr_time = time.strftime("%H:%M:%S", time_delta)
print ("_"*35 + " < " + line + " > " + "_"*35)
print( "Status: " + format_statut(cmd.status))
print( "State: " + str(cmd.state))
print( "umask: " + str(cmd.umask) )
print( "Path: " + str(cmd.path))
print( "workingdir: " + str(cmd.workingdir))
print( "Numprocs: " + str(cmd.numprocs))
print( "Stdout: " + str(cmd.stdout))
print( "Stderr: " + str(cmd.stderr))
print( "Autostart: " + str(cmd.autostart))
print( "Autorestart: " + str(cmd.autorestart))
print( "Exit codes: " + str(cmd.exit))
print( "Stop signal: " + str(cmd.stop_signal))
print( "Start fail | retry: " + str(cmd.start_fail) + " | " +str(cmd.startretries))
print( "Stop time: " + str(cmd.stoptime) )
if (cmd.process != None):
print ("_PROCESS_INFO" + "_"*((70-13 + len(line))/2))
print ("PID: " + str(cmd.process.pid))
print ("Process stdin: " + str(cmd.process.stdin))
print ("Process stdout: " + str(cmd.process.stdout))
print ("Process stderr: " + str(cmd.process.stderr))
print ("time: " + str(curr_time))
except:
print ("Info: " + line + ": Failed (INFOERR)")
def load_conf(file):
try:
fd = open(file, "r")
data = yaml.load(fd)
fd.close()
return data
except Exception, e:
print("ERROR loading yaml's data -> check yourfile.yaml")
log.warning("ERROR loading yaml's data -> check yourfile.yaml")
def check_process(cmd, line):
for k, v in cmd.iteritems():
curr = cmd[k]
if (curr.process and curr.id == str(line)):
return (curr)
return (None)
def close_fd(all_cmd):
for k, v in all_cmd.iteritems():
cmd = all_cmd[k]
try:
os.close(cmd.fdout)
except:
pass
try:
os.close(cmd.fderr)
except:
pass
def set_env(environ_os, environ_user):
try:
final_environ = copy.deepcopy(environ_os)
final_environ.update(environ_user)
return final_environ
except:
print ("set_env: Err")
log.warning("ERROR Environnement")
signaux = siglib()
task = task_event()