lablink_allocator_service.db.operations¶
lablink_allocator_service.db.operations
¶
Persistence for the operations table (on-demand apply/destroy jobs).
A standalone class rather than more methods on VmDatabase: the operations table has no foreign-key or column coupling to the vms table (unlike the AdminReservedAt admin-reservation columns, which reach directly into VM rows), so there's no reason for it to share VmDatabase's god-class surface. It shares the same connection pool (see VmDatabase.pool) rather than opening a second one, since POOL_MAX_SIZE is already tuned for this allocator's total connection budget.
Attributes¶
logger = logging.getLogger(__name__)
module-attribute
¶
Classes¶
OperationInProgress(job_id)
¶
Bases: Exception
Raised by create_operation when another operation is already queued/running — the operations_single_flight partial unique index was violated.
Source code in packages/allocator/src/lablink_allocator_service/db/operations.py
OperationsDatabase(pool)
¶
Persistence for the operations table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pool
|
A psycopg2 connection pool, shared with VmDatabase (see VmDatabase.pool) rather than owned here. |
required |
Source code in packages/allocator/src/lablink_allocator_service/db/operations.py
Methods:¶
create_operation(op_type, params=None, created_by=None)
¶
Create a queued operation and return its ID.
Raises:
| Type | Description |
|---|---|
OperationInProgress
|
if another operation is already queued/running (operations_single_flight guard). |
RuntimeError
|
if the INSERT fails on the single-flight guard but the in-progress operation has since vanished (an unusual race between the failed insert and the get_in_progress_operation lookup, e.g. the colliding operation finished in between). |
Source code in packages/allocator/src/lablink_allocator_service/db/operations.py
finish_operation(operation_id, status, output=None, error=None)
¶
Mark an operation succeeded or failed. A successful finish also snaps resources_completed to resources_total (if a total was ever recorded), so a completed job never displays a stale partial count from a missed final progress update.
Source code in packages/allocator/src/lablink_allocator_service/db/operations.py
get_in_progress_operation()
¶
Return the currently queued/running operation, if any.
The operations_single_flight partial unique index guarantees at most one row can match; LIMIT 1 is defensive, not load-bearing.
Source code in packages/allocator/src/lablink_allocator_service/db/operations.py
get_operation(operation_id)
¶
Get an operation by ID.
Source code in packages/allocator/src/lablink_allocator_service/db/operations.py
list_operations(limit=50)
¶
List recent operations, newest first.
Source code in packages/allocator/src/lablink_allocator_service/db/operations.py
start_operation(operation_id)
¶
Mark an operation running.
Source code in packages/allocator/src/lablink_allocator_service/db/operations.py
sweep_interrupted_operations()
¶
Mark any queued/running operation as interrupted.
Called once at allocator startup: a row still queued/running means the allocator process died mid-job last time, so the OpenTofu subprocess died with it. Returns the number of rows affected.
Source code in packages/allocator/src/lablink_allocator_service/db/operations.py
update_operation_progress(operation_id, completed, total)
¶
Update an operation's incremental resource-completion progress.
Called from OperationsWorker as resources finish creating/
destroying during a launch/destroy job still in running status.