This repository has been archived by the owner on Jul 3, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessManager.py
214 lines (174 loc) · 6.36 KB
/
ProcessManager.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
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
209
210
211
212
213
214
"""THIS FILE IS SUBJECT TO THE LICENSE TERMS GRANTED BY THE UNIVERSITY OF SASKATCHEWAN SPACE TEAM (USST)."""
import shlex
import sys
import asyncio
import logging
from subprocess import Popen
from time import sleep
log = logging.getLogger("Process-Manager")
log.setLevel(logging.INFO)
class RoboProcess:
"""Manages and keeps track of a process."""
def __init__(self, name, cmd):
"""Initialize a process containing command cmd."""
if not isinstance(cmd, str):
raise ValueError('command must be a string')
self.name = name
self.cmd = cmd
self.pid = None
self.released = False
self.process = None
async def restart(self):
"""
Restarts/starts a process. If the process is currenlty executing it will be killed then restarted.
Returns
-------
asyncio.subprocess.Process
The created asyncio process.
"""
# if the process was released, unrelease it
self._released = False
# kill the process if it exists
await self.kill()
self.process = await asyncio.create_subprocess_shell(self.cmd)
self.pid = self.process.pid
return self.process
async def run(self):
"""
Run the given process definition until it exists successfully. This function is a coroutine.
Returns
-------
int
The program's return value.
"""
# start the loop until the process exists successfully
returncode = None
self.process = await self.restart()
while self.process:
log.info("Started process({}, '{}')".format(self.pid, self.name))
returncode = await self.process.wait()
last_pid = self.pid
self.pid = None
if self._released:
self.process = None
else:
self.on_exit(returncode)
if self.process:
log.warning("Process ({}, '{}') failed, exiting with {}. Restaring process.".format(last_pid, self.name, returncode))
else:
log.info("Process ({}, {}) exited successfully with {}.".format(last_pid, self.name, returncode))
return returncode
async def kill(self, timeout=1, release=False):
"""
Stop the process.
Ignored if the process is not running.
"""
if self.process:
self.process.terminate()
try:
future = self.process.wait()
await asyncio.wait_for(future, timeout)
except asyncio.TimeoutError:
self.process.kill()
await self.process.wait()
self.released = release
def on_exit(self, returncode):
"""To be called when the process exits"""
raise NotImplementedError('RoboProcess does not define a default behaivior on exit. Please inherit and define the on_exit(returncode) method')
class RunOnce(RoboProcess):
"""Process that runs """
def on_exit(self, returncode):
self.process = None
class RestartOnCrash(RoboProcess):
"""Process that restarts on non zero exit"""
def on_exit(self, returncode):
if returncode != 0:
self.restart()
else:
self.process = None
class ProcessManager:
"""
Manages processes that run in the robocluster framework.
Processes are programs that can run independently from each other, in any
language supported by the robocluster library.
The ProcessManager is in charge of starting and stopping processes, and
monitoring their status and handle the event of a crash or a process not
responding.
"""
def __init__(self):
"""Initialize a process manager."""
self.processes = {} # store processes by name
self._futures = []
def __enter__(self):
"""Enter context manager."""
return self
def __exit__(self, *exc):
"""Exit context manager, makes sure all processes are stopped."""
self.stop()
return False
def isEmpty(self):
"""Return if processes is empty."""
return len(self.processes) == 0
def createProcess(self, name, command):
"""
Create a process.
Arguments:
name - name to identify process, must be unique to process manager.
command - shell command for process to execute.
"""
self.addProcess(RoboProcess(name, command))
def addProcess(self, roboprocess):
"""Adds roboprocess that was created externally to the manager"""
if roboprocess.name in self.processes:
raise ValueError('Process with the same name exists: {name}')
self.processes[roboprocess.name] = roboprocess
def start(self, *names):
"""
Start processes.
If no arguments are provided, starts all processes.
"""
processes = names if names else self.processes.keys()
for process in processes:
try:
print('Starting:', process)
self._futures.append(asyncio.ensure_future(
self.processes[process].run()))
except KeyError:
pass
def stop(self, *names, timeout=0):
"""
Stop processes.
If no arguments are provided, stops all processes.
"""
processes = names if names else self.processes.keys()
for process in processes:
try:
print('Stopping:', process)
asyncio.ensure_future(
self.processes[process].kill(timeout=timeout, release=True))
except KeyError:
pass
@staticmethod
def run():
"""Run the event loop"""
loop = asyncio.get_event_loop()
loop.run_forever()
def main():
"""Run a process manager in the foreground."""
process_list = [
RunOnce('sleep', 'python sleeper.py'),
RunOnce("printer", "python ./examples/processes/print_periodically.py 1 4"),
RunOnce("terminal", 'termite'),
# RestartOnCrash('crasher', 'python crash.py')
]
with ProcessManager() as manager:
"""Initialize all the processes"""
for proc in process_list:
manager.addProcess(proc)
manager.start()
try:
manager.run()
except KeyboardInterrupt:
pass
if __name__ == "__main__":
exit(main())