browsertrix/backend/btrixcloud/operator/bgjobs.py
Tessa Walsh 589819682e
Optionally delay replica deletion (#2252)
Fixes #2170

The number of days to delay file replication deletion by is configurable
in the Helm chart with `replica_deletion_delay_days` (set by default to
7 days in `values.yaml` to encourage good practice, though we could
change this).

When `replica_deletion_delay_days` is set to an int above 0, when a
delete replica job would otherwise be started as a Kubernetes Job,
a CronJob is created instead with a cron schedule set to run yearly,
starting x days from the current moment. This cronjob is then deleted by
the operator after the job successfully completes. If a failed
background job is retried, it is re-run immediately as a Job rather
than being scheduled out into the future again.

---------
Co-authored-by: Ilya Kreymer <ikreymer@users.noreply.github.com>
2024-12-19 18:50:28 -08:00

67 lines
2.0 KiB
Python

""" Operator handler for BackgroundJobs """
from uuid import UUID
import traceback
from btrixcloud.utils import (
str_to_date,
dt_now,
)
from .models import MCDecoratorSyncData
from .baseoperator import BaseOperator
# ============================================================================
class BgJobOperator(BaseOperator):
"""BgJobOperator"""
def init_routes(self, app):
"""init routes for this operator"""
# nop, but needed for metacontroller
@app.post("/op/backgroundjob/sync")
async def mc_sync_background_jobs():
return {"attachments": []}
@app.post("/op/backgroundjob/finalize")
async def mc_finalize_background_jobs(data: MCDecoratorSyncData):
return await self.finalize_background_job(data)
async def finalize_background_job(self, data: MCDecoratorSyncData) -> dict:
"""handle finished background job"""
metadata = data.object["metadata"]
labels: dict[str, str] = metadata.get("labels", {})
oid: str = labels.get("btrix.org") or ""
job_type: str = labels.get("job_type") or ""
job_id: str = labels.get("job_id") or metadata.get("name")
status = data.object["status"]
success = status.get("succeeded") == 1
completion_time = status.get("completionTime")
finalized = True
finished = None
if completion_time:
finished = str_to_date(completion_time)
if not finished:
finished = dt_now()
try:
await self.background_job_ops.job_finished(
job_id, job_type, UUID(oid), success=success, finished=finished
)
# print(
# f"{job_type} background job completed: success: {success}, {job_id}",
# flush=True,
# )
# pylint: disable=broad-except
except Exception:
print("Update Background Job Error", flush=True)
traceback.print_exc()
return {"attachments": [], "finalized": finalized}