keeptabs.engine#

The scheduled run: work out what is due, acquire it, match it, store it.

tick() is idempotent and safe to call as often as you like. Whether a source is due is computed from stored state, so any trigger works (cron, launchd, a systemd timer, a loop) and a missed run is simply caught up by the next one.

Functions

due_sources(spec, state_store, *[, now])

The sources of a spec that should be fetched now.

fetch_source(spec, source, mall, *, ...)

Fetch one source and store what is new.

is_due(state, *, now)

Whether a source (or a digest) with this stored state should run now.

runnable_sources(spec)

The sources of a spec that are enabled and wait on no human action.

state_key(source)

The key of a source's record in the state store.

tick([watch_ids, rootdir, specs, malls, ...])

Run everything that is due, once, and say what happened.

tick_lock([rootdir])

Hold the local lock that keeps two ticks from running at once.

Exceptions

AlreadyRunning

Another tick holds the lock.

exception keeptabs.engine.AlreadyRunning[source]#

Bases: RuntimeError

Another tick holds the lock.

keeptabs.engine.due_sources(spec, state_store, *, now=None)[source]#

The sources of a spec that should be fetched now.

Return type:

list

keeptabs.engine.fetch_source(spec, source, mall, *, fetchers, http_get, matcher, now)[source]#

Fetch one source and store what is new. Returns the run record.

It never raises for a failing source: the failure is recorded, the source backs off, and the caller carries on.

Return type:

dict

keeptabs.engine.is_due(state, *, now)[source]#

Whether a source (or a digest) with this stored state should run now.

>>> now = to_datetime('2026-01-02')
>>> is_due({}, now=now), is_due({'next_due': '2026-01-03T00:00:00+00:00'}, now=now)
(True, False)
Return type:

bool

keeptabs.engine.runnable_sources(spec)[source]#

The sources of a spec that are enabled and wait on no human action.

Return type:

list

keeptabs.engine.state_key(source)[source]#

The key of a source’s record in the state store.

Return type:

str

keeptabs.engine.tick(watch_ids=None, *, rootdir=None, specs=None, malls=None, fetchers=None, http_get=None, matcher=None, on_digest_due=None, lock=None, now=None, force=False)[source]#

Run everything that is due, once, and say what happened.

  • specs: the store of watch specifications (default: local files under rootdir).

  • malls: a function from a watch id to that watch’s stores (default: local files). Stores of your own come in a pair: give both specs and malls.

  • fetchers: source kind to fetcher, added to the defaults.

  • http_get: the one door to the network.

  • matcher: match(item, spec, *, mall), scoring an item against a spec.

  • on_digest_due: called with (spec, mall, now) for each watch whose digest is due (default: keeptabs.digest.scheduled_digest()); False for no digest.

  • lock: a context manager held for the run, or False for none. The default is a local file lock, and none when the stores are your own.

  • force: run every enabled, unblocked source whatever its schedule.

A watch or a source that fails is reported in failures and does not stop the rest.

Return type:

dict

keeptabs.engine.tick_lock(rootdir=None)[source]#

Hold the local lock that keeps two ticks from running at once.

The operating system releases it when the process ends, however it ends.