s365b.jobs.liveness

Whether a job run is really still running.

A run reports progress every few seconds (see s365b.jobs.progress). A run that has gone quiet belongs to a worker that stopped (restart, crash, out of memory): it will never finish, so it is marked interrupted rather than shown as running forever. Work it committed is kept, and the next run continues from its last checkpoint.

Attributes

Functions

heartbeat(db, run_id[, every]) → collections.abc.AsyncIterator[None]

Keep a run marked live while work without progress reports runs.

last_heartbeat(run) → datetime.datetime | None

When the run last reported progress (or started, if it never did).

is_live(run[, now]) → bool

Whether a running run has reported progress recently.

live_run(s, job_id, *[, other_than]) → s365b.db.models.JobRun | None

Return a live run of a job, if there is one.

reap_stale_runs(db[, now]) → list[int]

Mark runs whose worker is gone as failed.

Module Contents

s365b.jobs.liveness.LIVE_WITHIN[source]
s365b.jobs.liveness.STALE_AFTER[source]
s365b.jobs.liveness.INTERRUPTED = 'Interrupted: the worker stopped during this run (restart or crash). What was stored is kept;...[source]
s365b.jobs.liveness.INTERRUPTED_RESTORE = 'Interrupted: the worker stopped during this restore (restart or crash). What was restored...[source]
s365b.jobs.liveness.HEARTBEAT_EVERY[source]
async s365b.jobs.liveness.heartbeat(db: s365b.db.session.Database, run_id: int, every: datetime.timedelta = HEARTBEAT_EVERY) → collections.abc.AsyncIterator[None][source]

Keep a run marked live while work without progress reports runs.

For runs that have no progress to show (retention): only the updated_at of their progress is refreshed.

Parameters:
  • db – Database.

  • run_id – The running run.

  • every – How often.

s365b.jobs.liveness.last_heartbeat(run: s365b.db.models.JobRun) → datetime.datetime | None[source]

When the run last reported progress (or started, if it never did).

Parameters:

run – The run.

Returns:

The time, or None if it has not started.

s365b.jobs.liveness.is_live(run: s365b.db.models.JobRun, now: datetime.datetime | None = None) → bool[source]

Whether a running run has reported progress recently.

Parameters:
  • run – The run.

  • now – Current time.

Returns:

True if it is running and was heard from within LIVE_WITHIN.

async s365b.jobs.liveness.live_run(s: sqlalchemy.ext.asyncio.AsyncSession, job_id: int, *, other_than: int | None = None) → s365b.db.models.JobRun | None[source]

Return a live run of a job, if there is one.

Parameters:
  • s – Database session.

  • job_id – The job.

  • other_than – A run to ignore (the caller’s own).

Returns:

The live run, or None.

async s365b.jobs.liveness.reap_stale_runs(db: s365b.db.session.Database, now: datetime.datetime | None = None) → list[int][source]

Mark runs whose worker is gone as failed.

Parameters:
  • db – Database.

  • now – Current time.

Returns:

IDs of the runs marked.