Skip to content

Commit c2fb57a

Browse files
authored
Refactor orchestrator pulse script for better logging
1 parent bbfc9a2 commit c2fb57a

1 file changed

Lines changed: 48 additions & 8 deletions

File tree

scripts/orchestrator-pulse.sh

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,63 @@
11
#!/bin/bash
22
# QuickClaw Orchestrator Pulse - Triggers orchestrator check-in
3+
set -euo pipefail
4+
5+
# Require QUICKCLAW_HOME to be set
6+
if [ -z "${QUICKCLAW_HOME:-}" ]; then
7+
echo "ERROR: QUICKCLAW_HOME environment variable is not set" >&2
8+
exit 1
9+
fi
10+
11+
# Require a message argument
12+
if [ -z "${1:-}" ]; then
13+
echo "ERROR: Message argument is required" >&2
14+
echo "Usage: $0 <message>" >&2
15+
exit 1
16+
fi
17+
18+
MESSAGE="$1"
19+
LOG_FILE="$QUICKCLAW_HOME/logs/orchestrator.log"
20+
OPENCLAW_PORT="${OPENCLAW_PORT:-18789}"
21+
OPENCLAW_CONFIG="${OPENCLAW_CONFIG:-$HOME/.openclaw/openclaw.json}"
322

4-
LOG_FILE="/Users/clawd/quickclaw/logs/orchestrator.log"
523
mkdir -p "$(dirname "$LOG_FILE")"
624

7-
echo "[$(date '+%Y-%m-%d %H:%M:%S')] Orchestrator pulse: $1" >> "$LOG_FILE"
25+
log() {
26+
echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> "$LOG_FILE"
27+
}
28+
29+
log "Orchestrator pulse: $MESSAGE"
30+
31+
# Verify config file exists before attempting to read
32+
if [ ! -f "$OPENCLAW_CONFIG" ]; then
33+
log "ERROR: OpenClaw config not found at $OPENCLAW_CONFIG"
34+
exit 1
35+
fi
836

937
# Get the auth token from openclaw.json
10-
TOKEN=$(grep -o '"token": "[^"]*"' /Users/clawd/.openclaw/openclaw.json 2>/dev/null | head -1 | cut -d'"' -f4)
38+
TOKEN=$(grep -o '"token": "[^"]*"' "$OPENCLAW_CONFIG" 2>/dev/null | head -1 | cut -d'"' -f4)
1139

1240
if [ -z "$TOKEN" ]; then
13-
echo "[$(date '+%Y-%m-%d %H:%M:%S')] ERROR: Could not find auth token" >> "$LOG_FILE"
14-
exit 1
41+
log "ERROR: Could not find auth token in $OPENCLAW_CONFIG"
42+
exit 1
1543
fi
1644

45+
# Build JSON payload safely using printf to avoid injection
46+
PAYLOAD=$(printf '{"agentId": "orchestrator", "message": "%s"}' "$MESSAGE")
47+
1748
# Send message to orchestrator
18-
curl -s -X POST http://localhost:18789/api/v1/sessions \
49+
HTTP_STATUS=$(curl -s -o /tmp/quickclaw_response.tmp -w "%{http_code}" \
50+
-X POST "http://localhost:${OPENCLAW_PORT}/api/v1/sessions" \
1951
-H "Authorization: Bearer $TOKEN" \
2052
-H "Content-Type: application/json" \
21-
-d "{\"agentId\": \"orchestrator\", \"message\": \"$1\"}" >> "$LOG_FILE" 2>&1
53+
-d "$PAYLOAD")
54+
55+
if [ "$HTTP_STATUS" -ge 200 ] && [ "$HTTP_STATUS" -lt 300 ]; then
56+
log "Pulse delivered successfully (HTTP $HTTP_STATUS)"
57+
else
58+
log "ERROR: Pulse failed (HTTP $HTTP_STATUS): $(cat /tmp/quickclaw_response.tmp 2>/dev/null)"
59+
rm -f /tmp/quickclaw_response.tmp
60+
exit 1
61+
fi
2262

23-
echo "" >> "$LOG_FILE"
63+
rm -f /tmp/quickclaw_response.tmp

0 commit comments

Comments
 (0)