File size: 5,816 Bytes
47091a0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
"""
backup_watchdog.py -- Polling watchdog that snapshots on change.

Complements the PostToolUse hook: the hook catches edits that happen
*inside* a Claude Code session, while the watchdog catches anything
else (manual IDE edits, git pull, external tooling) by polling
``snapshot_if_changed`` on a fixed interval.

Design
------

Zero dependencies — a simple loop that sleeps and calls into
:func:`backup_mirror.snapshot_if_changed`. Change detection is already
SHA-256-gated, so polling is cheap even at 30–60 s intervals.

The loop is fully testable because:

- the clock is injected (``sleeper`` / ``now``);
- the iteration count is capped (``max_iterations``);
- SIGINT / SIGTERM flip a shared flag rather than raise mid-snapshot.

Install
-------

Run it by hand::

    python src/backup_mirror.py watchdog --interval 60

Or register it as a background service using whatever init system fits
(Task Scheduler on Windows, systemd on Linux, launchd on macOS). See
``docs/backup-hook-install.md``.

Exit codes:
    0  stopped cleanly (SIGINT / SIGTERM / max_iterations reached)
    2  unrecoverable config error (e.g. BACKUPS_DIR not writable)
"""

from __future__ import annotations

import signal
import sys
import time
from dataclasses import dataclass, field
from types import FrameType
from typing import Any, Callable


# Clamp so a mis-configured interval cannot turn the watchdog into a
# busy loop or a near-dead process.
_MIN_INTERVAL_SEC = 5
_MAX_INTERVAL_SEC = 3600
_SignalHandler = Callable[[int, FrameType | None], Any] | int | signal.Handlers | None


@dataclass
class WatchdogStats:
    """Counters the loop emits to stderr on shutdown."""

    ticks: int = 0
    snapshots_taken: int = 0
    errors: int = 0
    # Snapshot IDs created during this run, newest last.
    snapshot_ids: list[str] = field(default_factory=list)

    def to_dict(self) -> dict:
        return {
            "ticks": self.ticks,
            "snapshots_taken": self.snapshots_taken,
            "errors": self.errors,
            "snapshot_ids": list(self.snapshot_ids),
        }


class _StopFlag:
    """Tiny holder so the signal handler can flip shared state."""

    def __init__(self) -> None:
        self.stop = False

    def set(self, *_args: object) -> None:  # signal handler signature
        self.stop = True


def _install_signal_handlers(flag: _StopFlag) -> list[tuple[int, _SignalHandler]]:
    """Install SIGINT/SIGTERM handlers, return previous so we can restore."""
    previous: list[tuple[int, _SignalHandler]] = []
    for sig in (signal.SIGINT, signal.SIGTERM):
        try:
            prev = signal.signal(sig, flag.set)
            previous.append((sig, prev))
        except (OSError, ValueError):
            # Non-main thread, or signal not supported on this platform;
            # watchdog still works, it just won't stop on that signal.
            pass
    return previous


def _restore_signal_handlers(previous: list[tuple[int, _SignalHandler]]) -> None:
    for sig, prev in previous:
        try:
            signal.signal(sig, prev)
        except (OSError, ValueError, TypeError):
            pass


def run_watchdog(
    *,
    interval: float = 60.0,
    reason_prefix: str = "watchdog",
    max_iterations: int | None = None,
    sleeper: Callable[[float], None] = time.sleep,
    log: Callable[[str], None] | None = None,
) -> WatchdogStats:
    """Run the polling watchdog loop.

    Parameters
    ----------
    interval
        Seconds between polls. Clamped to ``[5, 3600]``.
    reason_prefix
        Prefix for the ``--reason`` label on every snapshot.
    max_iterations
        When set, stop after N polls. Lets tests exit deterministically
        without relying on signals or wall-clock time.
    sleeper
        Injectable sleep function. Tests pass a no-op.
    log
        Injectable writer for diagnostic lines. Defaults to stderr.
    """
    # Intentional import cycle with backup_mirror (see the matching
    # comment at the top of that module). backup_mirror.cmd_watchdog
    # calls run_watchdog, and run_watchdog calls back into
    # backup_mirror.snapshot_if_changed — the call graph is cyclic by
    # design. Importing lazily here keeps the module import graph
    # acyclic so both modules load in either order.
    from backup_mirror import snapshot_if_changed  # noqa: PLC0415

    def emit(msg: str) -> None:
        if log is not None:
            log(msg)
        else:
            print(msg, file=sys.stderr)

    interval = max(_MIN_INTERVAL_SEC, min(_MAX_INTERVAL_SEC, float(interval)))
    stop = _StopFlag()
    previous = _install_signal_handlers(stop)
    stats = WatchdogStats()

    emit(f"[watchdog] start interval={interval:.1f}s prefix={reason_prefix!r}")

    try:
        while not stop.stop:
            stats.ticks += 1
            reason = f"{reason_prefix}:tick{stats.ticks}"
            try:
                result = snapshot_if_changed(reason=reason)
            except Exception as exc:  # noqa: BLE001  (never kill the loop)
                stats.errors += 1
                emit(f"[watchdog] tick {stats.ticks} error: {exc!r}")
            else:
                if result.snapshot_path is not None:
                    stats.snapshots_taken += 1
                    stats.snapshot_ids.append(result.snapshot_path.name)
                    emit(f"[watchdog] tick {stats.ticks} snapshot "
                         f"{result.snapshot_path.name}")

            if max_iterations is not None and stats.ticks >= max_iterations:
                break
            if stop.stop:
                break
            sleeper(interval)
    finally:
        _restore_signal_handlers(previous)

    emit(f"[watchdog] stop ticks={stats.ticks} "
         f"snapshots={stats.snapshots_taken} errors={stats.errors}")
    return stats