25 lines
835 B
TypeScript
25 lines
835 B
TypeScript
import type { WorkerActivity } from '../../provisioning-detail.js';
|
|
import { currentOperationSource, withOperationSource } from '../hosts/operation-context.js';
|
|
|
|
const active = new Map<string, WorkerActivity>();
|
|
let sequence = 0;
|
|
|
|
/** Live diagnostics only. Durable operation milestones belong in provisioning history. */
|
|
export function workerActivity(): WorkerActivity[] {
|
|
return [...active.values()].map((value) => ({ ...value }));
|
|
}
|
|
|
|
export async function trackActivity<T>(
|
|
key: string,
|
|
label: string,
|
|
work: () => Promise<T>
|
|
): Promise<T> {
|
|
if (active.has(key)) key = `${key}:${++sequence}`;
|
|
active.set(key, { key, label, startedAt: new Date().toISOString() });
|
|
try {
|
|
return await withOperationSource(currentOperationSource() ?? label, work);
|
|
} finally {
|
|
active.delete(key);
|
|
}
|
|
}
|