# Class BSV::Wallet::Scheduler <a id="class-BSV-Wallet-Scheduler"></a>

**Inherits:** `Object`

Discovery loops for walletd background tasks.

Each loop queries the Store for pending work and pushes IDs to the appropriate
logical model's PULL socket. Runs as fibers inside the Daemon's Async reactor.

Provides a cooperative drain via {#shutdown} so the Daemon can wait for
in-flight broadcasts and proof acquisitions to settle before the Async reactor
exits. The drain is event-driven: an observer on
<code>BSV::Wallet.on_event</code> tracks <code>task.dispatched</code> minus
the four terminal events (<code>task.succeeded</code> / <code>.failed</code> /
<code>.aborted</code> / <code>.skipped</code>). When the counter reaches zero
the system is quiesced.

## Constants
### `DEFAULT_SHUTDOWN_TIMEOUT_S` <a id="constant-DEFAULT_SHUTDOWN_TIMEOUT_S"></a> <a id="DEFAULT_SHUTDOWN_TIMEOUT_S-constant"></a>
Not documented.

### `REAP_INTERVAL_S` <a id="constant-REAP_INTERVAL_S"></a> <a id="REAP_INTERVAL_S-constant"></a>
Reaper cadence. Cleanup is not latency-sensitive — run it slowly, well off the
broadcast hot path. The staleness threshold lives in Config (the load-bearing
knob, keyed to the delayed_broadcast lifecycle); the limit bounds reclaim per
pass so a backlog drains in batches.

### `REAP_LIMIT` <a id="constant-REAP_LIMIT"></a> <a id="REAP_LIMIT-constant"></a>
Not documented.

### `SHUTDOWN_POLL_INTERVAL_S` <a id="constant-SHUTDOWN_POLL_INTERVAL_S"></a> <a id="SHUTDOWN_POLL_INTERVAL_S-constant"></a>
Not documented.

### `TERMINAL_EVENTS` <a id="constant-TERMINAL_EVENTS"></a> <a id="TERMINAL_EVENTS-constant"></a>
Not documented.

## Public Instance Methods
### `in_flight()` <a id="method-i-in_flight"></a> <a id="in_flight-instance_method"></a>
Current count of dispatched-but-not-terminal tasks. Thread-safe.

### `initialize(store:)` <a id="method-i-initialize"></a> <a id="initialize-instance_method"></a>
- **@return** [Scheduler] a new instance of Scheduler

### `run!(task:)` <a id="method-i-run-21"></a> <a id="run!-instance_method"></a>
Not documented.

### `shutdown(timeout: = DEFAULT_SHUTDOWN_TIMEOUT_S)` <a id="method-i-shutdown"></a> <a id="shutdown-instance_method"></a>
Cooperative drain. Stops the discovery loops from enqueuing new work and waits
for in-flight tasks to reach a terminal lifecycle event
(<code>task.succeeded</code> / <code>.failed</code> / <code>.aborted</code> /
<code>.skipped</code>).

Returns when the in-flight counter hits zero OR `timeout` seconds elapse.
Truthy return = clean drain; `false` = timed out.

Always idempotent — calling shutdown when already stopped returns immediately
with `true`.
- **@param** `timeout` [Float] seconds to wait for the drain to complete
- **@return** [Boolean] true if drained cleanly, false on timeout

### `stopping?()` <a id="method-i-stopping-3F"></a> <a id="stopping?-instance_method"></a>
True once {#shutdown} has been called. Discovery loops check this flag and
exit when set.
- **@return** [Boolean]
