-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathslowglass.py
117 lines (94 loc) · 2.76 KB
/
slowglass.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
import sys, time
from os import sep
import socket
from multiprocessing import Process, Pipe
from multiprocessing.managers import BaseManager
import signal, errno
import shutil
import os
import capture, player
procs = {}
pipes = {}
running = False
def startup():
global procs
global pipes
global running
#cleanup in case of bad shutdown
shutil.rmtree('images', ignore_errors=True)
os.mkdir("images")
running = True
# init capture
master, slave = Pipe() # endpoint [0]-PlexConnect, [1]-DNSServer
proc = Process(target=capture.Run, args=[slave])
proc.start()
time.sleep(0.1)
if proc.is_alive():
procs['capture'] = proc
pipes['capture'] = master
else:
print('Slowglass', 0, "capture not alive. Shutting down.")
running = False
time.sleep(2)
# init player
if running:
master, slave = Pipe() # endpoint [0]-PlexConnect, [1]-WebServer
proc = Process(target=player.Run, args=[slave])
proc.start()
time.sleep(0.1)
if proc.is_alive():
procs['player'] = proc
pipes['player'] = master
else:
dprint('Slowglass', 0, "player not alive. Shutting down.")
running = False
# not started successful - clean up
if not running:
cmdShutdown()
return running
def shutdown():
for slave in procs:
procs[slave].join()
shutil.rmtree('images')
print('Slowglass', 0, "shutdown")
def cmdShutdown():
global running
running = False
# send shutdown to all pipes
for slave in pipes:
pipes[slave].send('shutdown')
print('Slowglass', 0, "Shutting down.")
def run(timeout=5):
global procs
# do something important
try:
time.sleep(timeout)
for slave in procs:
if procs[slave].is_alive() is not True:
print('Slowglass', 0, "One of the slaves has died...shuting down")
cmdShutdown()
break;
except IOError as e:
if e.errno == errno.EINTR and not running:
pass # mask "IOError: [Errno 4] Interrupted function call"
else:
raise
return running
def sighandler_shutdown(signum, frame):
signal.signal(signal.SIGINT, signal.SIG_IGN) # we heard you!
cmdShutdown()
def getRunning():
global running
return running
if __name__=="__main__":
signal.signal(signal.SIGINT, sighandler_shutdown)
signal.signal(signal.SIGTERM, sighandler_shutdown)
print('Slowglass', 0, "***")
print('Slowglass', 0, "Slowglass")
print('Slowglass', 0, "Press CTRL-C to shut down.")
print('Slowglass', 0, "***")
running = startup()
print('Slowglass', 0, "Slowglass started")
while running:
running = run()
shutdown()