-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_event.py
More file actions
117 lines (105 loc) · 4.05 KB
/
cmd_event.py
File metadata and controls
117 lines (105 loc) · 4.05 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
# **************************************************************************** #
# #
# ::: :::::::: #
# cmd_event.py :+: :+: :+: #
# +:+ +:+ +:+ #
# By: skhatir <marvin@42.fr> +#+ +:+ +#+ #
# +#+#+#+#+#+ +#+ #
# Created: 2016/06/30 10:04:58 by skhatir #+# #+# #
# Updated: 2016/06/30 10:05:00 by skhatir ### ########.fr #
# #
# **************************************************************************** #
import os
import time
import shlex
import subprocess
import task_lib
import cmd_lib
class cmd_event:
def __init__(self, key, params):
in_config = cmd_lib.in_config
self.id = key
self.status = "WAITING"
self.state = "OK"
self.path = in_config(self, params, "cmd") or task_lib.color_string("RED","Empty")
self.workingdir = in_config(self, params, "workingdir") or "/tmp"
self.numprocs = in_config(self, params, "numprocs") or 1
self.stdout = in_config(self, params, "stdout") or "/tmp/task_STDOUT"
self.fdout = None
self.stderr = in_config(self, params, "stderr") or "/tmp/task_STDERR"
self.fderr = None
self.autostart = in_config(self, params, "autostart") or False
self.autorestart = in_config(self, params, "autorestart") or False
self.exit = in_config(self, params, "exitcodes") or [0, 2]
self.starttime = in_config(self, params, "starttime") or 1
self.start_timer = -1
self.startretries = in_config(self, params, "startretries") or 3
self.start_fail = 0
self.stop_signal = task_lib.signaux.get_signum(in_config(self, params, "stopsignal")) or 15
self.stoptime = in_config(self, params, "stoptime") or 1
self.umask = in_config(self, params, "umask") or 022
self.stop_timer = -1
self.time = 0
self.process = None
self.env = task_lib.set_env(os.environ, in_config(self, params, "env"))
if (self.state == "OK"): cmd_lib.post_init(self)
if (self.state != "OK"):
self.status = "FATAL"
task_lib.log.info(self.id + ': has been created: status:' + self.status)
def start(self, autostart):
if (self.state == "OK" or self.state == "ERROR processus: check: /tmp/logger.task"):
try:
cmd_split = shlex.split(self.path)
proc = subprocess.Popen(
cmd_split,
cwd = self.workingdir,
stdin = subprocess.PIPE,
stdout = self.fdout,
stderr = self.fderr,
env = self.env
)
self.status = "STARTING"
self.stop_timer = -1
self.start_timer = 0
self.process = proc
self.time = time.time()
task_lib.log.info(self.id + ': is starting: status:' + self.status)
except Exception, e:
if (self.start_fail >= self.startretries):
self.status = "FATAL"
else:
self.status = "FAILED"
self.state = "ERROR processus: check: /tmp/logger.task"
task_lib.log.warning(self.id + ': ' + e.strerror)
self.start_fail +=1
else:
self.status = "FATAL"
def stop(self):
self.start_timer = -1
self.start_fail = 0
self.stop_timer = 0
self.status = "STOPPING"
task_lib.log.info(self.id + ': is stopping: status:' + self.status)
try:
self.process.send_signal(self.stop_signal)
except OSError, e:
task_lib.log.warning(self.id + ': ' + e.strerror)
self.finish(15)
def show_status(self):
task_lib.line_format(self)
def restart(self):
task_lib.log.info(self.id + ': is restarting: status:' + self.status)
self.stop()
self.start(False)
def finish(self, signum):
self.start_timer = -1
self.stop_timer = -1
self.status = "STOPPED"
self.process = None
task_lib.log.info(self.id + ': has been stopped: status:' + self.status)
if (self.autorestart == True):
self.start(False)
elif (self.autorestart == "unexpected"):
for exit in self.exit:
if (exit == signum):
self.start(False)