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
|
```bash
|
||||||
cat > /var/www/your-app-name/auto-sync.sh << 'EOF'
|
# ... (Script content same as before, ensures single instance)
|
||||||
#!/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
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### 6.2 Create Systemd Timer Units
|
### 6.2 Create Systemd Timer Units
|
||||||
|
|
||||||
Create the Service (What to run):
|
Service Unit (`/etc/systemd/system/your-app-name-sync.service`):
|
||||||
```bash
|
```ini
|
||||||
cat > /etc/systemd/system/your-app-name-sync.service << 'EOF'
|
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Auto-sync Service
|
Description=Auto-sync Service
|
||||||
[Service]
|
[Service]
|
||||||
Type=oneshot
|
Type=oneshot
|
||||||
ExecStart=/var/www/your-app-name/auto-sync.sh
|
ExecStart=/var/www/your-app-name/auto-sync.sh
|
||||||
User=root
|
User=root
|
||||||
EOF
|
|
||||||
```
|
```
|
||||||
|
|
||||||
Create the Timer (When to run):
|
Timer Unit (`/etc/systemd/system/your-app-name-sync.timer`):
|
||||||
```bash
|
```ini
|
||||||
cat > /etc/systemd/system/your-app-name-sync.timer << 'EOF'
|
|
||||||
[Unit]
|
[Unit]
|
||||||
Description=Run auto-sync every 30 seconds
|
Description=Run auto-sync every 60 seconds
|
||||||
|
|
||||||
[Timer]
|
[Timer]
|
||||||
OnBootSec=1min
|
OnBootSec=1min
|
||||||
OnUnitActiveSec=30s
|
OnUnitActiveSec=60s
|
||||||
Unit=your-app-name-sync.service
|
Unit=your-app-name-sync.service
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
WantedBy=timers.target
|
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
|
### 6.3 Enable the Timer
|
||||||
|
|
||||||
```bash
|
```bash
|
||||||
|
|||||||
30
deploy-sync-from-git.ps1
Normal file
30
deploy-sync-from-git.ps1
Normal file
@@ -0,0 +1,30 @@
|
|||||||
|
$Server = "172.16.69.219"
|
||||||
|
$User = "root"
|
||||||
|
$Pass = "Q4dv!Z`$nCe#`$OT&h"
|
||||||
|
$RemotePath = "/var/www/web-page-performance-test"
|
||||||
|
|
||||||
|
# Commands to run on server
|
||||||
|
# 1. Pull latest files
|
||||||
|
# 2. Copy Systemd units to /etc/systemd/system/
|
||||||
|
# 3. Enable and start timer
|
||||||
|
$Commands = @(
|
||||||
|
"cd $RemotePath",
|
||||||
|
"git fetch origin main",
|
||||||
|
"git reset --hard origin/main", # Force match repo
|
||||||
|
"chmod +x auto-sync-robust.sh",
|
||||||
|
"chmod +x fix-ssh-limits.sh",
|
||||||
|
"./fix-ssh-limits.sh", # Apply SSH limits fix first
|
||||||
|
"cp auto-sync-robust.sh auto-sync.sh",
|
||||||
|
"cp web-page-performance-test-sync.service /etc/systemd/system/",
|
||||||
|
"cp web-page-performance-test-sync.timer /etc/systemd/system/",
|
||||||
|
"systemctl daemon-reload",
|
||||||
|
"crontab -l | grep -v 'auto-sync.sh' | crontab -",
|
||||||
|
"systemctl enable web-page-performance-test-sync.timer",
|
||||||
|
"systemctl restart web-page-performance-test-sync.timer",
|
||||||
|
"systemctl list-timers --all | grep web-page",
|
||||||
|
"echo '✅ Systemd Timer (60s) Deployed & SSH Limits Fixed!'"
|
||||||
|
)
|
||||||
|
$CommandStr = $Commands -join " && "
|
||||||
|
|
||||||
|
echo "🚀 Triggering remote deployment (Pull & Apply)..."
|
||||||
|
plink -batch -pw "$Pass" "$User@$Server" $CommandStr
|
||||||
43
fix-ssh-limits.sh
Normal file
43
fix-ssh-limits.sh
Normal file
@@ -0,0 +1,43 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
# fix-ssh-limits.sh
|
||||||
|
# Increases SSH connection limits to prevent "Connection refused" during rapid automation.
|
||||||
|
|
||||||
|
SSHD_CONFIG="/etc/ssh/sshd_config"
|
||||||
|
|
||||||
|
log() {
|
||||||
|
echo "[SSH-FIX] $1"
|
||||||
|
}
|
||||||
|
|
||||||
|
if [ ! -f "$SSHD_CONFIG" ]; then
|
||||||
|
log "Error: $SSHD_CONFIG not found."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# Backup config
|
||||||
|
cp "$SSHD_CONFIG" "$SSHD_CONFIG.bak.$(date +%s)"
|
||||||
|
|
||||||
|
# Function to update or add a setting
|
||||||
|
set_config() {
|
||||||
|
local param=$1
|
||||||
|
local value=$2
|
||||||
|
if grep -q "^#\?${param}" "$SSHD_CONFIG"; then
|
||||||
|
sed -i "s/^#\?${param}.*/${param} ${value}/" "$SSHD_CONFIG"
|
||||||
|
else
|
||||||
|
echo "${param} ${value}" >> "$SSHD_CONFIG"
|
||||||
|
fi
|
||||||
|
log "Set ${param} to ${value}"
|
||||||
|
}
|
||||||
|
|
||||||
|
# Apply high limits
|
||||||
|
set_config "MaxStartups" "100:30:200"
|
||||||
|
set_config "MaxSessions" "100"
|
||||||
|
set_config "MaxAuthTries" "20"
|
||||||
|
|
||||||
|
# Reload SSH service
|
||||||
|
if systemctl restart ssh; then
|
||||||
|
log "✅ SSH service restarted successfully with new limits."
|
||||||
|
log "VERIFICATION: $(grep 'MaxStartups' $SSHD_CONFIG)"
|
||||||
|
else
|
||||||
|
log "❌ Failed to restart SSH service."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
@@ -3,7 +3,7 @@ Description=Run auto-sync every 30 seconds
|
|||||||
|
|
||||||
[Timer]
|
[Timer]
|
||||||
OnBootSec=1min
|
OnBootSec=1min
|
||||||
OnUnitActiveSec=30s
|
OnUnitActiveSec=60s
|
||||||
Unit=web-page-performance-test-sync.service
|
Unit=web-page-performance-test-sync.service
|
||||||
|
|
||||||
[Install]
|
[Install]
|
||||||
|
|||||||
Reference in New Issue
Block a user