-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathexternalrunner.py
More file actions
58 lines (50 loc) · 2.16 KB
/
externalrunner.py
File metadata and controls
58 lines (50 loc) · 2.16 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
from events import Events
import asyncio
import os
import signal
class ExternalProcess:
def __init__(self, command, sessionBased, restartOnExit, logName=None):
self.command = command
self.task = None
self.process = None
self.restartOnExit = restartOnExit
self.logName = logName
self.terminating = False
if sessionBased:
Events.getInstance().sessionStarted.append(lambda: self.onSessionStarted())
Events.getInstance().sessionEnded.append(lambda: self.onSessionEnded())
else:
self.startProcess()
def onSessionStarted(self):
self.startProcess()
def onSessionEnded(self):
self.endProcess()
def startProcess(self):
self.terminating = False
loop = asyncio.get_event_loop()
self.task = loop.create_task(self.__startProcess())
async def __startProcess(self):
print('Executing \'{}\''.format(self.command))
logFilePath = os.path.join(os.path.dirname(os.path.realpath(__file__)), self.logName)
print("Logging to \'{}\'".format(logFilePath))
with open(logFilePath, 'w') as logFile:
try:
self.process = await asyncio.create_subprocess_shell(self.command, stdout=logFile, stderr=logFile, start_new_session=True)
await self.process.communicate()
print("Process \'{}\' finished".format(self.command))
self.task = None
self.process = None
if self.restartOnExit and not self.terminating:
print("Restarting \'{}\'".format(self.command))
self.startProcess()
except asyncio.CancelledError:
print('Terminated \'{}\''.format(self.command))
if self.process is not None:
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)
self.task = None
self.process = None
def endProcess(self):
print("Terminating \'{}\'".format(self.command))
if self.process is not None:
self.terminating = True
os.killpg(os.getpgid(self.process.pid), signal.SIGTERM)