All files / backend/dist/jobs job-manager.js

15% Statements 3/20
0% Branches 0/4
12.5% Functions 1/8
16.66% Lines 3/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67                          1x 1x                                                                                                     1x  
import { BackgroundJobRunner } from './background-job-runner.js';
/**
 * JobsManager handles job execution with caching and deduplication.
 * Shared jobs are cached so multiple callers get the same result.
 */
export class JobsManager {
    backgroundJobRunner;
    // Using `any` here because this map caches promises with different result types.
    // The generic type T varies per job. Type safety is enforced at the API boundary
    // via the generic runSharedJob<T>() and runSharedBackgroundJob<T>() methods.
    // eslint-disable-next-line @typescript-eslint/no-explicit-any
    runningActions;
    constructor() {
        this.backgroundJobRunner = new BackgroundJobRunner();
        this.runningActions = new Map();
    }
    /**
     * Run a shared job that can be called by multiple consumers.
     * If the job is already running, returns the existing promise.
     */
    runSharedJob(key, job) {
        let promise = this.runningActions.get(key);
        if (promise == null) {
            promise = job();
            promise.finally(() => this.runningActions.delete(key));
            this.runningActions.set(key, promise);
        }
        return promise;
    }
    /**
     * Run a single background job in a worker thread.
     * Each call creates a new worker.
     */
    runBackgroundJob(key, resolvedPath, payload) {
        return this.backgroundJobRunner.run(resolvedPath, payload);
    }
    /**
     * Run a shared background job in a worker thread.
     * If the job is already running, returns the existing promise.
     */
    runSharedBackgroundJob(key, resolvedPath, payload) {
        let promise = this.runningActions.get(key);
        if (promise == null) {
            promise = this.backgroundJobRunner.run(resolvedPath, payload);
            promise.finally(() => this.runningActions.delete(key));
            this.runningActions.set(key, promise);
        }
        return promise;
    }
    /**
     * Run a shared debounced background job.
     * Maximum number of times it can be called over time.
     */
    runSharedDebouncedBackgroundJob() {
        throw new Error('runSharedDebouncedJob is not implemented.');
    }
    /**
     * Run a shared throttled background job.
     * Cannot be called again until a certain amount of time has passed.
     */
    runSharedThrottledBackgroundJob() {
        throw new Error('runSharedThrottledJob is not implemented.');
    }
}
// Export singleton instance
export const jobsManager = new JobsManager();