11#! /bin/bash
22set -e
33
4- # Prepare OpenRC for container environment
5- if [ ! -d /run/openrc ]; then
6- mkdir -p /run/openrc
7- touch /run/openrc/softlevel
8- fi
9-
10- # Setup necessary directories for OpenRC
11- mkdir -p /tmp/gwrs/log
12- mkdir -p /run
13- mkdir -p /var/run
14-
15- # Prevent init scripts from running during install
16- echo ' rc_provide="loopback net"' >> /etc/rc.conf
17-
18- # Setup basic mounts expected by OpenRC
19- mount -t proc none /proc
20- mount -t sysfs none /sys
21- mount -t tmpfs none /run
22-
23- # Start OpenRC
24- if [ " $1 " = " /sbin/init" ]; then
25- # Start services directly
26- /sbin/rc-service gwrs-core start
27- /sbin/rc-service gwrs-api start
4+ # Simple configuration
5+ LOG_DIR=" /tmp/gwrs/log"
6+ PID_DIR=" /tmp/gwrs/pids"
7+ CHECK_INTERVAL=5
8+
9+ # Create directories
10+ mkdir -p " $LOG_DIR " " $PID_DIR "
11+
12+ # Logging
13+ log () {
14+ echo " [$( date ' +%Y-%m-%d %H:%M:%S' ) ] $1 " | tee -a " $LOG_DIR /manager.log"
15+ }
16+
17+ # Start core service
18+ start_core () {
19+ log " Starting router-core..."
20+ nohup /usr/local/bin/router-core > " $LOG_DIR /core.log" 2> " $LOG_DIR /core.error" &
21+ echo $! > " $PID_DIR /core.pid"
22+ log " router-core started (PID: $! )"
23+ }
24+
25+ # Start API service
26+ start_api () {
27+ log " Starting router-api..."
28+ nohup /usr/local/bin/router-api > " $LOG_DIR /api.log" 2> " $LOG_DIR /api.error" &
29+ echo $! > " $PID_DIR /api.pid"
30+ log " router-api started (PID: $! )"
31+ }
32+
33+ # Stop a service
34+ stop_service () {
35+ local service=$1
36+ local pid_file=" $PID_DIR /${service} .pid"
37+
38+ if [ -f " $pid_file " ]; then
39+ local pid=$( cat " $pid_file " )
40+ if kill -0 $pid 2> /dev/null; then
41+ log " Stopping $service (PID: $pid )"
42+ kill $pid
43+ sleep 2
44+ # Force kill if still running
45+ if kill -0 $pid 2> /dev/null; then
46+ kill -9 $pid
47+ fi
48+ fi
49+ rm -f " $pid_file "
50+ fi
51+ }
52+
53+ # Check if service is running
54+ is_running () {
55+ local service=$1
56+ local pid_file=" $PID_DIR /${service} .pid"
57+
58+ if [ -f " $pid_file " ]; then
59+ local pid=$( cat " $pid_file " )
60+ if kill -0 $pid 2> /dev/null; then
61+ return 0 # Running
62+ else
63+ rm -f " $pid_file " # Clean up stale PID
64+ fi
65+ fi
66+ return 1 # Not running
67+ }
68+
69+ # Restart core (and then API)
70+ restart_core () {
71+ log " Core is down! Restarting core and API..."
72+
73+ # Stop both services
74+ stop_service " api"
75+ stop_service " core"
2876
29- # Keep container running
30- echo " Services started. Container is now running..."
31- exec tail -f /tmp/gwrs/log/core.log /tmp/gwrs/log/api.log
32- else
33- # Run command as specified
34- exec " $@ "
35- fi
77+ sleep 2
78+
79+ # Start core first
80+ start_core
81+ sleep 3
82+
83+ # Then start API
84+ start_api
85+ }
86+
87+ # Restart API only
88+ restart_api () {
89+ log " API is down! Restarting API..."
90+ stop_service " api"
91+ sleep 2
92+ start_api
93+ }
94+
95+ # Monitor services
96+ monitor () {
97+ log " Starting service monitor..."
98+
99+ while true ; do
100+ # Check core
101+ if ! is_running " core" ; then
102+ restart_core
103+ # Check API (only if core is running)
104+ elif ! is_running " api" ; then
105+ restart_api
106+ fi
107+
108+ sleep $CHECK_INTERVAL
109+ done
110+ }
111+
112+ # Cleanup on exit
113+ cleanup () {
114+ log " Shutting down services..."
115+ stop_service " api"
116+ stop_service " core"
117+ exit 0
118+ }
119+
120+ # Handle signals
121+ trap cleanup SIGTERM SIGINT
122+
123+ # Main execution
124+ log " === Router Process Manager Starting ==="
125+
126+ # Start both services
127+ start_core
128+ sleep 3 # Give core time to start
129+ start_api
130+
131+ # Monitor forever
132+ monitor
0 commit comments