s365b.jobs.runner

Running backup jobs.

JobRunner contains the logic; s365b.jobs.tasks only wraps it in Procrastinate tasks. Keeping them apart lets tests run jobs directly.

Attributes

log

Exceptions

JobSetupError

A job cannot start (no tenant connected, repository missing…).

Classes

JobRunner

Executes backup jobs.

Functions

selected(rows, rules) → list[s365b.db.models.ProtectedObjectRow]

Apply a job's include/exclude rules for one module.

is_due(schedule, at) → bool

Return whether a cron schedule fires at the given minute.

copies_warning(missing, error) → str

The warning for items without a restore copy (an extra copy).

discover_objects(services, module_id) → list[s365b.db.models.ProtectedObjectRow]

Discover a module's objects in the tenant now and update the catalog.

Module Contents

s365b.jobs.runner.log[source]
exception s365b.jobs.runner.JobSetupError[source]

Bases: Exception

A job cannot start (no tenant connected, repository missing…).

s365b.jobs.runner.selected(rows: list[s365b.db.models.ProtectedObjectRow], rules: dict[str, Any] | None) → list[s365b.db.models.ProtectedObjectRow][source]

Apply a job’s include/exclude rules for one module.

Rules: {"include": "all" | [external_id, ...], "exclude": [external_id, ...]}. No rules means everything.

Parameters:
  • rows – Discovered objects.

  • rules – The module’s selection rules from the job.

Returns:

The objects to back up.

s365b.jobs.runner.is_due(schedule: str, at: datetime.datetime) → bool[source]

Return whether a cron schedule fires at the given minute.

Parameters:
  • schedule – Cron expression, e.g. "0 22 * * *".

  • at – The minute to test.

Returns:

True if the schedule matches.

s365b.jobs.runner.copies_warning(missing: int, error: str | None) → str[source]

The warning for items without a restore copy (an extra copy).

Parameters:
  • missing – Current items without the copy.

  • error – Why the last attempt stopped, if it failed.

Returns:

A sentence for the run details and the alert email.

class s365b.jobs.runner.JobRunner(services: s365b.core.services.Services)[source]

Executes backup jobs.

Parameters:

services – Process services.

progress_interval = 3.0[source]
services[source]
async run_backup_job(job_id: int, *, requested_by: str = 'scheduler') → int[source]

Run one backup job to completion.

Never raises for problems inside the job: they are recorded on the job run and as events.

Parameters:
  • job_id – Backup job ID.

  • requested_by – Who started it (scheduler or a username).

Returns:

The job run ID.

check_permissions(job: s365b.db.models.BackupJob, app: s365b.db.models.AppRegistration, enabled: dict[str, dict[str, Any]]) → None[source]

Fail early if the Backup app lacks permissions the job’s modules need.

Without this every protected object fails with the same 403.

Parameters:
  • job – The job.

  • app – The Backup app registration.

  • enabled – Raw configs of the enabled modules.

Raises:

JobSetupError – Naming the missing permissions and the fix.

async due_jobs(at: datetime.datetime) → list[int][source]

Return the IDs of enabled jobs whose schedule fires at at.

Parameters:

at – The minute being scheduled (server time zone).

Returns:

Job IDs.

async s365b.jobs.runner.discover_objects(services: s365b.core.services.Services, module_id: str) → list[s365b.db.models.ProtectedObjectRow][source]

Discover a module’s objects in the tenant now and update the catalog.

Parameters:
  • services – Process services.

  • module_id – The module.

Returns:

The discovered objects.

Raises:

JobSetupError – If no tenant or Backup app is set up.