"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.WorkerRecorderService = void 0;
const tslib_1 = require("tslib");
const _a = tslib_1.__importStar(require("@zyno-io/ts-reflection/type-metadata-runtime"));
const helpers_1 = require("../../helpers");
const sql_1 = require("../../database/sql");
const entity_1 = require("./entity");
const observer_1 = require("./observer");
const __tsf_metadata_type = _a.createCompactMetadataRegistryV1("[1,[{\"kind\":2},{\"kind\":18,\"types\":[{\"kind\":20,\"name\":\"recordToDatabase\",\"type\":{\"kind\":12,\"types\":[{\"kind\":8},{\"kind\":4}]},\"optional\":true}]}]]", []);
let _WorkerRecorderServiceMetadata_1;
class WorkerRecorderService {
    db;
    records = [];
    constructor(db) {
        this.db = db;
        if (db)
            entity_1.JobEntity.registerDatabase(db);
    }
    getRecords() {
        return this.records.map(record => ({ ...record }));
    }
    async recordCompleted(job, result, options = {}) {
        return this.record(job, 'completed', result, options);
    }
    async recordFailed(job, result, options = {}) {
        return this.record(job, 'failed', result, options);
    }
    async record(job, status, result, options) {
        const now = new Date();
        const record = {
            id: `${job.queue}:${job.id}:${job.attemptsMade || 1}`,
            queue: job.queue,
            queueId: job.id,
            attempt: job.attemptsMade || 1,
            name: job.name,
            data: job.data,
            traceId: null,
            status,
            result,
            createdAt: job.createdAt,
            shouldExecuteAt: job.shouldExecuteAt,
            executedAt: now,
            completedAt: now
        };
        this.records.push(record);
        if (options.recordToDatabase && this.db)
            await this.insertRecord(record);
        (0, observer_1.notifyWorkerObservers)({ type: status, job, record });
        return record;
    }
    async insertRecord(record) {
        await this.db.rawExecute((0, sql_1.sql) `INSERT INTO ${sql_1.sql.identifier('_jobs')} (${sql_1.sql.join([
            'id',
            'queue',
            'queueId',
            'attempt',
            'name',
            'data',
            'traceId',
            'status',
            'result',
            'createdAt',
            'shouldExecuteAt',
            'executedAt',
            'completedAt'
        ].map(name => sql_1.sql.identifier(name)))}) VALUES (${sql_1.sql.join([
            (0, sql_1.sql) `${record.id || (0, helpers_1.uuid7)()}`,
            (0, sql_1.sql) `${record.queue}`,
            (0, sql_1.sql) `${record.queueId}`,
            (0, sql_1.sql) `${record.attempt}`,
            (0, sql_1.sql) `${record.name}`,
            (0, sql_1.sql) `${toJsonValue(record.data)}`,
            (0, sql_1.sql) `${record.traceId}`,
            (0, sql_1.sql) `${record.status}`,
            (0, sql_1.sql) `${toJsonValue(record.result)}`,
            (0, sql_1.sql) `${record.createdAt}`,
            (0, sql_1.sql) `${record.shouldExecuteAt}`,
            (0, sql_1.sql) `${record.executedAt}`,
            (0, sql_1.sql) `${record.completedAt}`
        ])})`);
    }
    static __tsfType = _WorkerRecorderServiceMetadata_1 = _a.decodeCompactMetadataV1("[1,{\"kind\":16,\"name\":\"WorkerRecorderService\",\"typeName\":\"WorkerRecorderService\",\"classType\":{\"$tsf\":0},\"properties\":[],\"methods\":[{\"name\":\"getRecords\",\"parameters\":[],\"returnType\":{\"kind\":14,\"type\":{\"kind\":18,\"typeName\":\"WorkerJobRecord\",\"types\":[{\"kind\":20,\"name\":\"id\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queue\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queueId\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"attempt\",\"type\":{\"kind\":7},\"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\":\"traceId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":5}]},\"optional\":false},{\"kind\":20,\"name\":\"status\",\"type\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"completed\"},{\"kind\":10,\"literal\":\"failed\"}]},\"optional\":false},{\"kind\":20,\"name\":\"result\",\"type\":{\"kind\":2,\"typeName\":\"O\"},\"optional\":false},{\"kind\":20,\"name\":\"createdAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"shouldExecuteAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"executedAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"completedAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false}],\"typeParameters\":[\"I\",\"O\"]}}},{\"name\":\"recordCompleted\",\"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\":1}},\"optional\":false},{\"kind\":20,\"name\":\"shouldExecuteAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"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}],\"typeParameters\":[\"I\"]},\"optional\":false,\"default\":false},{\"name\":\"result\",\"type\":{\"$tsfType\":0},\"optional\":false,\"default\":false},{\"name\":\"options\",\"type\":{\"$tsfType\":1},\"optional\":true,\"default\":true}],\"returnType\":{\"kind\":22,\"type\":{\"kind\":18,\"typeName\":\"WorkerJobRecord\",\"types\":[{\"kind\":20,\"name\":\"id\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queue\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queueId\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"attempt\",\"type\":{\"kind\":7},\"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\":\"traceId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":5}]},\"optional\":false},{\"kind\":20,\"name\":\"status\",\"type\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"completed\"},{\"kind\":10,\"literal\":\"failed\"}]},\"optional\":false},{\"kind\":20,\"name\":\"result\",\"type\":{\"kind\":2,\"typeName\":\"O\"},\"optional\":false},{\"kind\":20,\"name\":\"createdAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"shouldExecuteAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"executedAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"completedAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false}],\"typeParameters\":[\"I\",\"O\"]}}},{\"name\":\"recordFailed\",\"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\":1}},\"optional\":false},{\"kind\":20,\"name\":\"shouldExecuteAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"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}],\"typeParameters\":[\"I\"]},\"optional\":false,\"default\":false},{\"name\":\"result\",\"type\":{\"$tsfType\":0},\"optional\":false,\"default\":false},{\"name\":\"options\",\"type\":{\"$tsfType\":1},\"optional\":true,\"default\":true}],\"returnType\":{\"kind\":22,\"type\":{\"kind\":18,\"typeName\":\"WorkerJobRecord\",\"types\":[{\"kind\":20,\"name\":\"id\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queue\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"queueId\",\"type\":{\"kind\":6},\"optional\":false},{\"kind\":20,\"name\":\"attempt\",\"type\":{\"kind\":7},\"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\":\"traceId\",\"type\":{\"kind\":12,\"types\":[{\"kind\":6},{\"kind\":5}]},\"optional\":false},{\"kind\":20,\"name\":\"status\",\"type\":{\"kind\":12,\"types\":[{\"kind\":10,\"literal\":\"completed\"},{\"kind\":10,\"literal\":\"failed\"}]},\"optional\":false},{\"kind\":20,\"name\":\"result\",\"type\":{\"kind\":2,\"typeName\":\"O\"},\"optional\":false},{\"kind\":20,\"name\":\"createdAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"shouldExecuteAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"executedAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false},{\"kind\":20,\"name\":\"completedAt\",\"type\":{\"kind\":16,\"typeName\":\"Date\",\"classType\":{\"$tsf\":1}},\"optional\":false}],\"typeParameters\":[\"I\",\"O\"]}}}],\"hasConstructor\":true,\"constructorParameters\":[{\"name\":\"db\",\"type\":{\"kind\":16,\"typeName\":\"BaseDatabase\",\"classType\":{\"$tsfImport\":[2,\"../../database/index\",\"BaseDatabase\"]}},\"optional\":true,\"default\":false}]}]", [() => WorkerRecorderService, () => (typeof Date !== "undefined" ? Date : (typeof exports !== "undefined" ? exports.Date : undefined)), require], __tsf_metadata_type);
}
exports.WorkerRecorderService = WorkerRecorderService;
_WorkerRecorderServiceMetadata_1.classType = WorkerRecorderService;
WorkerRecorderService.__tsfType = _WorkerRecorderServiceMetadata_1;
function toJsonValue(value) {
    return value === undefined ? null : JSON.stringify(value);
}
//# sourceMappingURL=recorder.js.map