import Owl from "@quirrel/owl";
import { cron, every } from "../../shared/repeat";
import { IncidentForwarder } from "./incident-forwarder";
import { decodeQueueDescriptor } from "./queue-descriptor";
export async function createOwl(redisFactory, logger, incidentReceiver, telemetrist) {
    var _a;
    const incidentForwarder = incidentReceiver
        ? new IncidentForwarder(incidentReceiver.endpoint, incidentReceiver.passphrase)
        : undefined;
    const owl = new Owl({
        redisFactory: redisFactory,
        scheduleMap: {
            every,
            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 } = 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;
}
//# sourceMappingURL=owl.js.map