"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.__tsfTypeAliases = exports.WorkerQueueRegistry = void 0;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-server-foundation/type-metadata-runtime"));
const redis_1 = require("../../helpers/redis/redis");
const observer_1 = require("./observer");
const bullmq_1 = require("bullmq");
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":8},{\"kind\":6},{\"kind\":2,\"typeName\":\"JobClass\"},{\"kind\":18,\"typeName\":\"IJobOptions\",\"types\":[{\"kind\":20,\"name\":\"delay\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"queueName\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runInTest\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runImmediately\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"recordToDatabase\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"repeatKey\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]},{\"kind\":2,\"typeName\":\"JobClass\",\"typeArguments\":[{\"kind\":2,\"typeName\":\"I\"}]},{\"kind\":2,\"typeName\":\"I\"},{\"kind\":2},{\"kind\":3},{\"kind\":12,\"types\":[{\"kind\":2,\"typeName\":\"I\"},{\"kind\":5},{\"kind\":4}]},{\"kind\":22,\"type\":{\"kind\":3}}]]", []);
let _WorkerQueueRegistryMetadata_1;
class WorkerQueueRegistry {
    config;
    static queues = new Map();
    static bullQueues = new Set();
    static nextId = 1;
    bullQueues = new Map();
    constructor(config) {
        this.config = config;
    }
    usesBullMq() {
        if (this.config?.APP_ENV === 'test')
            return false;
        return true;
    }
    getDefaultQueueName() {
        return this.config?.BULL_QUEUE ?? 'default';
    }
    getQueueName(jobClass, options = {}) {
        return options.queueName ?? getOwnQueueName(jobClass) ?? this.getDefaultQueueName();
    }
    async enqueue(jobClass, data, options = {}) {
        if (!this.usesBullMq())
            return this.add(jobClass, data, options);
        const queueName = this.getQueueName(jobClass, options);
        const bullJob = await this.getBullQueue(queueName).add(jobClass.name, {
            data,
            options: { ...options }
        }, {
            delay: options.delay,
            removeOnComplete: 1000,
            removeOnFail: 1000
        });
        const job = this.fromBullJob(bullJob, jobClass);
        (0, observer_1.notifyWorkerObservers)({
            type: job.shouldExecuteAt.getTime() > Date.now() ? 'delayed' : 'added',
            job
        });
        return job;
    }
    add(jobClass, data, options = {}) {
        const queue = this.getQueueName(jobClass, options);
        const createdAt = new Date();
        const job = {
            id: String(WorkerQueueRegistry.nextId++),
            queue,
            name: jobClass.name,
            data,
            jobClass,
            options: { ...options },
            createdAt,
            shouldExecuteAt: new Date(createdAt.getTime() + (options.delay ?? 0)),
            attemptsMade: 0,
            status: 'queued'
        };
        const jobs = WorkerQueueRegistry.queues.get(queue) ?? [];
        jobs.push(job);
        WorkerQueueRegistry.queues.set(queue, jobs);
        (0, observer_1.notifyWorkerObservers)({
            type: job.shouldExecuteAt.getTime() > Date.now() ? 'delayed' : 'added',
            job
        });
        return job;
    }
    getQueuedJobs(queue = this.getDefaultQueueName()) {
        return [...(WorkerQueueRegistry.queues.get(queue) ?? [])];
    }
    getAllQueuedJobs() {
        return [...WorkerQueueRegistry.queues.values()].flat();
    }
    markCompleted(job, result) {
        job.status = 'completed';
        job.result = result;
    }
    markFailed(job, result) {
        job.status = 'failed';
        job.result = result;
    }
    remove(job) {
        const jobs = WorkerQueueRegistry.queues.get(job.queue);
        if (!jobs)
            return;
        const remaining = jobs.filter(item => item !== job);
        if (remaining.length)
            WorkerQueueRegistry.queues.set(job.queue, remaining);
        else
            WorkerQueueRegistry.queues.delete(job.queue);
    }
    clear(queue) {
        if (queue)
            WorkerQueueRegistry.queues.delete(queue);
        else
            WorkerQueueRegistry.queues.clear();
    }
    getBullQueue(queueName) {
        const existing = this.bullQueues.get(queueName);
        if (existing)
            return existing;
        const queue = new bullmq_1.Queue(queueName, this.getBullMqOptions());
        this.bullQueues.set(queueName, queue);
        WorkerQueueRegistry.bullQueues.add(queue);
        return queue;
    }
    getBullQueues() {
        return [...this.bullQueues.entries()].map(([name, queue]) => ({ name, queue }));
    }
    normalizeJobData(data) {
        return (data === undefined ? {} : data);
    }
    fromBullJob(job, jobClass) {
        const createdAt = new Date(job.timestamp);
        const delay = typeof job.delay === 'number' ? job.delay : Number(job.opts.delay ?? 0);
        const payload = this.unwrapBullMqJobData(job.data);
        return {
            id: String(job.id ?? ''),
            queue: job.queueName,
            name: job.name,
            data: this.normalizeJobData(payload.data),
            jobClass,
            options: { ...payload.options },
            createdAt,
            shouldExecuteAt: new Date(createdAt.getTime() + delay),
            attemptsMade: job.attemptsMade,
            status: 'queued'
        };
    }
    async shutdown() {
        await Promise.all([...this.bullQueues.values()].map(queue => queue.close().catch(() => { })));
        for (const queue of this.bullQueues.values())
            WorkerQueueRegistry.bullQueues.delete(queue);
        this.bullQueues.clear();
    }
    static async closeQueues() {
        WorkerQueueRegistry.queues.clear();
        const queues = [...WorkerQueueRegistry.bullQueues];
        WorkerQueueRegistry.bullQueues.clear();
        await Promise.all(queues.map(queue => queue.close().catch(() => { })));
    }
    getBullMqOptions() {
        const config = this.config;
        if (!config) {
            throw new Error('BullMQ workers require app config');
        }
        let options;
        let prefix;
        try {
            ({ options, prefix } = (0, redis_1.createRedisOptions)('BULL'));
        }
        catch (error) {
            throw new Error('BullMQ workers require REDIS_HOST or REDIS_SENTINEL_HOST (or BULL_REDIS_HOST/BULL_REDIS_SENTINEL_HOST) to be configured', { cause: error });
        }
        return {
            connection: { ...options, maxRetriesPerRequest: null },
            prefix: `${prefix}:bmq`
        };
    }
    unwrapBullMqJobData(raw) {
        if (isRecord(raw) && isWorkerPayloadWrapper(raw)) {
            const wrapped = raw;
            return {
                data: wrapped.data,
                options: isRecord(wrapped.options) ? { ...wrapped.options } : {}
            };
        }
        return { data: raw, options: {} };
    }
    static __tsfType = _WorkerQueueRegistryMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"WorkerQueueRegistry\",\"typeName\":\"WorkerQueueRegistry\",\"classType\":{\"$tsf\":0},\"properties\":[],\"methods\":[{\"name\":\"usesBullMq\",\"parameters\":[],\"returnType\":{\"$tsfType\":0}},{\"name\":\"getDefaultQueueName\",\"parameters\":[],\"returnType\":{\"$tsfType\":1}},{\"name\":\"getQueueName\",\"parameters\":[{\"name\":\"jobClass\",\"type\":{\"$tsfType\":2},\"optional\":false,\"default\":false},{\"name\":\"options\",\"type\":{\"$tsfType\":3},\"optional\":true,\"default\":true}],\"returnType\":{\"$tsfType\":1}},{\"name\":\"enqueue\",\"parameters\":[{\"name\":\"jobClass\",\"type\":{\"$tsfType\":4},\"optional\":false,\"default\":false},{\"name\":\"data\",\"type\":{\"$tsfType\":5},\"optional\":false,\"default\":false},{\"name\":\"options\",\"type\":{\"$tsfType\":3},\"optional\":true,\"default\":true}],\"returnType\":{\"kind\":22,\"type\":{\"kind\":16,\"typeName\":\"QueuedWorkerJob\",\"classType\":{\"$tsfImport\":[1,\"./types\",\"QueuedWorkerJob\"]},\"typeArguments\":[{\"kind\":2,\"typeName\":\"I\"}]}}},{\"name\":\"add\",\"parameters\":[{\"name\":\"jobClass\",\"type\":{\"$tsfType\":4},\"optional\":false,\"default\":false},{\"name\":\"data\",\"type\":{\"$tsfType\":5},\"optional\":false,\"default\":false},{\"name\":\"options\",\"type\":{\"$tsfType\":3},\"optional\":true,\"default\":true}],\"returnType\":{\"kind\":16,\"typeName\":\"QueuedWorkerJob\",\"classType\":{\"$tsfImport\":[1,\"./types\",\"QueuedWorkerJob\"]},\"typeArguments\":[{\"kind\":2,\"typeName\":\"I\"}]}},{\"name\":\"getQueuedJobs\",\"parameters\":[{\"name\":\"queue\",\"type\":{\"$tsfType\":6},\"optional\":true,\"default\":true}],\"returnType\":{\"kind\":14,\"type\":{\"kind\":18,\"typeName\":\"QueuedWorkerJob\",\"types\":[{\"kind\":20,\"name\":\"id\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queue\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"data\",\"type\":{\"kind\":2,\"typeName\":\"I\"},\"optional\":false},{\"kind\":20,\"name\":\"jobClass\",\"type\":{\"kind\":2,\"typeName\":\"JobClass\",\"typeArguments\":[{\"kind\":2,\"typeName\":\"I\"}]},\"optional\":false},{\"kind\":20,\"name\":\"options\",\"type\":{\"kind\":18,\"typeName\":\"IJobOptions\",\"types\":[{\"kind\":20,\"name\":\"delay\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"queueName\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runInTest\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runImmediately\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"recordToDatabase\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"repeatKey\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]},\"optional\":false},{\"kind\":20,\"name\":\"createdAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":2}},\"optional\":false},{\"kind\":20,\"name\":\"shouldExecuteAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":2}},\"optional\":false},{\"kind\":20,\"name\":\"attemptsMade\",\"type\":{\"kind\":7},\"optional\":false},{\"kind\":20,\"name\":\"status\",\"type\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"queued\"},{\"kind\":10,\"literal\":\"completed\"},{\"kind\":10,\"literal\":\"failed\"},{\"kind\":10,\"literal\":\"skipped\"}],\"typeName\":\"WorkerJobStatus\"},\"optional\":false},{\"kind\":20,\"name\":\"result\",\"type\":{\"kind\":12,\"types\":[{\"kind\":2},{\"kind\":4}]},\"optional\":true}]}}},{\"name\":\"getAllQueuedJobs\",\"parameters\":[],\"returnType\":{\"kind\":14,\"type\":{\"kind\":18,\"typeName\":\"QueuedWorkerJob\",\"types\":[{\"kind\":20,\"name\":\"id\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queue\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"data\",\"type\":{\"kind\":2,\"typeName\":\"I\"},\"optional\":false},{\"kind\":20,\"name\":\"jobClass\",\"type\":{\"kind\":2,\"typeName\":\"JobClass\",\"typeArguments\":[{\"kind\":2,\"typeName\":\"I\"}]},\"optional\":false},{\"kind\":20,\"name\":\"options\",\"type\":{\"kind\":18,\"typeName\":\"IJobOptions\",\"types\":[{\"kind\":20,\"name\":\"delay\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"queueName\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runInTest\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runImmediately\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"recordToDatabase\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"repeatKey\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]},\"optional\":false},{\"kind\":20,\"name\":\"createdAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":2}},\"optional\":false},{\"kind\":20,\"name\":\"shouldExecuteAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":2}},\"optional\":false},{\"kind\":20,\"name\":\"attemptsMade\",\"type\":{\"kind\":7},\"optional\":false},{\"kind\":20,\"name\":\"status\",\"type\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"queued\"},{\"kind\":10,\"literal\":\"completed\"},{\"kind\":10,\"literal\":\"failed\"},{\"kind\":10,\"literal\":\"skipped\"}],\"typeName\":\"WorkerJobStatus\"},\"optional\":false},{\"kind\":20,\"name\":\"result\",\"type\":{\"kind\":12,\"types\":[{\"kind\":2},{\"kind\":4}]},\"optional\":true}]}}},{\"name\":\"markCompleted\",\"parameters\":[{\"name\":\"job\",\"type\":{\"kind\":18,\"typeName\":\"QueuedWorkerJob\",\"types\":[{\"kind\":20,\"name\":\"id\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queue\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"data\",\"type\":{\"kind\":2,\"typeName\":\"I\"},\"optional\":false},{\"kind\":20,\"name\":\"jobClass\",\"type\":{\"kind\":2,\"typeName\":\"JobClass\",\"typeArguments\":[{\"kind\":2,\"typeName\":\"I\"}]},\"optional\":false},{\"kind\":20,\"name\":\"options\",\"type\":{\"kind\":18,\"typeName\":\"IJobOptions\",\"types\":[{\"kind\":20,\"name\":\"delay\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"queueName\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runInTest\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runImmediately\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"recordToDatabase\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"repeatKey\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]},\"optional\":false},{\"kind\":20,\"name\":\"createdAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":2}},\"optional\":false},{\"kind\":20,\"name\":\"shouldExecuteAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":2}},\"optional\":false},{\"kind\":20,\"name\":\"attemptsMade\",\"type\":{\"kind\":7},\"optional\":false},{\"kind\":20,\"name\":\"status\",\"type\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"queued\"},{\"kind\":10,\"literal\":\"completed\"},{\"kind\":10,\"literal\":\"failed\"},{\"kind\":10,\"literal\":\"skipped\"}],\"typeName\":\"WorkerJobStatus\"},\"optional\":false},{\"kind\":20,\"name\":\"result\",\"type\":{\"kind\":12,\"types\":[{\"kind\":2},{\"kind\":4}]},\"optional\":true}]},\"optional\":false,\"default\":false},{\"name\":\"result\",\"type\":{\"$tsfType\":6},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":7}},{\"name\":\"markFailed\",\"parameters\":[{\"name\":\"job\",\"type\":{\"kind\":18,\"typeName\":\"QueuedWorkerJob\",\"types\":[{\"kind\":20,\"name\":\"id\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queue\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"data\",\"type\":{\"kind\":2,\"typeName\":\"I\"},\"optional\":false},{\"kind\":20,\"name\":\"jobClass\",\"type\":{\"kind\":2,\"typeName\":\"JobClass\",\"typeArguments\":[{\"kind\":2,\"typeName\":\"I\"}]},\"optional\":false},{\"kind\":20,\"name\":\"options\",\"type\":{\"kind\":18,\"typeName\":\"IJobOptions\",\"types\":[{\"kind\":20,\"name\":\"delay\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"queueName\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runInTest\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runImmediately\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"recordToDatabase\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"repeatKey\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]},\"optional\":false},{\"kind\":20,\"name\":\"createdAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":2}},\"optional\":false},{\"kind\":20,\"name\":\"shouldExecuteAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":2}},\"optional\":false},{\"kind\":20,\"name\":\"attemptsMade\",\"type\":{\"kind\":7},\"optional\":false},{\"kind\":20,\"name\":\"status\",\"type\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"queued\"},{\"kind\":10,\"literal\":\"completed\"},{\"kind\":10,\"literal\":\"failed\"},{\"kind\":10,\"literal\":\"skipped\"}],\"typeName\":\"WorkerJobStatus\"},\"optional\":false},{\"kind\":20,\"name\":\"result\",\"type\":{\"kind\":12,\"types\":[{\"kind\":2},{\"kind\":4}]},\"optional\":true}]},\"optional\":false,\"default\":false},{\"name\":\"result\",\"type\":{\"$tsfType\":6},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":7}},{\"name\":\"remove\",\"parameters\":[{\"name\":\"job\",\"type\":{\"kind\":18,\"typeName\":\"QueuedWorkerJob\",\"types\":[{\"kind\":20,\"name\":\"id\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queue\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"data\",\"type\":{\"kind\":2,\"typeName\":\"I\"},\"optional\":false},{\"kind\":20,\"name\":\"jobClass\",\"type\":{\"kind\":2,\"typeName\":\"JobClass\",\"typeArguments\":[{\"kind\":2,\"typeName\":\"I\"}]},\"optional\":false},{\"kind\":20,\"name\":\"options\",\"type\":{\"kind\":18,\"typeName\":\"IJobOptions\",\"types\":[{\"kind\":20,\"name\":\"delay\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"queueName\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runInTest\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runImmediately\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"recordToDatabase\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"repeatKey\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]},\"optional\":false},{\"kind\":20,\"name\":\"createdAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":2}},\"optional\":false},{\"kind\":20,\"name\":\"shouldExecuteAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":2}},\"optional\":false},{\"kind\":20,\"name\":\"attemptsMade\",\"type\":{\"kind\":7},\"optional\":false},{\"kind\":20,\"name\":\"status\",\"type\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"queued\"},{\"kind\":10,\"literal\":\"completed\"},{\"kind\":10,\"literal\":\"failed\"},{\"kind\":10,\"literal\":\"skipped\"}],\"typeName\":\"WorkerJobStatus\"},\"optional\":false},{\"kind\":20,\"name\":\"result\",\"type\":{\"kind\":12,\"types\":[{\"kind\":2},{\"kind\":4}]},\"optional\":true}]},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":7}},{\"name\":\"clear\",\"parameters\":[{\"name\":\"queue\",\"type\":{\"$tsfType\":1},\"optional\":true,\"default\":false}],\"returnType\":{\"$tsfType\":7}},{\"name\":\"getBullQueue\",\"parameters\":[{\"name\":\"queueName\",\"type\":{\"$tsfType\":1},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsf\":3}},{\"name\":\"getBullQueues\",\"parameters\":[],\"returnType\":{\"kind\":14,\"type\":{\"kind\":18,\"types\":[{\"kind\":20,\"name\":\"name\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queue\",\"type\":{\"$tsf\":4},\"optional\":false}]}}},{\"name\":\"normalizeJobData\",\"parameters\":[{\"name\":\"data\",\"type\":{\"$tsfType\":8},\"optional\":false,\"default\":false}],\"returnType\":{\"$tsfType\":5}},{\"name\":\"fromBullJob\",\"parameters\":[{\"name\":\"job\",\"type\":{\"$tsf\":5},\"optional\":false,\"default\":false},{\"name\":\"jobClass\",\"type\":{\"$tsfType\":4},\"optional\":false,\"default\":false}],\"returnType\":{\"kind\":16,\"typeName\":\"QueuedWorkerJob\",\"classType\":{\"$tsfImport\":[1,\"./types\",\"QueuedWorkerJob\"]},\"typeArguments\":[{\"kind\":2,\"typeName\":\"I\"}]}},{\"name\":\"shutdown\",\"parameters\":[],\"returnType\":{\"$tsfType\":9}},{\"name\":\"getBullMqOptions\",\"parameters\":[],\"returnType\":{\"kind\":13,\"types\":[{\"$tsfAlias\":[1,\"bullmq\",\"QueueOptions\",\"QueueOptions\"]},{\"kind\":18,\"typeName\":\"Pick\",\"utilityType\":\"Pick\",\"typeArguments\":[{\"$tsfAlias\":[1,\"bullmq\",\"WorkerOptions\",\"WorkerOptions\"]}],\"utilityKeys\":[\"connection\",\"prefix\"],\"types\":[]}]}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"config\",\"type\":{\"kind\":16,\"typeName\":\"BaseAppConfig\",\"classType\":{\"$tsfImport\":[1,\"../../app/index\",\"BaseAppConfig\"]}},\"optional\":true,\"default\":false}]}]", [() => WorkerQueueRegistry, require, () => (typeof Date !== "undefined" ? Date : (typeof exports !== "undefined" ? exports.Date : undefined)), Object.assign(_a.resolveCompactMetadataAliasV1(require, "bullmq", "Queue", "Queue"), { typeArguments: [{ kind: 18, typeName: "BullMqWorkerJobData", types: [{ kind: 20, name: "data", type: { kind: 2, typeName: "I" }, optional: false }, { kind: 20, name: "options", type: { kind: 18, typeName: "IJobOptions", types: [{ kind: 20, name: "delay", type: { kind: 12, types: [{ kind: 7 }, { kind: 4 }] }, optional: true }, { kind: 20, name: "queueName", type: { kind: 12, types: [{ kind: 6 }, { kind: 4 }] }, optional: true }, { kind: 20, name: "runInTest", type: { kind: 12, types: [{ kind: 8 }, { kind: 4 }] }, optional: true }, { kind: 20, name: "runImmediately", type: { kind: 12, types: [{ kind: 8 }, { kind: 4 }] }, optional: true }, { kind: 20, name: "recordToDatabase", type: { kind: 12, types: [{ kind: 8 }, { kind: 4 }] }, optional: true }, { kind: 20, name: "repeatKey", type: { kind: 12, types: [{ kind: 6 }, { kind: 4 }] }, optional: true }] }, optional: false }] }] }), Object.assign(_a.resolveCompactMetadataAliasV1(require, "bullmq", "Queue", "Queue"), { typeArguments: [{ kind: 18, typeName: "BullMqWorkerJobData", types: [{ kind: 20, name: "data", type: { kind: 2, typeName: "I" }, optional: false }, { kind: 20, name: "options", type: { kind: 18, typeName: "IJobOptions", types: [{ kind: 20, name: "delay", type: { kind: 12, types: [{ kind: 7 }, { kind: 4 }] }, optional: true }, { kind: 20, name: "queueName", type: { kind: 12, types: [{ kind: 6 }, { kind: 4 }] }, optional: true }, { kind: 20, name: "runInTest", type: { kind: 12, types: [{ kind: 8 }, { kind: 4 }] }, optional: true }, { kind: 20, name: "runImmediately", type: { kind: 12, types: [{ kind: 8 }, { kind: 4 }] }, optional: true }, { kind: 20, name: "recordToDatabase", type: { kind: 12, types: [{ kind: 8 }, { kind: 4 }] }, optional: true }, { kind: 20, name: "repeatKey", type: { kind: 12, types: [{ kind: 6 }, { kind: 4 }] }, optional: true }] }, optional: false }] }] }), Object.assign(_a.resolveCompactMetadataAliasV1(require, "bullmq", "Job", "BullJob"), { typeArguments: [{ kind: 16, typeName: "BullMqWorkerJobData", classType: () => (typeof BullMqWorkerJobData !== "undefined" ? BullMqWorkerJobData : (typeof exports !== "undefined" ? exports.BullMqWorkerJobData : undefined)), typeArguments: [{ kind: 2, typeName: "I" }] }] })], __tsf_metadata_type);
}
exports.WorkerQueueRegistry = WorkerQueueRegistry;
_WorkerQueueRegistryMetadata_1.classType = WorkerQueueRegistry;
WorkerQueueRegistry.__tsfType = _WorkerQueueRegistryMetadata_1;
function getOwnQueueName(jobClass) {
    return Object.prototype.hasOwnProperty.call(jobClass, 'QUEUE_NAME') ? jobClass.QUEUE_NAME : undefined;
}
function isRecord(value) {
    return typeof value === 'object' && value !== null;
}
function isWorkerPayloadWrapper(value) {
    if (!Object.prototype.hasOwnProperty.call(value, 'options'))
        return false;
    if (Object.prototype.hasOwnProperty.call(value, 'data'))
        return true;
    return isRecord(value.options) && Object.prototype.hasOwnProperty.call(value.options, 'repeatKey');
}
exports.__tsfTypeAliases = _a.decodeCompactMetadataV1("[1,{\"BullMqWorkerJobData\":{\"kind\":18,\"typeName\":\"BullMqWorkerJobData\",\"types\":[{\"kind\":20,\"name\":\"data\",\"type\":{\"kind\":2,\"typeName\":\"I\"},\"optional\":false},{\"kind\":20,\"name\":\"options\",\"type\":{\"kind\":18,\"typeName\":\"IJobOptions\",\"types\":[{\"kind\":20,\"name\":\"delay\",\"type\":{\"kind\":12,\"types\":[{\"kind\":7},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"queueName\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runInTest\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"runImmediately\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"recordToDatabase\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true},{\"kind\":20,\"name\":\"repeatKey\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":4}]},\"optional\":true}]},\"optional\":false}]}}]", [], __tsf_metadata_type);
//# sourceMappingURL=queue.js.map