prefactor_core.monitoring package
prefactor_core.monitoring package
Section titled “prefactor_core.monitoring package”class prefactor_core.monitoring.TerminationMonitor(fetch_instance: Callable, poll_interval: float = 30.0)
Section titled “class prefactor_core.monitoring.TerminationMonitor(fetch_instance: Callable, poll_interval: float = 30.0)”Bases: object
Monitors an agent instance for termination.
Thread-safety note: detect_termination and get_termination_event().is_set() are safe to call from sync worker threads — they only read/write a bool under the GIL (asyncio.Event._value is a plain bool).
destroy() → None
Section titled “destroy() → None”Permanently shut down the monitor (no further events will fire).
detect_termination(reason: str | None) → None
Section titled “detect_termination(reason: str | None) → None”Signal that the instance has been terminated.
No-op if already terminated, destroyed, or fenced (post-reset stale call).
get_termination_event() → Event
Section titled “get_termination_event() → Event”reset() → None
Section titled “reset() → None”Prepare monitor for the next agent run.
- Creates a fresh (unset) event
- Clears the termination reason
- Cancels any in-flight poll
- Sets fence so stale callbacks from the dying run are ignored
- Increments generation so stale polls self-discard
subscribe(callback: Callable[[], None]) → Callable[[], None]
Section titled “subscribe(callback: Callable[[], None]) → Callable[[], None]”Register a callback invoked on termination. Returns an unsubscribe fn.
sync(instance_id: str | None) → None
Section titled “sync(instance_id: str | None) → None”Update the tracked instance ID and start/stop the fallback poll.
Idempotent: calling with the same non-None ID when a poll is already running does not restart it (preserving the sleep interval).