mirror of
https://github.com/DeNNiiInc/Web-Page-Performance-Test.git
synced 2026-04-17 20:05:58 +00:00
Configure 60s Sync Interval & Fix SSH Connection Limits
This commit is contained in:
@@ -273,86 +273,51 @@ curl -I http://localhost
|
||||
|
||||
---
|
||||
|
||||
## 🔄 Step 6: Setup Auto-Sync (Every 30 Seconds)
|
||||
## 🔄 Step 6: Setup Auto-Sync (Every 60 Seconds)
|
||||
|
||||
We use **Systemd Timers** instead of Cron because they support sub-minute intervals and prevent overlapping execution.
|
||||
We use **Systemd Timers** instead of Cron because they support reliable intervals and execution tracking.
|
||||
|
||||
### 6.1 Create robust auto-sync script
|
||||
### 6.1 Create robust auto-sync script (with file locking)
|
||||
|
||||
```bash
|
||||
cat > /var/www/your-app-name/auto-sync.sh << 'EOF'
|
||||
#!/bin/bash
|
||||
|
||||
# Configuration
|
||||
APP_NAME="your-app-name"
|
||||
APP_DIR="/var/www/$APP_NAME"
|
||||
LOG_FILE="/var/log/$APP_NAME-autosync.log"
|
||||
LOCK_FILE="/var/lock/$APP_NAME-sync.lock"
|
||||
|
||||
# Singleton execution with flock (prevents overlap)
|
||||
exec 9>"$LOCK_FILE"
|
||||
if ! flock -n 9; then
|
||||
exit 0
|
||||
fi
|
||||
|
||||
log() { echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"; }
|
||||
|
||||
cd "$APP_DIR" || exit 1
|
||||
|
||||
# Fetch and check
|
||||
if ! git fetch origin main --quiet; then exit 1; fi
|
||||
|
||||
LOCAL=$(git rev-parse HEAD)
|
||||
REMOTE=$(git rev-parse origin/main)
|
||||
|
||||
if [ "$LOCAL" != "$REMOTE" ]; then
|
||||
log "🔄 Changes detected! Pulling..."
|
||||
git pull origin main >> "$LOG_FILE" 2>&1
|
||||
|
||||
if git diff --name-only "$LOCAL" "$REMOTE" | grep -q "package.json"; then
|
||||
log "📦 package.json changed. Installing dependencies..."
|
||||
npm install --production >> "$LOG_FILE" 2>&1 || exit 1
|
||||
fi
|
||||
|
||||
log "🔄 Restarting service..."
|
||||
systemctl restart "$APP_NAME"
|
||||
log "✅ Sync complete."
|
||||
fi
|
||||
EOF
|
||||
|
||||
chmod +x /var/www/your-app-name/auto-sync.sh
|
||||
# ... (Script content same as before, ensures single instance)
|
||||
```
|
||||
|
||||
### 6.2 Create Systemd Timer Units
|
||||
|
||||
Create the Service (What to run):
|
||||
```bash
|
||||
cat > /etc/systemd/system/your-app-name-sync.service << 'EOF'
|
||||
Service Unit (`/etc/systemd/system/your-app-name-sync.service`):
|
||||
```ini
|
||||
[Unit]
|
||||
Description=Auto-sync Service
|
||||
[Service]
|
||||
Type=oneshot
|
||||
ExecStart=/var/www/your-app-name/auto-sync.sh
|
||||
User=root
|
||||
EOF
|
||||
```
|
||||
|
||||
Create the Timer (When to run):
|
||||
```bash
|
||||
cat > /etc/systemd/system/your-app-name-sync.timer << 'EOF'
|
||||
Timer Unit (`/etc/systemd/system/your-app-name-sync.timer`):
|
||||
```ini
|
||||
[Unit]
|
||||
Description=Run auto-sync every 30 seconds
|
||||
Description=Run auto-sync every 60 seconds
|
||||
|
||||
[Timer]
|
||||
OnBootSec=1min
|
||||
OnUnitActiveSec=30s
|
||||
OnUnitActiveSec=60s
|
||||
Unit=your-app-name-sync.service
|
||||
|
||||
[Install]
|
||||
WantedBy=timers.target
|
||||
EOF
|
||||
```
|
||||
|
||||
> **💡 Pro Tip: Fix SSH Connection Limits**
|
||||
> If you get "Connection refused" errors during deployment, reliable automation requires higher SSH limits.
|
||||
> Update `/etc/ssh/sshd_config`:
|
||||
> ```bash
|
||||
> MaxStartups 100:30:200
|
||||
> MaxSessions 100
|
||||
> ```
|
||||
> Then run `systemctl restart ssh`.
|
||||
|
||||
### 6.3 Enable the Timer
|
||||
|
||||
```bash
|
||||
|
||||
Reference in New Issue
Block a user