-
Notifications
You must be signed in to change notification settings - Fork 0
/
supervisor.py
637 lines (565 loc) · 23.5 KB
/
supervisor.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
##########################################################################################
# #
# OpenSpace Streaming #
# #
# Copyright (c) 2024 #
# #
# Permission is hereby granted, free of charge, to any person obtaining a copy of this #
# software and associated documentation files (the "Software"), to deal in the Software #
# without restriction, including without limitation the rights to use, copy, modify, #
# merge, publish, distribute, sublicense, and/or sell copies of the Software, and to #
# permit persons to whom the Software is furnished to do so, subject to the following #
# conditions: #
# #
# The above copyright notice and this permission notice shall be included in all copies #
# or substantial portions of the Software. #
# #
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, #
# INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A #
# PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT #
# HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF #
# CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE #
# OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #
##########################################################################################
import argparse
import asyncio
from enum import Enum, auto
import functools
import glob
import json
import os
import psutil
import shutil
import subprocess
import sys
if not os.name == "nt":
import termios
import tty
else:
import msvcrt
import time
from threading import Thread, Timer
import websockets
from openspace import Api
"""
This Supervisor script continuously runs on a streaming rendering server.
Upon startup, it starts the WebGUI Frontend served by node.js in a separate terminal,
and the WebRTC signaling server in another terminal.
It communicates with a web backend server via a websocket connection. It sends messages
only as a response to received commands.
A separate API document covers the API and functionality in greater detail.
"""
OpenSpaceExecRelativeDir = "bin/RelWithDebInfo"
OpenSpaceCfgRelativeDir = "config"
Processes = []
RunOpenSpaceInShell = False
class State(Enum):
# Running state for an OpenSpace instance
IDLE = auto()
INITIALIZING = auto()
RUNNING = auto()
DEINITIALIZING = auto()
INVALID = auto()
class OsProcess:
"""
Class for running and tracking an individual OpenSpace executable instance,
with the state and thread it runs in.
"""
def __init__(self):
self.state = State.IDLE
self.handle = None
self.pid_OpenSpace = None
self.pid_ParentShell = None
self.stopSignal = None
self.thread = None
def setState(self, newState):
self.state = newState
def currentState(self):
return self.state
def currentStateString(self):
if self.state == State.IDLE:
return "IDLE"
elif self.state == State.INITIALIZING:
return "INITIALIZING"
elif self.state == State.RUNNING:
return "RUNNING"
elif self.state == State.DEINITIALIZING:
return "DEINITIALIZING"
else:
return "INVALID"
def setProcessHandle(self, handle):
self.handle = handle
def getHandle(self):
return self.handle
def setPidOpenSpace(self, pid):
self.pid_OpenSpace = pid
def pidOpenSpace(self):
return self.pid_OpenSpace
def setPidParentShell(self, pid):
self.pid_ParentShell = pid
def pidParentShell(self):
return self.pid_ParentShell
def assignStopSignal(self, signal):
self.stopSignal = signal
def hasStopSignaled(self):
return self.stopSignal.is_set()
def setThread(self, thread):
self.thread = thread
def thread(self):
return self.thread
def reset(self):
self.__init__()
def setupArgparse():
"""
Creates and sets up a parser for commandline arguments. This function returns the parsed
arguments as a dictionary.
"""
parser = argparse.ArgumentParser()
parser.add_argument(
"--openspaceDir",
dest="osdir",
type=str,
help="Specifies the OpenSpace directory relative to this script.",
default="OpenSpace",
required=False
)
parser.add_argument(
"--webGuiDir",
dest="webguidir",
type=str,
help="Specifies the OpenSpace WebGuiFrontend directory relative to this script.",
default="OpenSpace-WebGuiFrontend",
required=False
)
parser.add_argument(
"--ipaddress",
dest="ip_addr",
type=str,
help="The IP address of this rendering server",
default="155.98.19.66",
required=False
)
parser.add_argument(
"--capacity",
dest="renderCapacity",
type=str,
help="The max number of simultaneous openspace instances.",
default=3,
required=False
)
args = parser.parse_args()
return args
def runOpenspace(executable, baseDir, instanceId):
"""
Run Openspace using the streaming SGCT configuration.
- `executable`: The path to the OpenSpace executable that should be run
- `baseDir`: The base path of the OpenSpace installation
- `stopEvents_instance`: Event used to signal that this OpenSpace instance should stop
- 'instanceId': Unique ID for this particular instance of OpenSpace
"""
global Processes
print(f"Starting OpenSpace ID {instanceId}")
workingDirectory = os.path.dirname(os.path.normpath(executable))
sgctConfigFile = os.path.normpath(f"{baseDir}/config/remote_gstreamer_output.json")
openspaceArgs = []
if RunOpenSpaceInShell:
openspaceArgs = ["start"]
if os.name == "nt":
openspaceArgs.extend(["powershell", "$Host.UI.RawUI.WindowTitle='OpenSpace'; "])
else:
openspaceArgs.append("gnome-terminal")
openspaceArgs.extend([
os.path.normpath(executable),
"--config", sgctConfigFile,
"--profile", "default",
"--bypassLauncher"
])
process = subprocess.Popen(
openspaceArgs,
shell=RunOpenSpaceInShell,
cwd=workingDirectory,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE
)
if RunOpenSpaceInShell:
time.sleep(4) #Wait for it to start OpenSpace
Processes[instanceId].setPidOpenSpace(None)
Processes[instanceId].setPidParentShell(None)
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
try:
# Check if the process command matches the target
if not proc.info:
continue
if proc.info['name'].lower() == 'powershell.exe':
parent = psutil.Process(proc.pid)
children = parent.children(recursive=False)
for child in children:
if child.name():
if child.name().lower() == "openspace.exe":
msg = f"Found powershell pid {proc.pid}" \
f" with OpenSpace.exe child ({child.pid})."
Processes[instanceId].setPidOpenSpace(child.pid)
Processes[instanceId].setPidParentShell(proc.pid)
print(msg)
break
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
print("exception with ps info")
pass
else:
Processes[instanceId].setProcessHandle(process)
time.sleep(10)
async def mainLoop():
# Loop for waiting until Openspace has initialized
print(f"Establishing API connection for ID {instanceId}...")
os_api = Api("localhost", 4681)
os_api.connect()
openspace = await os_api.singleReturnLibrary()
asyncio.new_event_loop().run_until_complete(mainLoop())
Processes[instanceId].setState(State.RUNNING)
print(f"OpenSpace ID {instanceId} INITIALIZING -> RUNNING")
async def waitForInstanceToStopByPid(procPid):
while psutil.pid_exists(procPid):
await asyncio.sleep(2.0)
async def waitForInstanceToStopByHandle(procHandle):
while procHandle.poll() is None:
await asyncio.sleep(2.0)
# Wait until this OpenSpace instance has stopped (e.g. via the frontend gui)
if RunOpenSpaceInShell:
asyncio.new_event_loop().run_until_complete(
waitForInstanceToStopByPid(Processes[instanceId].pidOpenSpace())
)
else:
asyncio.new_event_loop().run_until_complete(
waitForInstanceToStopByHandle(process)
)
Processes[instanceId].setState(State.IDLE)
print(f"OpenSpace ID {instanceId} RUNNING -> IDLE")
def setTimerForDeinitializationPeriod(idStopped):
global Processes
Processes[idStopped].setState(State.IDLE)
print("timer expired")
async def processMessage(websocket, message, openspaceBaseDir):
"""
Handle JSON messages from web backend, execute command, then send response
"""
global Processes
try:
json_data = json.loads(message)
command = json_data['command']
print(f"Received command: {command}")
result = json.loads("""{
"command": "START",
"error": "none",
"id": 0
}""")
result['command'] = command # echo the command back
if command == "START":
startId = -1
for i in range(0, len(Processes)):
if Processes[i].currentState() == State.IDLE:
startId = i
break
if startId != -1:
if startId > 0:
openspaceBaseDir = f"{openspaceBaseDir}/../OpenSpace_s{startId}"
Processes[startId].setThread(Thread(
target=runOpenspace,
args= [
f"{openspaceBaseDir}/{OpenSpaceExecRelativeDir}/OpenSpace.exe",
openspaceBaseDir,
startId
]
)
)
Processes[startId].thread.daemon = True
Processes[startId].thread.start()
Processes[startId].setState(State.INITIALIZING)
else:
result['error'] = "no available slots"
result['id'] = startId
await sendMessage(websocket, json.dumps(result))
elif command == "STOP":
idToStop = json_data['id']
result['id'] = idToStop
if idToStop < len(Processes):
if Processes[idToStop].currentState() == State.IDLE:
result['error'] = "not running"
await sendMessage(websocket, json.dumps(result))
if Processes[idToStop].currentState() != State.IDLE:
Processes[idToStop].setState(State.DEINITIALIZING)
timer = Timer(5.0, setTimerForDeinitializationPeriod, args=(idToStop,))
timer.start()
if RunOpenSpaceInShell:
await terminateOpenSpaceInstanceInShell(idToStop)
Processes[idToStop].reset()
else:
terminateOpenSpaceInstance(idToStop)
else:
result['error'] = "invalid id"
await sendMessage(websocket, json.dumps(result))
elif command == "STATUS":
idForStatus = json_data['id']
if idForStatus < len(Processes) and idForStatus >= 0:
result['status'] = Processes[idForStatus].currentStateString()
else:
result['status'] = State.INVALID
result['error'] = "invalid id"
await sendMessage(websocket, json.dumps(result))
elif command == "SERVER_STATUS":
nRunning = 0
for i in range(0, len(Processes)):
if Processes[i].currentState() != State.IDLE:
nRunning += 1
result['running'] = nRunning
result['total'] = len(Processes)
await sendMessage(websocket, json.dumps(result))
else:
print(f"Invalid message received: '{message}'")
await sendMessage(
websocket,
json.dumps({"error": f"invalid message received: {command}"})
)
except json.JSONDecodeError as e:
print(f"JSON decode error {e}")
await sendMessage(
websocket,
json.dumps({"error": "json decode error"})
)
def terminateOpenSpaceInstance(id):
c = Processes[id].currentState()
if c == State.INITIALIZING or c == State.RUNNING or c == State.DEINITIALIZING:
Processes[id].getHandle().kill()
async def terminateOpenSpaceInstanceInShell(id):
c = Processes[id].currentState()
if c == State.INITIALIZING or c == State.RUNNING or c == State.DEINITIALIZING:
if Processes[id].pidOpenSpace():
p = psutil.Process(Processes[id].pidParentShell())
terminateProcessByPid(Processes[id].pidParentShell())
p.kill()
p.terminate()
await asyncio.sleep(2)
p = psutil.Process(Processes[id].pidOpenSpace())
p.kill()
p.terminate()
else:
print(f"Unable to terminate OpenSpace via its parent shell pid "
f"{Processes[id].pidParentShell()}")
async def sendMessage(websocket, message):
print("Sending msg: '" + message + "'")
await websocket.send(message)
async def receiveProcess(websocket, openspaceBaseDir):
try:
message = await websocket.recv()
await processMessage(
websocket,
message,
openspaceBaseDir
)
except websockets.ConnectionClosed:
print("Connection closed")
async def websocketServer(stopEvent_main, openspaceBaseDir):
boundHandler = functools.partial(
receiveProcess,
openspaceBaseDir=openspaceBaseDir
)
async with websockets.serve(boundHandler, "localhost", 4699):
print("WebSocket server started on ws://localhost:4699")
await stopEvent_main.wait() # Wait until stop event is set
print(f"Quitting websocketServer.")
def keyPressedUnix():
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
return sys.stdin.read(1) #non-blocking single-char read
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
def keyPressedWin():
return msvcrt.kbhit()
def keyPressed():
if os.name == "nt":
return keyPressedWin()
else:
return keyPressedUnix()
async def webGuiFrontendServer(stopEvent, workingDir):
"""
Start WebGUI Frontend node.js server in the workingDir in a separate terminal.
Runs until the stopEvent signal is set.
"""
execPath = os.path.normpath(workingDir)
execArgs = ["start", "powershell", "npm", "start"]
if os.name != "nt":
execArgs[1] = "gnome-terminal"
process = subprocess.Popen(
execArgs,
shell=True,
cwd=execPath,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE)
print("Started WebGUI Frontend server.")
# Wait for the stop signal while the process runs
while not stopEvent.is_set():
await asyncio.sleep(1) # Periodically check for the stop signal
# Stop signal received, terminate the subprocess
await terminateProcess("node.exe", ["start"])
await terminateProcess("node.exe", ["webpack-dev-server"])
print("Quit WebGUI Frontend server.")
async def signalingServer(stopEvent, workingDir):
"""
Start WebRTC signaling server in the workingDir in a separate terminal.
Runs until the stopEvent signal is set.
"""
execPath = os.path.normpath(workingDir)
execArgs = ["start"]
if os.name == "nt":
execArgs.extend(["powershell", "$Host.UI.RawUI.WindowTitle='signalingserver'; "])
else:
execArgs.append("gnome-terminal")
execArgs.extend(["node", "signalingserver"])
process = subprocess.Popen(
execArgs,
shell=True,
cwd=execPath,
stdout=subprocess.DEVNULL,
stderr=subprocess.PIPE)
print("Started signalingserver.")
# Wait for the stop signal while the process runs
while not stopEvent.is_set():
await asyncio.sleep(1) # Periodically check for the stop signal
# Stop signal received, terminate the subprocess
await terminateProcess("node.exe", ["signalingserver"])
print("Quit signalingserver.")
async def terminateProcess(processName, processElems, ignoreElems=[]):
"""
Terminate a process by its commandline elements
- `processName`: The exact name of the process executable
- `processElems`: An array of strings containing the commandline elements of the
running process. For a process to be terminated, its 'cmdline'
properties must contain ALL of the string elements in this array.
- `ignoreElems`: An array of elements that will disqualify a process from being
terminated. If the process' cmdline elements contain any one of
these, then it will not be terminated.
"""
processElems = [elem.lower() for elem in processElems]
ignoreElems = [elem.lower() for elem in ignoreElems]
# Iterate through all running processes
for proc in psutil.process_iter(['pid', 'name', 'cmdline']):
try:
# Check if the process command matches the target
if not proc.info:
continue
if proc.info['name'].lower() != processName.lower():
continue
iterProcElems = proc.info['cmdline']
fullName = f"{processName} {iterProcElems}"
iterProcElems = [elem.lower() for elem in iterProcElems]
if iterProcElems == None or len(iterProcElems) == 0:
continue
proceedWithTermination = True
for ignore in ignoreElems:
if any(ignore in iterProcElem for iterProcElem in iterProcElems):
proceedWithTermination = False
if len(processElems) > 0:
for elem in processElems:
if not any(elem in iterProcElem for iterProcElem in iterProcElems):
proceedWithTermination = False
if proceedWithTermination:
if RunOpenSpaceInShell:
terminateProcessByPid(proc.info['pid'])
else:
subprocess.check_output(f"Taskkill /PID {proc.info['pid']} /F")
print(f"Terminated {fullName} with PID: {proc.info['pid']}")
await asyncio.sleep(0.5) # Give it time to terminate
except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
pass
def terminateProcessByPid(pidToTerminate):
"""
Terminate a process by its pid. Some processes don't seem to respond to psutil.kill,
so this function uses a windows 'Taskkill /PID # /F' command.
- `pidToTerminate`: The process pid that will be terminated.
"""
subprocess.check_output(f"Taskkill /PID {pidToTerminate} /F")
async def shutdownOnKeypress(stopEvent):
"""
If 'q' key is pressed, signal the stopEvent which is used to stop other processes
"""
while True:
if keyPressed():
key = str(msvcrt.getch())
if key == "b'q'" or key == "b'Q'":
print("Shutting down...")
stopEvent.set()
break
await asyncio.sleep(0.25)
async def shutdownTaskAndVerify(taskHandle, taskName):
#taskHandle.cancel()
try:
await taskHandle
except asyncio.CancelledError:
print(f"Task '{taskName}' clean shutdown.")
async def mainAsync(openspaceBaseDir, openspaceFrontendDir, signalingServerDir):
"""
Main asynchronous loop to run:
server for websocket comms with web backend
WebGUI frontend node.js server
WebRTC signaling server
After starting these processes, waits for a keypress to initiate shutdown.
Parameters:
- openspacceBaseDir : absolute path to the base dir of the OpenSpace installation
- openspaceFrontendDir : absolute path to the base dir of WebGUI Frontend
- signalingServerDir : absolute path to the directory where the signaling server
code resides (currently within the WebGUI Frontend dir)
"""
global Processes
stopEvent_main = asyncio.Event()
for i in range(0, len(Processes)):
Processes[i].assignStopSignal(asyncio.Event())
# Start the websocket server
websocket_task = asyncio.create_task(
websocketServer(stopEvent_main, openspaceBaseDir)
)
# Start the webGUI frontend node.js server
webGuiFrontend_task = asyncio.create_task(
webGuiFrontendServer(stopEvent_main, openspaceFrontendDir)
)
# Start the signaling server
signalingserver_task = asyncio.create_task(
signalingServer(stopEvent_main, signalingServerDir)
)
# Start the shutdown task on keypress
shutdown_task = asyncio.create_task(shutdownOnKeypress(stopEvent_main))
print("Press 'q' to stop.")
await shutdown_task
print("Finished waiting for shutdown_task")
# Kill any OpenSpace instances that are running
for i in range(0, len(Processes)):
if RunOpenSpaceInShell:
await terminateOpenSpaceInstanceInShell(i)
Processes[i].reset()
else:
terminateOpenSpaceInstance(i)
await shutdownTaskAndVerify(websocket_task, "websocket server")
await shutdownTaskAndVerify(webGuiFrontend_task, "webGuiFrontend server")
await shutdownTaskAndVerify(signalingserver_task, "signaling server")
print("Exiting mainAsync.")
if __name__ == "__main__":
args = setupArgparse()
for i in range(0, args.renderCapacity):
Processes.append(OsProcess())
print(f"Render capacity: {args.renderCapacity} instances.")
scriptDir = os.path.realpath(os.path.dirname(__file__))
openspaceExec = f"{scriptDir}/{args.osdir}/{OpenSpaceExecRelativeDir}/OpenSpace.exe"
if not os.path.exists(openspaceExec):
raise Exception(f"Could not find OpenSpace exe '{openspaceExec}'")
openspaceFrontendDir = f"{scriptDir}/{args.webguidir}"
if not os.path.exists(openspaceFrontendDir):
raise Exception(f"Could not find frontend gui '{openspaceFrontendDir}'")
openspaceSignaling = f"{scriptDir}/{args.webguidir}/src/signalingserver"
if not os.path.exists(openspaceSignaling):
raise Exception(f"Could not find signaling server '{openspaceSignaling}'")
asyncio.run(mainAsync(
f"{scriptDir}/{args.osdir}",
openspaceFrontendDir,
openspaceSignaling)
)
print("Quit __main__")