-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathvideo_ready.py
More file actions
executable file
·54 lines (42 loc) · 2.09 KB
/
video_ready.py
File metadata and controls
executable file
·54 lines (42 loc) · 2.09 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
import json
import signal
import sys
from http.server import BaseHTTPRequestHandler, HTTPServer
from os import environ
import psutil
video_ready_port = int(environ.get('VIDEO_READY_PORT', 9000))
class Handler(BaseHTTPRequestHandler):
def do_GET(self):
event_driven = environ.get('SE_VIDEO_EVENT_DRIVEN', 'false').lower() == 'true'
record_video_enabled = environ.get('SE_RECORD_VIDEO', 'true').lower() == 'true'
# Legacy shell mode compatibility:
# when global recording is disabled, report ready immediately.
if not event_driven and not record_video_enabled:
video_ready = True
else:
# Event-driven mode enables upload by non-empty destination.
# Legacy shell mode keeps original SE_VIDEO_UPLOAD_ENABLED behaviour.
if event_driven:
upload_enabled = bool(environ.get('SE_UPLOAD_DESTINATION_PREFIX', '').strip())
else:
upload_enabled = environ.get('SE_VIDEO_UPLOAD_ENABLED', 'false').lower() == 'true'
if not upload_enabled and environ.get('SE_VIDEO_FILE_NAME', 'video.mp4').lower() != 'auto':
video_ready = "ffmpeg" in (p.name().lower() for p in psutil.process_iter())
else:
video_ready = True
response_code = 200 if video_ready else 404
response_text = "ready" if video_ready else "not ready"
self.send_response(response_code)
self.end_headers()
self.wfile.write(json.dumps({'status': response_text}).encode('utf-8'))
def graceful_shutdown(signum, frame):
print("Trapped SIGTERM/SIGINT/x so shutting down video-ready...")
# httpd.shutdown() must be called from a different thread than serve_forever()
# or it deadlocks. video-ready has no state to drain, so close the socket
# and exit directly — supervisord will see the clean exit immediately.
httpd.server_close()
sys.exit(0)
signal.signal(signal.SIGINT, graceful_shutdown)
signal.signal(signal.SIGTERM, graceful_shutdown)
httpd = HTTPServer(('0.0.0.0', video_ready_port), Handler)
httpd.serve_forever()