"use strict";
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.createOwl = void 0;
const owl_1 = __importDefault(require("@quirrel/owl"));
const repeat_1 = require("../../shared/repeat");
const incident_forwarder_1 = require("./incident-forwarder");
const queue_descriptor_1 = require("./queue-descriptor");
async function createOwl(redisFactory, logger, incidentReceiver, telemetrist) {
    var _a;
    const incidentForwarder = incidentReceiver
        ? new incident_forwarder_1.IncidentForwarder(incidentReceiver.endpoint, incidentReceiver.passphrase)
        : undefined;
    const owl = new owl_1.default({
        redisFactory: redisFactory,
        scheduleMap: {
            every: repeat_1.every,
            cron: repeat_1.cron,
        },
        logger: (_a = logger === null || logger === void 0 ? void 0 : logger.log) === null || _a === void 0 ? void 0 : _a.child({ module: "owl" }),
        async onError(ack, job, error) {
            let { tokenId, endpoint } = (0, queue_descriptor_1.decodeQueueDescriptor)(job.queue);
            await (incidentForwarder === null || incidentForwarder === void 0 ? void 0 : incidentForwarder.dispatch({
                endpoint,
                tokenId,
                payload: job.payload,
                id: job.id,
                runAt: job.runAt,
            }, {
                body: error.responseBody,
                status: error.responseStatus,
            }));
            await (telemetrist === null || telemetrist === void 0 ? void 0 : telemetrist.dispatch("execution_errored"));
        },
    });
    if (!process.env.SKIP_OWL_MIGRATIONS) {
        await owl.runMigrations();
    }
    return owl;
}
exports.createOwl = createOwl;
//# sourceMappingURL=owl.js.map