Spaces:
Paused
Paused
Merge pull request #15 from anurag162008/codex/investigate-changes-in-openclaw.json-backup
Browse filesImprove workspace sync (config watch/settle, locking, restart sync) and key-rotator logging
Files changed (2)
- openclaw-sync.py+27-1
- start.sh+94-45
6 unmodified lines78910111224 unmodified lines373839404142349 unmodified lines39239339439539639739841 unmodified lines440441442443444445137 unmodified lines5835845855865875886 unmodified linesremotes or requiring a manual HF_USERNAME secret."""import hashlibimport jsonimport logging24 unmodified linesOPENCLAW_CONFIG_FILE = OPENCLAW_HOME / "openclaw.json"WORKSPACE = OPENCLAW_HOME / "workspace"STATUS_FILE = Path("/tmp/sync-status.json")INTERVAL = int(os.environ.get("SYNC_INTERVAL", "180"))INITIAL_DELAY = int(os.environ.get("SYNC_START_DELAY", "10"))CONFIG_WATCH_INTERVAL = max(349 unmodified linesreturn Falsedef sync_once(last_fingerprint: str | None = None,last_marker: tuple[int, int, int] | None = None,) -> tuple[str, tuple[int, int, int]]:41 unmodified linesreturn (current_fingerprint, current_marker)def handle_signal(_sig, _frame) -> None:STOP_EVENT.set()137 unmodified lineswrite_status("error", f"Shutdown sync failed: {exc}")print(f"Workspace sync: shutdown sync failed: {exc}")return 1if command == "loop":return loop()6 unmodified lines7891011121324 unmodified lines38394041424344349 unmodified lines39439539639739839940041 unmodified lines442443444445446447448449450451452453454455456457458459460137 unmodified lines5985996006016026036046056066076086096106116126136146 unmodified linesremotes or requiring a manual HF_USERNAME secret."""import fcntlimport hashlibimport jsonimport logging24 unmodified linesOPENCLAW_CONFIG_FILE = OPENCLAW_HOME / "openclaw.json"WORKSPACE = OPENCLAW_HOME / "workspace"STATUS_FILE = Path("/tmp/sync-status.json")SYNC_LOCK_FILE = Path("/tmp/huggingclaw-sync.lock")INTERVAL = int(os.environ.get("SYNC_INTERVAL", "180"))INITIAL_DELAY = int(os.environ.get("SYNC_START_DELAY", "10"))CONFIG_WATCH_INTERVAL = max(349 unmodified linesreturn Falsedef _sync_once_unlocked(last_fingerprint: str | None = None,last_marker: tuple[int, int, int] | None = None,) -> tuple[str, tuple[int, int, int]]:41 unmodified linesreturn (current_fingerprint, current_marker)def sync_once(last_fingerprint: str | None = None,last_marker: tuple[int, int, int] | None = None,) -> tuple[str, tuple[int, int, int]]:SYNC_LOCK_FILE.parent.mkdir(parents=True, exist_ok=True)with SYNC_LOCK_FILE.open("w", encoding="utf-8") as lock_handle:fcntl.flock(lock_handle, fcntl.LOCK_EX)try:return _sync_once_unlocked(last_fingerprint, last_marker)finally:fcntl.flock(lock_handle, fcntl.LOCK_UN)def handle_signal(_sig, _frame) -> None:STOP_EVENT.set()137 unmodified lineswrite_status("error", f"Shutdown sync failed: {exc}")print(f"Workspace sync: shutdown sync failed: {exc}")return 1if command == "sync-once-settled":try:trigger, _ = wait_for_config_settle(file_marker(OPENCLAW_CONFIG_FILE))if trigger == "stopped":return 1sync_once()return 0except Exception as exc:write_status("error", f"Settled sync failed: {exc}")print(f"Workspace sync: settled sync failed: {exc}")return 1if command == "loop":return loop()
1017 unmodified lines101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761017 unmodified linessync_installed_plugins_into_allow# ββ Launch gateway ββecho "Launching OpenClaw gateway on port 7860..."GATEWAY_ARGS=(gateway run --port 7860 --bind lan)if [ "${GATEWAY_VERBOSE:-0}" = "1" ]; thenGATEWAY_ARGS+=(--verbose)echo "Gateway verbose logging enabled (GATEWAY_VERBOSE=1)"fi# Use stdbuf -oL -eL to ensure logs are not buffered and appear immediately# in the console. NOTE: $! captures the LAST pipeline element (tee), not# openclaw β fine for passing to `wait` (waits for the whole pipeline to# finish), but kill -0 on it is uninformative. We probe TCP instead.stdbuf -oL -eL openclaw "${GATEWAY_ARGS[@]}" 2>&1 | tee -a /home/node/.openclaw/gateway.log &GATEWAY_PID=$!# Poll for the gateway to start listening on 7860. OpenClaw can take 20-30s# on cold start (plugin install + auto-restore). Bail out early if the# pipeline died.GATEWAY_READY_TIMEOUT="${GATEWAY_READY_TIMEOUT:-90}"ready=falsefor ((i=0; i<GATEWAY_READY_TIMEOUT; i++)); doif (echo > /dev/tcp/127.0.0.1/7860) 2>/dev/null; thenready=truebreakfiif ! kill -0 "$GATEWAY_PID" 2>/dev/null; thenbreakfisleep 1doneif [ "$ready" != "true" ]; thenecho ""echo "Gateway failed to start. Last 30 lines of log:"echo "ββββββββββββββββββββββββββββββββββββββββββββ"tail -30 /home/node/.openclaw/gateway.logexit 1fi# 11. Start WhatsApp Guardian after the gateway is accepting connectionsif [ "$WHATSAPP_ENABLED_NORMALIZED" = "true" ]; thennode /home/node/app/wa-guardian.js &GUARDIAN_PID=$!echo "WhatsApp Guardian started (PID: $GUARDIAN_PID)"fi# 11.5 Warm up the managed browser so first browser actions have a live tabwarmup_browser# 12. Start Workspace Sync after startup settlesif [ -n "${HF_TOKEN:-}" ]; thenpython3 -u /home/node/app/openclaw-sync.py loop &fi# Wait for gateway (allows trap to fire)wait $GATEWAY_PID1017 unmodified lines1018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251017 unmodified linessync_installed_plugins_into_allow# ββ Launch gateway ββGATEWAY_RESTART_DELAY="${GATEWAY_RESTART_DELAY:-2}"GATEWAY_MAX_RESTARTS="${GATEWAY_MAX_RESTARTS:-0}"GATEWAY_RESTART_COUNT=0SYNC_LOOP_PID=""GUARDIAN_PID=""sync_before_gateway_restart() {[ -n "${HF_TOKEN:-}" ] || return 0[ -f "/home/node/app/openclaw-sync.py" ] || return 0echo "Gateway stopped; saving latest OpenClaw state before restart..."python3 /home/node/app/openclaw-sync.py sync-once-settled || \echo "Warning: could not sync settled state before gateway restart"}start_background_sync_once() {[ -n "${HF_TOKEN:-}" ] || return 0if [ -n "$SYNC_LOOP_PID" ] && kill -0 "$SYNC_LOOP_PID" 2>/dev/null; thenreturn 0fipython3 -u /home/node/app/openclaw-sync.py loop &SYNC_LOOP_PID=$!}start_guardian_once() {[ "$WHATSAPP_ENABLED_NORMALIZED" = "true" ] || return 0if [ -n "$GUARDIAN_PID" ] && kill -0 "$GUARDIAN_PID" 2>/dev/null; thenreturn 0finode /home/node/app/wa-guardian.js &GUARDIAN_PID=$!echo "WhatsApp Guardian started (PID: $GUARDIAN_PID)"}while true; doecho "Launching OpenClaw gateway on port 7860..."GATEWAY_ARGS=(gateway run --port 7860 --bind lan)if [ "${GATEWAY_VERBOSE:-0}" = "1" ]; thenGATEWAY_ARGS+=(--verbose)echo "Gateway verbose logging enabled (GATEWAY_VERBOSE=1)"fi# Use stdbuf -oL -eL to ensure logs are not buffered and appear immediately# in the console. NOTE: $! captures the LAST pipeline element (tee), not# openclaw β fine for passing to `wait` (waits for the whole pipeline to# finish), but kill -0 on it is uninformative. We probe TCP instead.stdbuf -oL -eL openclaw "${GATEWAY_ARGS[@]}" 2>&1 | tee -a /home/node/.openclaw/gateway.log &GATEWAY_PID=$!# Poll for the gateway to start listening on 7860. OpenClaw can take 20-30s# on cold start (plugin install + auto-restore). Bail out early if the# pipeline died.GATEWAY_READY_TIMEOUT="${GATEWAY_READY_TIMEOUT:-90}"ready=falsefor ((i=0; i<GATEWAY_READY_TIMEOUT; i++)); doif (echo > /dev/tcp/127.0.0.1/7860) 2>/dev/null; thenready=truebreakfiif ! kill -0 "$GATEWAY_PID" 2>/dev/null; thenbreakfisleep 1doneif [ "$ready" != "true" ]; thenecho ""echo "Gateway failed to start. Last 30 lines of log:"echo "ββββββββββββββββββββββββββββββββββββββββββββ"tail -30 /home/node/.openclaw/gateway.logexit 1fi# 11. Start WhatsApp Guardian after the gateway is accepting connectionsstart_guardian_once# 11.5 Warm up the managed browser so first browser actions have a live tabwarmup_browser# 12. Start Workspace Sync after startup settles. Keep only one loop active;# config edits can make OpenClaw exit/reload, and the gateway loop below will# relaunch it without rerunning all startup code.start_background_sync_onceset +ewait "$GATEWAY_PID"GATEWAY_EXIT_CODE=$?set -esync_before_gateway_restartGATEWAY_RESTART_COUNT=$((GATEWAY_RESTART_COUNT + 1))if [ "$GATEWAY_MAX_RESTARTS" != "0" ] && [ "$GATEWAY_RESTART_COUNT" -ge "$GATEWAY_MAX_RESTARTS" ]; thenecho "Gateway exited with code ${GATEWAY_EXIT_CODE}; restart limit (${GATEWAY_MAX_RESTARTS}) reached."exit "$GATEWAY_EXIT_CODE"fiecho "Gateway exited with code ${GATEWAY_EXIT_CODE}; restarting in ${GATEWAY_RESTART_DELAY}s..."sleep "$GATEWAY_RESTART_DELAY"done
openclaw-sync.py+27-1
start.sh+94-45