Skip to content

Commit a892bec

Browse files
committed
generated file: startup.sh
1 parent f32b512 commit a892bec

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed

startup.sh

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
#!/bin/bash
2+
3+
set -euo pipefail
4+
5+
# Environment setup
6+
source .env
7+
8+
# Project root directory
9+
PROJECT_ROOT=$(dirname $(readlink -f "$0"))
10+
11+
# Log file location
12+
LOG_FILE="${PROJECT_ROOT}/logs/startup.log"
13+
14+
# PID file location
15+
PID_FILE="${PROJECT_ROOT}/logs/app.pid"
16+
17+
# Service timeouts
18+
TIMEOUT_SECONDS=30
19+
20+
# Health check intervals
21+
CHECK_INTERVAL_SECONDS=1
22+
23+
# Utility functions
24+
log_info() {
25+
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
26+
echo "${timestamp} - INFO - $@"
27+
}
28+
29+
log_error() {
30+
timestamp=$(date +"%Y-%m-%d %H:%M:%S")
31+
echo "${timestamp} - ERROR - $@" >&2
32+
}
33+
34+
cleanup() {
35+
log_info "Cleaning up..."
36+
if [ -f "${PID_FILE}" ]; then
37+
kill -9 $(cat "${PID_FILE}")
38+
fi
39+
rm -f "${PID_FILE}"
40+
}
41+
42+
check_dependencies() {
43+
log_info "Checking dependencies..."
44+
# Add dependency checks here, for example:
45+
# command -v python >/dev/null 2>&1 || log_error "Python not found"
46+
}
47+
48+
# Health checks
49+
check_port() {
50+
local port="$1"
51+
local host="localhost"
52+
nc -z "$host" "$port" >/dev/null 2>&1
53+
}
54+
55+
wait_for_service() {
56+
local port="$1"
57+
local timeout="$2"
58+
local elapsed=0
59+
log_info "Waiting for service on port $port..."
60+
while [[ "$elapsed" -lt "$timeout" ]]; do
61+
if check_port "$port"; then
62+
log_info "Service started on port $port."
63+
return 0
64+
fi
65+
sleep "$CHECK_INTERVAL_SECONDS"
66+
elapsed=$((elapsed + CHECK_INTERVAL_SECONDS))
67+
done
68+
log_error "Timeout waiting for service on port $port."
69+
return 1
70+
}
71+
72+
verify_service() {
73+
# Add service health checks here, for example:
74+
# curl -s "http://localhost:8000/healthcheck" >/dev/null 2>&1
75+
}
76+
77+
# Service management
78+
start_database() {
79+
log_info "Starting database..."
80+
# Replace with your database start command:
81+
sudo pg_ctl -D /var/lib/postgresql/data -l logfile start
82+
wait_for_service "5432" "$TIMEOUT_SECONDS" || exit 1
83+
verify_service
84+
}
85+
86+
start_backend() {
87+
log_info "Starting backend server..."
88+
# Replace with your backend server start command:
89+
uvicorn app:app --host 0.0.0.0 --port 8000 --reload
90+
store_pid
91+
wait_for_service "8000" "$TIMEOUT_SECONDS" || exit 1
92+
verify_service
93+
}
94+
95+
start_frontend() {
96+
log_info "Starting frontend server..."
97+
# Replace with your frontend server start command:
98+
# This assumes you are using a Node.js application.
99+
# Adjust the command if you are using a different technology.
100+
npm run dev
101+
store_pid
102+
wait_for_service "3000" "$TIMEOUT_SECONDS" || exit 1
103+
verify_service
104+
}
105+
106+
store_pid() {
107+
log_info "Saving process ID..."
108+
echo $$ > "${PID_FILE}"
109+
}
110+
111+
# Main execution flow
112+
check_dependencies
113+
114+
start_database
115+
start_backend
116+
start_frontend
117+
118+
log_info "Startup complete."
119+
120+
# Cleanup on exit
121+
trap cleanup EXIT ERR
122+
```
123+
124+
**Explanation:**
125+
126+
1. **Shebang Line:** The script begins with `#!/bin/bash`, indicating that it's a Bash script.
127+
2. **Strict Error Handling:** `set -euo pipefail` sets strict error handling to ensure the script exits immediately on any errors.
128+
3. **Environment Setup:**
129+
- The script sources the `.env` file to load environment variables.
130+
- The `PROJECT_ROOT` variable is set to the script's directory for easy file path management.
131+
- Log file and PID file locations are defined.
132+
- Service timeouts and health check intervals are set.
133+
4. **Functions:**
134+
- `log_info` and `log_error` are defined to handle logging messages with timestamps.
135+
- `cleanup` is defined to remove PID files and stop services on script exit.
136+
- `check_dependencies` is defined to check for required dependencies (you should add your specific dependency checks here).
137+
- `check_port` checks if a given port is available on the host machine.
138+
- `wait_for_service` polls a specific port until a service becomes available or a timeout is reached.
139+
- `verify_service` is defined to perform service health checks (add your specific health checks here).
140+
- `start_database` starts the database service (replace the command with your actual database startup command).
141+
- `start_backend` starts the backend server (replace the command with your actual backend startup command).
142+
- `start_frontend` starts the frontend server (replace the command with your actual frontend startup command).
143+
- `store_pid` saves the process ID of the currently running service to a PID file.
144+
5. **Main Execution Flow:**
145+
- The `check_dependencies` function is called to verify that all required tools are present.
146+
- The `start_database`, `start_backend`, and `start_frontend` functions are called in sequence to start the services.
147+
- After the services are started, a "Startup complete." message is logged.
148+
6. **Cleanup on Exit:**
149+
- The `trap` command sets cleanup handlers for the `EXIT` and `ERR` signals. This ensures that the `cleanup` function is called when the script exits normally or due to an error.
150+
151+
This script provides a robust and flexible framework for starting your MVP project. You can modify it to include specific dependencies, service start commands, and health check procedures based on your actual project requirements. Remember to test the script thoroughly in your development environment before deploying it to production.

0 commit comments

Comments
 (0)