From 72ae9bd3897ed89270c1c9c6802cb92550fd954d Mon Sep 17 00:00:00 2001 From: Viet Nguyen Duc Date: Tue, 24 Oct 2023 05:42:41 +0700 Subject: [PATCH] Add session-task script for video recording --- Video/Dockerfile | 7 ++++--- Video/session_task.sh | 46 ++++++++++++++++++++++++++++++++++++++++++ Video/supervisord.conf | 12 +++++++++++ Video/video.sh | 10 +++++++-- 4 files changed, 70 insertions(+), 5 deletions(-) create mode 100644 Video/session_task.sh diff --git a/Video/Dockerfile b/Video/Dockerfile index 173b238ad..644bee05f 100644 --- a/Video/Dockerfile +++ b/Video/Dockerfile @@ -17,11 +17,13 @@ RUN python3 -m pip install --no-cache --upgrade --no-cache-dir pip urllib3 setup #====================================== # Add Supervisor configuration files #====================================== +ENV SE_VIDEO_FOLDER /videos +ENV SE_SESSION_FOLDER /sessions RUN mkdir -p /opt/bin/ -RUN mkdir -p /var/run/supervisor /var/log/supervisor /videos +RUN mkdir -p /var/run/supervisor /var/log/supervisor ${SE_VIDEO_FOLDER} ${SE_SESSION_FOLDER} COPY supervisord.conf /etc -COPY entry_point.sh video.sh video_ready.py /opt/bin/ +COPY entry_point.sh video.sh session_task.sh video_ready.py /opt/bin/ RUN chmod +x /opt/bin/* ENTRYPOINT ["/opt/bin/entry_point.sh"] @@ -35,7 +37,6 @@ ENV SE_FRAME_RATE 15 ENV SE_CODEC libx264 ENV SE_PRESET "-preset ultrafast" ENV FILE_NAME video.mp4 -ENV SE_VIDEO_FOLDER /videos ENV SE_VIDEO_RECORD true ENV SE_NODE_PORT 5555 ENV DRAIN_AFTER_SESSION_COUNT 0 diff --git a/Video/session_task.sh b/Video/session_task.sh new file mode 100644 index 000000000..b1a34bd5f --- /dev/null +++ b/Video/session_task.sh @@ -0,0 +1,46 @@ +#!/usr/bin/env bash + +max_recorded_count=$((${DRAIN_AFTER_SESSION_COUNT:-SE_DRAIN_AFTER_SESSION_COUNT})) +recorded_count=0 +last_session_id="" + +function get_session_id { + session_id=$(curl -s --request GET 'http://'${DISPLAY_CONTAINER_NAME}':'${SE_NODE_PORT}'/status' | jq -r '.[]?.node?.slots | .[0]?.session?.sessionId') +} + +function capture_session() { + echo "${POD_NAME}" > ${SE_SESSION_FOLDER}'/'$session_id'.txt' + cat ${SE_SESSION_FOLDER}'/'$session_id'.txt' + last_session_id=$session_id + recorded_count=$((recorded_count+1)) +} + +function terminate_gracefully { + echo "Trapped SIGTERM/SIGINT/x so shutting down the listener..." + while true; + do + get_session_id + if [ "$session_id" != "null" -a "$session_id" != "" ]; + then + echo "Session: $session_id is active, waiting for it to finish" + sleep 2 + else + break + fi + done + echo "Shutdown completed!" + exit 0 +} + +trap terminate_gracefully SIGTERM SIGINT + +while true; +do + get_session_id + if [ "$session_id" != "null" -a "$session_id" != "" ] && [ "$last_session_id" != "$session_id" ]; + then + capture_session + else + sleep 2 + fi +done diff --git a/Video/supervisord.conf b/Video/supervisord.conf index de8690f34..98166567a 100644 --- a/Video/supervisord.conf +++ b/Video/supervisord.conf @@ -35,3 +35,15 @@ stopsignal=TERM redirect_stderr=true stdout_logfile=/dev/stdout stdout_logfile_maxbytes=0 + +[program:session-task] +priority=1 +command=bash -c "/opt/bin/session_task.sh; EXIT_CODE=$?; kill -s SIGTERM `cat /var/run/supervisor/supervisord.pid`; exit $EXIT_CODE" +autostart=true +autorestart=unexpected +stopsignal=TERM + +;Logs (all activity redirected to stdout so it can be seen through "docker logs" +redirect_stderr=true +stdout_logfile=/dev/stdout +stdout_logfile_maxbytes=0 diff --git a/Video/video.sh b/Video/video.sh index d0d2844a8..2e874d77c 100644 --- a/Video/video.sh +++ b/Video/video.sh @@ -26,6 +26,11 @@ function stop_recording { recorded_count=$((recorded_count+1)) } +function stop_dependencies() { + pkill -15 -f "video_ready.py" + pkill -15 -f "session_task.sh" +} + function terminate_gracefully { echo "Trapped SIGTERM/SIGINT/x so shutting down video-recording..." while true; @@ -36,11 +41,11 @@ function terminate_gracefully { echo "Session: $session_id is active, waiting for it to finish" sleep 1 else - pkill --signal TERM -f "ffmpeg" + pkill -15 -f "ffmpeg" break fi done - pkill --signal TERM -f "video_ready.py" + stop_dependencies echo "Shutdown completed!" } @@ -84,6 +89,7 @@ then if [ $max_recorded_count -gt 0 ] && [ $recorded_count -ge $max_recorded_count ]; then echo "Max recorded count reached, exiting" + stop_dependencies exit 0 fi elif [ $recording_started = "true" ];