@@ -17,6 +17,9 @@ if [[ -z "${SYSTMP+x}" ]] || [[ -z "${SYSTMP##*[[:space:]]}" ]]; then
1717fi
1818export USER HOME SYSTMP
1919pushd " ${HOME} " & > /dev/null || exit 1
20+ # Global variables for signal handling
21+ CONTAINER_ID=" "
22+ CLEANUP_DONE=0
2023# ------------------------------------------------------------------------------------#
2124
2225# ------------------------------------------------------------------------------------#
@@ -56,6 +59,46 @@ log_debug() {
5659}
5760# ------------------------------------------------------------------------------------#
5861
62+ # ------------------------------------------------------------------------------------#
63+ # Signal handling functions
64+ cleanup_on_exit () {
65+ if [[ " ${CLEANUP_DONE} " == " 1" ]]; then
66+ return
67+ fi
68+ CLEANUP_DONE=1
69+
70+ log_warn " Received exit signal, cleaning up..."
71+
72+ if [[ -n " ${CONTAINER_ID} " ]]; then
73+ log_info " Stopping container: ${CONTAINER_ID} "
74+ ${PODMAN_SUDO} podman stop " ${CONTAINER_ID} " --time 10 & > /dev/null || true
75+
76+ log_info " Removing container: ${CONTAINER_ID} "
77+ ${PODMAN_SUDO} podman rm " ${CONTAINER_ID} " --force & > /dev/null || true
78+ fi
79+
80+ # Clean up any containers with our name
81+ local cleanup_containers
82+ cleanup_containers=" $( ${PODMAN_SUDO} podman ps -aq --filter " name=${CONTAINER_NAME} " 2> /dev/null || true) "
83+ if [[ -n " ${cleanup_containers} " ]]; then
84+ log_info " Cleaning up remaining containers with name: ${CONTAINER_NAME} "
85+ echo " ${cleanup_containers} " | xargs -r ${PODMAN_SUDO} podman stop --time 10 & > /dev/null || true
86+ echo " ${cleanup_containers} " | xargs -r ${PODMAN_SUDO} podman rm --force & > /dev/null || true
87+ fi
88+
89+ log_info " Cleanup completed"
90+ }
91+
92+ setup_signal_handlers () {
93+ # Set up signal handlers for graceful shutdown
94+ trap cleanup_on_exit EXIT
95+ trap ' log_warn "Received SIGINT (Ctrl+C)"; cleanup_on_exit; exit 130' INT
96+ trap ' log_warn "Received SIGTERM"; cleanup_on_exit; exit 143' TERM
97+ trap ' log_warn "Received SIGHUP"; cleanup_on_exit; exit 129' HUP
98+ trap ' log_warn "Received SIGQUIT"; cleanup_on_exit; exit 131' QUIT
99+ }
100+ # ------------------------------------------------------------------------------------#
101+
59102# ------------------------------------------------------------------------------------#
60103# Help function
61104show_help () {
@@ -118,6 +161,8 @@ NOTES:
118161 - Requires Podman to be installed and configured
119162 - May require passwordless sudo if Podman needs elevated privileges
120163 - Environment variables are overridden by command-line arguments
164+ - Script will run continuously and monitor the container until stopped
165+ - Use Ctrl+C or send SIGTERM to gracefully stop the container and exit
121166 - For more information: https://github.com/pkgforge/devscripts/blob/main/Github/Runners/README.md
122167
123168EOF
@@ -393,6 +438,18 @@ pull_image() {
393438 ;;
394439 esac
395440}
441+
442+ # Check if container is still running
443+ is_container_running () {
444+ local container_id=" $1 "
445+ [[ -n " ${container_id} " ]] && ${PODMAN_SUDO} podman ps -q --filter " id=${container_id} " | grep -q " ${container_id} "
446+ }
447+
448+ # Check if manager process is running inside container
449+ is_manager_running () {
450+ local container_id=" $1 "
451+ [[ -n " ${container_id} " ]] && ${PODMAN_SUDO} podman exec " ${container_id} " ps aux 2> /dev/null | grep -q " /usr/local/bin/manager.sh"
452+ }
396453# ------------------------------------------------------------------------------------#
397454
398455# ------------------------------------------------------------------------------------#
@@ -447,11 +504,10 @@ run_container() {
447504 sleep 30
448505
449506 # Get container details
450- local container_id
451- container_id=" $( ${PODMAN_SUDO} podman ps -qf name=" ${CONTAINER_NAME} " ) "
452- export PODMAN_ID=" ${container_id} "
507+ CONTAINER_ID=" $( ${PODMAN_SUDO} podman ps -qf name=" ${CONTAINER_NAME} " ) "
508+ export PODMAN_ID=" ${CONTAINER_ID} "
453509
454- if [[ -z " ${container_id } " ]]; then
510+ if [[ -z " ${CONTAINER_ID } " ]]; then
455511 log_error " Container failed to start. Check logs: ${LOG_FILE} "
456512 cat " ${LOG_FILE} "
457513 exit 1
@@ -462,46 +518,61 @@ run_container() {
462518 export PODMAN_LOGPATH=" ${log_path} "
463519
464520 log_info " Container started successfully"
465- log_info " Container ID: ${container_id } "
521+ log_info " Container ID: ${CONTAINER_ID } "
466522 log_info " Container Log Path: ${log_path} "
467523 log_info " Script Log File: ${LOG_FILE} "
468524
469525 # Execute runner manager
470526 log_info " Executing runner manager..."
471- ${PODMAN_SUDO} podman exec --user " runner" --env-file=" ${ENV_FILE} " " ${container_id } " " /usr/local/bin/manager.sh" >> " ${LOG_FILE} " 2>&1 &
527+ ${PODMAN_SUDO} podman exec --user " runner" --env-file=" ${ENV_FILE} " " ${CONTAINER_ID } " " /usr/local/bin/manager.sh" >> " ${LOG_FILE} " 2>&1 &
472528
473529 sleep 10
474530
475- # Monitor runner process
476- log_info " Monitoring runner process..."
531+ # Monitor runner process - stay active as long as container runs
532+ log_info " Monitoring runner process (will run until container stops or signal received)..."
533+ local consecutive_failures=0
534+ local max_consecutive_failures=3
535+
477536 while true ; do
478537 # Check if container is still running first
479- if ! ${PODMAN_SUDO} podman ps -q --filter " id= ${container_id} " | grep -qi " ${container_id }" ; then
538+ if ! is_container_running " ${CONTAINER_ID }" ; then
480539 log_warn " Container has stopped"
481540 break
482541 fi
483542
484543 # Check if manager process is running inside container
485- local process_check
486- process_check=" $( ${PODMAN_SUDO} podman exec " ${container_id} " ps aux 2> /dev/null | grep -c " /usr/local/bin/manager.sh" || echo " 0" ) "
487- if [[ " ${process_check} " -eq 0 ]]; then
488- log_warn " Runner process has stopped"
489- if [[ " ${VERBOSE} " == " 1" ]]; then
490- cat " ${LOG_FILE} "
544+ if ! is_manager_running " ${CONTAINER_ID} " ; then
545+ consecutive_failures=$(( consecutive_failures + 1 ))
546+ log_warn " Runner process check failed (attempt ${consecutive_failures} /${max_consecutive_failures} )"
547+
548+ if [[ " ${consecutive_failures} " -ge " ${max_consecutive_failures} " ]]; then
549+ log_warn " Runner process has stopped after ${max_consecutive_failures} consecutive checks"
550+ if [[ " ${VERBOSE} " == " 1" ]]; then
551+ echo " === Recent log output ==="
552+ tail -50 " ${LOG_FILE} " 2> /dev/null || true
553+ echo " ========================="
554+ fi
555+ break
491556 fi
492- ${PODMAN_SUDO} podman stop " ${container_id} " --ignore
493- break
557+ else
558+ consecutive_failures=0
559+ log_debug " Container and manager process are running normally"
494560 fi
561+
562+ # Wait before next check
495563 sleep 12
496564 done
497565
498- log_info " Runner completed"
566+ log_info " Container monitoring completed"
499567}
500568# ------------------------------------------------------------------------------------#
501569
502570# ------------------------------------------------------------------------------------#
503571# Main function
504572main () {
573+ # Set up signal handlers early
574+ setup_signal_handlers
575+
505576 # Parse command line arguments
506577 parse_arguments " $@ "
507578
@@ -543,7 +614,7 @@ main() {
543614 # Final status
544615 if [[ " ${QUIET} " != " 1" ]]; then
545616 echo
546- log_info " Runner completed successfully "
617+ log_info " Runner Session Ended "
547618 log_info " Log file: ${LOG_FILE} "
548619 echo
549620 log_info " Useful commands:"
0 commit comments