1313 APP_DIR : chipnik_monitor
1414 PYTHON_VERSION : " 3.11"
1515 SERVICE_PORT : " 8080"
16- SERVICE_NAME : chipnik-monitor
17- SYSTEMD_SERVICE_PATH : /etc/systemd/system/chipnik-monitor.service
18- SYSTEMD_ENV_DIR : /etc/chipnik-monitor
19- SYSTEMD_ENV_FILE : /etc/chipnik-monitor/chipnik-monitor.env
2016
2117jobs :
2218 deploy :
@@ -45,72 +41,50 @@ jobs:
4541 - name : Stop existing service
4642 shell : bash
4743 run : |
48- systemctl stop "${SERVICE_NAME}.service" || true
4944 if pgrep -f "uvicorn api:app" >/dev/null; then
50- echo "Stopping legacy Chipnik Monitor process"
51- pkill -f "uvicorn api:app" || true
45+ echo "Stopping existing Chipnik Monitor process"
46+ pkill -f "uvicorn api:app"
5247 sleep 5
5348 else
54- echo "No legacy Chipnik Monitor process found"
49+ echo "No existing Chipnik Monitor process found"
5550 fi
5651
57- - name : Configure systemd service
52+ - name : Start service
5853 env :
5954 EARTHDATA_BEARER_TOKEN : ${{ secrets.EARTHDATA_BEARER_TOKEN }}
6055 MONGO_URI : ${{ secrets.MONGO_URI }}
6156 MONGO_DB : ${{ secrets.MONGO_DB }}
6257 DAYS_BACK_LIMIT : ${{ vars.DAYS_BACK_LIMIT }}
58+ working-directory : ${{ env.APP_DIR }}
6359 shell : bash
6460 run : |
65- set -euo pipefail
66-
6761 for var in EARTHDATA_BEARER_TOKEN MONGO_URI MONGO_DB; do
68- if [[ -z "${!var:- }" ]]; then
62+ if [[ -z "${!var}" ]]; then
6963 echo "::error::Environment variable $var must be provided via repository secrets."
7064 exit 1
7165 fi
7266 done
7367
74- SERVICE_FILE="${SYSTEMD_SERVICE_PATH}"
75- ENV_DIR="${SYSTEMD_ENV_DIR}"
76- ENV_FILE="${SYSTEMD_ENV_FILE}"
77- WORKDIR="${GITHUB_WORKSPACE}/${APP_DIR}"
78- RUN_USER="$(whoami)"
79- PYTHON_BIN="$(command -v python)"
80-
81- if [[ -z "${PYTHON_BIN}" ]]; then
82- echo "::error::Python binary not found in PATH"
83- exit 1
84- fi
68+ LOG_PATH="${HOME}/chipnik-monitor.log"
69+ echo "Starting Chipnik Monitor on port ${SERVICE_PORT}, logging to ${LOG_PATH}"
8570
86- mkdir -p "${ENV_DIR}"
87- {
88- printf '%s\n' "EARTHDATA_BEARER_TOKEN=${EARTHDATA_BEARER_TOKEN}"
89- printf '%s\n' "MONGO_URI=${MONGO_URI}"
90- printf '%s\n' "MONGO_DB=${MONGO_DB}"
91- printf '%s\n' "DAYS_BACK_LIMIT=${DAYS_BACK_LIMIT:-2000}"
92- } | tee "${ENV_FILE}" >/dev/null
93- chmod 600 "${ENV_FILE}"
71+ nohup env \
72+ EARTHDATA_BEARER_TOKEN="${EARTHDATA_BEARER_TOKEN}" \
73+ MONGO_URI="${MONGO_URI}" \
74+ MONGO_DB="${MONGO_DB}" \
75+ DAYS_BACK_LIMIT="${DAYS_BACK_LIMIT:-2000}" \
76+ uvicorn api:app --host 0.0.0.0 --port "${SERVICE_PORT}" \
77+ > "${LOG_PATH}" 2>&1 &
9478
95- export SERVICE_FILE ENV_FILE WORKDIR RUN_USER PYTHON_BIN SERVICE_PORT
96- python -c "import os, textwrap; service = textwrap.dedent(f\"\"\"[Unit]\nDescription=Chipnik Monitor API\nAfter=network.target\n\n[Service]\nType=simple\nUser={os.environ['RUN_USER']}\nGroup={os.environ['RUN_USER']}\nWorkingDirectory={os.environ['WORKDIR']}\nEnvironmentFile={os.environ['ENV_FILE']}\nExecStart={os.environ['PYTHON_BIN']} -m uvicorn api:app --host 0.0.0.0 --port {os.environ['SERVICE_PORT']}\nRestart=on-failure\nRestartSec=5\n\n[Install]\nWantedBy=multi-user.target\n\"\"\"); print(service, end='')" | tee "${SERVICE_FILE}" >/dev/null
97-
98- systemctl daemon-reload
99- systemctl enable "${SERVICE_NAME}.service"
100-
101- - name : Restart systemd service
102- shell : bash
103- run : |
104- systemctl restart "${SERVICE_NAME}.service"
105- systemctl status "${SERVICE_NAME}.service" --no-pager
79+ echo $! > "${HOME}/chipnik-monitor.pid"
80+ echo "Process started with PID $(cat "${HOME}/chipnik-monitor.pid")"
10681
10782 - name : Verify service
10883 shell : bash
10984 run : |
11085 sleep 5
111- if ! systemctl is-active --quiet "${SERVICE_NAME}.service"; then
112- systemctl status "${SERVICE_NAME}.service" --no-pager
113- echo "::error::Chipnik Monitor systemd service failed to start."
86+ if ! pgrep -f "uvicorn api:app" >/dev/null; then
87+ echo "::error::Chipnik Monitor service failed to start. Check logs at ${HOME}/chipnik-monitor.log"
11488 exit 1
11589 fi
116- echo "Chipnik Monitor systemd service is running"
90+ echo "Chipnik Monitor service is running"
0 commit comments