Skip to content

tdfs4ds.serve.activity

tdfs4ds.serve.activity

What the admin app is doing in the background, for the status bar (0.3.3.15).

A process-wide, thread-safe registry of jobs: each has a key, a label, an optional done / total count and a one-line detail. Nothing here touches the database. GET /ui/status reads snapshot().

Two ways in:

  • start(key, label) returns a handle that an existing worker updates (update) and closes (finish) -- used around the streamed panel jobs (maintenance, documentation, migration, evaluation, benchmark).
  • run_in_background(key, label, fn) runs fn(handle) in a daemon thread. A job whose key is already running is not started twice: one re-run is queued and starts when the running one ends (two builds of one store's index at once would race on the same collections).

Finished jobs stay visible for KEEP_FINISHED_S seconds so the bar can say "done" or show the error, whole.

logger module-attribute

logger = logging.getLogger(__name__)

KEEP_FINISHED_S module-attribute

KEEP_FINISHED_S = 60.0

_LOCK module-attribute

_LOCK = threading.Lock()

_JOBS module-attribute

_JOBS: dict = {}

_RUNNING module-attribute

_RUNNING: dict = {}

_QUEUED module-attribute

_QUEUED: dict = {}

_HOOKS module-attribute

_HOOKS: dict = {}

_CANCELLING module-attribute

_CANCELLING: set = set()

_IDS module-attribute

_IDS = itertools.count(1)

_REQUESTS module-attribute

_REQUESTS = {'n': 0}

_CURRENT_REQUEST module-attribute

_CURRENT_REQUEST: ContextVar = contextvars.ContextVar('tdfs4ds_request_activity', default=None)

ROUTE_LABELS module-attribute

ROUTE_LABELS = {'/ui/dashboard/report': 'Overview report', '/ui/dashboard/storage': 'Storage counts', '/ui/dashboard/orphans': 'Orphan scan', '/ui/dashboard': 'Overview', '/ui/workflow': 'Health check', '/ui/efs': 'Store map', '/ui/catalog': 'Feature catalog', '/ui/lineage': 'Lineage', '/ui/doc/coverage': 'Documentation coverage', '/ui/doc': 'Documentation', '/ui/runs': 'Runs', '/ui/sizing': 'Sizing', '/ui/timemgr': 'Time managers', '/ui/framework': 'Framework cost', '/ui/params': 'Parameters', '/ui/home': 'Home', '/ui/dsbuild': 'Dataset builder', '/ui/parallel': 'Execution plan', '/ui/migration': 'Migration', '/ui/attached': 'Attached objects', '/ui/glossary': 'Glossary', '/ui/datasets': 'Datasets', '/ui/impact': 'Impact study', '/ui/qopt': 'Query optimization', '/ui/benchmark': 'Benchmark', '/ui/maintenance': 'Maintenance', '/ui/search': 'Search'}

_CURRENT_ROUTE module-attribute

_CURRENT_ROUTE: ContextVar = contextvars.ContextVar('tdfs4ds_request_route', default=('', ''))

Handle

A job's handle: update while it runs, finish once.

id instance-attribute

id = job_id

__init__

__init__(job_id: int)

set_cancel

set_cancel(fn: Optional[Callable[[], object]], profile: Optional[str] = None) -> None

Say how the job can be stopped (fn() stops it; None = it cannot), and the profile needed to stop it. Jobs without a hook show no Cancel button.

update

update(done=None, total=None, detail=None) -> None

finish

finish(error: Optional[str] = None, detail: Optional[str] = None, cancelled: bool = False) -> None

End the job: failed with error, cancelled when cancelled (a person or a time limit stopped it), else done.

__enter__

__enter__()

__exit__

__exit__(exc_type, exc, tb)

RequestState

What the middleware keeps for one request: the step text, and the activity once there is one.

step instance-attribute

step = ''

handle instance-attribute

handle: Optional[Handle] = None

__init__

__init__()

set_step

set_step(text: str) -> None

promote

promote(key: str, label: str, path: str) -> Handle

The request is slow: show it as an activity (with the step known so far).

request_started

request_started() -> None

A /ui request that may read the database began (see requests_in_flight).

request_done

request_done() -> None

requests_in_flight

requests_in_flight() -> int

How many panel requests run now. A reconnection replaces teradataml's one process-wide connection, so the health monitor must not do it under them: a request cut that way fails with 'NoneType' object has no attribute 'cursor' (seen with several browser sessions).

_describe

_describe(exc) -> str

start

start(key: str, label: str, kind: str = 'job', path: str = '', cancel: Optional[Callable[[], object]] = None, profile: str = 'analyst') -> Handle

Register a running job and return its handle. kind is "job" or "request" (an HTTP request still running after a second, see :func:request_activity).

cancel (or handle.set_cancel) is a callable that stops the job; profile is the profile a person needs to stop it (see :func:cancel).

cancel

cancel(job_id, caller_profile: Optional[str] = None) -> dict

Stop a running job through its hook, once. Never raises.

Returns {'ok', 'status'}. An unknown or finished id, a job with no hook, a job already being cancelled and a caller whose profile is below the job's all answer with a message and call nothing. caller_profile None = no check (no login gate: everybody is admin).

cancel_all

cancel_all(caller_profile: Optional[str] = None) -> dict

:func:cancel for every running job that has a hook (a job above the caller's profile is skipped and counted). Never raises.

route_label

route_label(path: str) -> str

A readable label for a /ui/... path (the path itself when unknown).

set_route

set_route(method: str, path: str)

reset_route

reset_route(token) -> None

current_route_profile

current_route_profile(default: str = 'analyst') -> str

The profile the route serving this request needs (auth.required_profile), default outside a request. Used for the profile a person needs to cancel a job this request started.

begin_request

begin_request()

Set the current request's state (a ContextVar copied into worker threads). Returns (state, token); pass the token to :func:end_request.

end_request

end_request(state, token, error: Optional[str] = None) -> None

step

step(text: str) -> None

Say what the current request is doing, e.g. activity.step("reading the lineage graph").

Shown in the status bar next to the request. A no-op outside a request (tests, notebooks, background jobs) and never raises. Safe to call from the worker threads of a request.

is_running

is_running(key: str) -> bool

run_in_background

run_in_background(key: str, label: str, fn: Callable[[Handle], object]) -> str

Run fn(handle) in a daemon thread under key.

Returns 'started', or 'queued' when a job with the same key is running (one re-run then follows it; queueing again replaces the waiting one, which is the same work).

_launch

_launch(key, label, fn)

snapshot

snapshot(now: Optional[float] = None) -> list

Running jobs (oldest first) then jobs finished in the last KEEP_FINISHED_S seconds (newest first). Older finished jobs are dropped.

_reset

_reset() -> None

Forget every job (tests).