"use strict";
(() => {
var exports = {};
exports.id = 841;
exports.ids = [841];
exports.modules = {

/***/ 2748:
/***/ ((module) => {

module.exports = require("luxon");

/***/ }),

/***/ 2725:
/***/ ((module) => {

module.exports = require("mongodb-memory-server");

/***/ }),

/***/ 1185:
/***/ ((module) => {

module.exports = require("mongoose");

/***/ }),

/***/ 9456:
/***/ ((module) => {

module.exports = require("phin");

/***/ }),

/***/ 7773:
/***/ ((module) => {

module.exports = require("winston");

/***/ }),

/***/ 7564:
/***/ ((module) => {

module.exports = import("chalk");;

/***/ }),

/***/ 6555:
/***/ ((module) => {

module.exports = import("uuid");;

/***/ }),

/***/ 5053:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ handler)
/* harmony export */ });
/* harmony import */ var rpc_updateJob__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(8350);
/* harmony import */ var uuid__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(6555);
/* harmony import */ var rpc_deleteJob__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(4969);
/* harmony import */ var rpc_getJobByName__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(3452);
/* harmony import */ var rpc_enqueueJob__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(1051);
/* harmony import */ var winston_logger__WEBPACK_IMPORTED_MODULE_5__ = __webpack_require__(674);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([rpc_updateJob__WEBPACK_IMPORTED_MODULE_0__, uuid__WEBPACK_IMPORTED_MODULE_1__, rpc_deleteJob__WEBPACK_IMPORTED_MODULE_2__, rpc_getJobByName__WEBPACK_IMPORTED_MODULE_3__, rpc_enqueueJob__WEBPACK_IMPORTED_MODULE_4__, winston_logger__WEBPACK_IMPORTED_MODULE_5__]);
([rpc_updateJob__WEBPACK_IMPORTED_MODULE_0__, uuid__WEBPACK_IMPORTED_MODULE_1__, rpc_deleteJob__WEBPACK_IMPORTED_MODULE_2__, rpc_getJobByName__WEBPACK_IMPORTED_MODULE_3__, rpc_enqueueJob__WEBPACK_IMPORTED_MODULE_4__, winston_logger__WEBPACK_IMPORTED_MODULE_5__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);






const initAppId = "00000000-0000-0000-0000-000000000000";
async function handler(req, res) {
    var ref, ref1;
    const headerAppId = Array.isArray(req.headers["x-taskless-app-id"]) ? req.headers["x-taskless-app-id"][0] : req.headers["x-taskless-app-id"];
    const appId = headerAppId ? headerAppId : initAppId;
    const context = {
        v5: (value)=>(0,uuid__WEBPACK_IMPORTED_MODULE_1__.v5)(value, appId)
    };
    winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.info */ .k.info(`Received request for Application ID ${appId}`);
    // success operation
    const success = (v)=>{
        res.status(200).json({
            data: v
        });
    };
    const error = (v)=>{
        winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.error */ .k.error(v);
        res.status(500).json({
            error: v,
            data: null
        });
    };
    try {
        if ((0,rpc_enqueueJob__WEBPACK_IMPORTED_MODULE_4__/* .isEnqueueJob */ .K)(req.body)) {
            const v = await (0,rpc_enqueueJob__WEBPACK_IMPORTED_MODULE_4__/* .enqueueJob */ .C)(req.body.variables, context);
            winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.info */ .k.info(`Enqueing Job: ${req.body.variables.name}`);
            return success(v);
        }
        if ((0,rpc_updateJob__WEBPACK_IMPORTED_MODULE_0__/* .isUpdateJob */ .f)(req.body)) {
            const v = await (0,rpc_updateJob__WEBPACK_IMPORTED_MODULE_0__/* .updateJob */ .g)(req.body.variables, context);
            winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.info */ .k.info(`Updating Job: ${req.body.variables.name}`);
            return success(v);
        }
        if ((0,rpc_deleteJob__WEBPACK_IMPORTED_MODULE_2__/* .isDeleteJob */ .I)(req.body)) {
            const v = await (0,rpc_deleteJob__WEBPACK_IMPORTED_MODULE_2__/* .deleteJob */ .C)(req.body.variables, context);
            winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.info */ .k.info(`Deleting Job: ${req.body.variables.name}`);
            return success(v);
        }
        if ((0,rpc_getJobByName__WEBPACK_IMPORTED_MODULE_3__/* .isGetJobByName */ .k)(req.body)) {
            const v = await (0,rpc_getJobByName__WEBPACK_IMPORTED_MODULE_3__/* .getJobByName */ .d)(req.body.variables, context);
            winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.info */ .k.info(`Getting Job: ${req.body.variables.name}`);
            return success(v);
        }
    } catch (e) {
        if (process.env.TASKLESS_FULL_ERRORS === "1") {
            console.error(e);
        }
        return error(e);
    }
    winston_logger__WEBPACK_IMPORTED_MODULE_5__/* .logger.error */ .k.error("Unsupported RPC method: " + (((ref = req.body) === null || ref === void 0 ? void 0 : ref.method) ?? "unknown"));
    res.status(500).json({
        data: null,
        error: "Unsupported RPC method: " + (((ref1 = req.body) === null || ref1 === void 0 ? void 0 : ref1.method) ?? "unknown")
    });
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 5357:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "e": () => (/* binding */ gqlHeadersToObject)
/* harmony export */ });
const gqlHeadersToObject = (gqlHeaders)=>{
    if (typeof gqlHeaders === "undefined" || gqlHeaders === null) {
        return {};
    }
    const all = {};
    for (const h of gqlHeaders){
        all[h.name] = h.value;
    }
    return all;
};


/***/ }),

/***/ 4969:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "C": () => (/* binding */ deleteJob),
/* harmony export */   "I": () => (/* binding */ isDeleteJob)
/* harmony export */ });
/* harmony import */ var mongo_db__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(9306);
/* harmony import */ var worker_loop__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5290);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([mongo_db__WEBPACK_IMPORTED_MODULE_0__, worker_loop__WEBPACK_IMPORTED_MODULE_1__]);
([mongo_db__WEBPACK_IMPORTED_MODULE_0__, worker_loop__WEBPACK_IMPORTED_MODULE_1__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);


const isDeleteJob = (v)=>{
    return v.method === "deleteJob";
};
const deleteJob = async (variables, context)=>{
    (0,worker_loop__WEBPACK_IMPORTED_MODULE_1__/* .start */ .B)();
    const id = context.v5(variables.name);
    const job = await mongo_db__WEBPACK_IMPORTED_MODULE_0__/* .Job.findOneAndDelete */ .oI.findOneAndDelete({
        v5id: {
            $eq: id
        }
    }, {
        returnDocument: "before"
    }).exec();
    if (!job) {
        return {
            deleteJob: null
        };
    }
    return {
        deleteJob: {
            ...(0,mongo_db__WEBPACK_IMPORTED_MODULE_0__/* .jobToJobFragment */ .dT)(variables.name, job)
        }
    };
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 1051:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "C": () => (/* binding */ enqueueJob),
/* harmony export */   "K": () => (/* binding */ isEnqueueJob)
/* harmony export */ });
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2748);
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(luxon__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var mongo_db__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(9306);
/* harmony import */ var worker_loop__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(5290);
/* harmony import */ var _common__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(5357);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([mongo_db__WEBPACK_IMPORTED_MODULE_1__, worker_loop__WEBPACK_IMPORTED_MODULE_2__]);
([mongo_db__WEBPACK_IMPORTED_MODULE_1__, worker_loop__WEBPACK_IMPORTED_MODULE_2__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);




const isEnqueueJob = (v)=>{
    return v.method === "enqueueJob";
};
const enqueueJob = async (variables, context)=>{
    (0,worker_loop__WEBPACK_IMPORTED_MODULE_2__/* .start */ .B)();
    const id = context.v5(variables.name);
    const runAt = variables.job.runAt ?? luxon__WEBPACK_IMPORTED_MODULE_0__.DateTime.now().toISO();
    const job = await mongo_db__WEBPACK_IMPORTED_MODULE_1__/* .Job.findOneAndUpdate */ .oI.findOneAndUpdate({
        v5id: {
            $eq: id
        }
    }, {
        $set: {
            body: variables.job.body ?? undefined,
            endpoint: variables.job.endpoint,
            headers: (0,_common__WEBPACK_IMPORTED_MODULE_3__/* .gqlHeadersToObject */ .e)(variables.job.headers),
            name: variables.name,
            retries: variables.job.retries === 0 ? 0 : variables.job.retries ?? 5,
            runAt,
            runEvery: variables.job.runEvery ?? undefined,
            updatedAt: new Date(),
            v5id: id,
            schedule: {
                next: luxon__WEBPACK_IMPORTED_MODULE_0__.DateTime.fromISO(runAt).toJSDate()
            }
        }
    }, {
        returnDocument: "after",
        upsert: true
    }).exec();
    if (!job) {
        throw new Error("No job could be created or updated");
    }
    return {
        replaceJob: {
            ...(0,mongo_db__WEBPACK_IMPORTED_MODULE_1__/* .jobToJobFragment */ .dT)(variables.name, job)
        }
    };
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 3452:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "d": () => (/* binding */ getJobByName),
/* harmony export */   "k": () => (/* binding */ isGetJobByName)
/* harmony export */ });
/* harmony import */ var mongo_db__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(9306);
/* harmony import */ var worker_loop__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(5290);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([mongo_db__WEBPACK_IMPORTED_MODULE_0__, worker_loop__WEBPACK_IMPORTED_MODULE_1__]);
([mongo_db__WEBPACK_IMPORTED_MODULE_0__, worker_loop__WEBPACK_IMPORTED_MODULE_1__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);


const isGetJobByName = (v)=>{
    return v.method === "getJobByName";
};
const getJobByName = async (variables, context)=>{
    (0,worker_loop__WEBPACK_IMPORTED_MODULE_1__/* .start */ .B)();
    const id = context.v5(variables.name);
    const job = await mongo_db__WEBPACK_IMPORTED_MODULE_0__/* .Job.findOne */ .oI.findOne({
        v5id: {
            $eq: id
        }
    }).exec();
    if (!job) {
        return null;
    }
    return {
        job: {
            ...(0,mongo_db__WEBPACK_IMPORTED_MODULE_0__/* .jobToJobFragment */ .dT)(job.name, job)
        }
    };
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 8350:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "f": () => (/* binding */ isUpdateJob),
/* harmony export */   "g": () => (/* binding */ updateJob)
/* harmony export */ });
/* harmony import */ var worker_loop__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(5290);
/* harmony import */ var mongo_db__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(9306);
/* harmony import */ var _common__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(5357);
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(2748);
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(luxon__WEBPACK_IMPORTED_MODULE_2__);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([worker_loop__WEBPACK_IMPORTED_MODULE_0__, mongo_db__WEBPACK_IMPORTED_MODULE_1__]);
([worker_loop__WEBPACK_IMPORTED_MODULE_0__, mongo_db__WEBPACK_IMPORTED_MODULE_1__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);




const isUpdateJob = (v)=>{
    return v.method === "updateJob";
};
const updateJob = async (variables, context)=>{
    (0,worker_loop__WEBPACK_IMPORTED_MODULE_0__/* .start */ .B)();
    const id = context.v5(variables.name);
    const next = {};
    if (variables.job.body) {
        next.body = variables.job.body;
    }
    if (variables.job.endpoint) {
        next.endpoint = variables.job.endpoint;
    }
    if (typeof variables.job.enabled === "boolean") {
        next.enabled = variables.job.enabled;
    }
    if (variables.job.headers) {
        next.headers = (0,_common__WEBPACK_IMPORTED_MODULE_3__/* .gqlHeadersToObject */ .e)(variables.job.headers);
    }
    if (typeof variables.job.retries !== "undefined" && variables.job.retries !== null) {
        next.retries = variables.job.retries;
    }
    if (variables.job.runAt) {
        next.runAt = variables.job.runAt;
        next.schedule = new mongo_db__WEBPACK_IMPORTED_MODULE_1__/* .Schedule */ .Pf({
            next: luxon__WEBPACK_IMPORTED_MODULE_2__.DateTime.fromISO(variables.job.runAt).toJSDate(),
            attempt: 0
        });
    }
    if (variables.job.runEvery) {
        next.runEvery = variables.job.runEvery;
    }
    next.updatedAt = new Date();
    const job = await mongo_db__WEBPACK_IMPORTED_MODULE_1__/* .Job.findOneAndUpdate */ .oI.findOneAndUpdate({
        v5id: {
            $eq: id
        }
    }, {
        $set: next
    }, {
        returnDocument: "after",
        upsert: false
    }).exec();
    if (!job) {
        throw new Error("No job could be created or updated");
    }
    // TODO: schedule next
    return {
        updateJob: {
            ...(0,mongo_db__WEBPACK_IMPORTED_MODULE_1__/* .jobToJobFragment */ .dT)(variables.name, job)
        }
    };
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 674:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "k": () => (/* binding */ logger)
/* harmony export */ });
/* harmony import */ var winston__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(7773);
/* harmony import */ var winston__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(winston__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var chalk__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(7564);
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(2748);
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(luxon__WEBPACK_IMPORTED_MODULE_2__);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([chalk__WEBPACK_IMPORTED_MODULE_1__]);
chalk__WEBPACK_IMPORTED_MODULE_1__ = (__webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__)[0];



const { combine , timestamp , printf  } = winston__WEBPACK_IMPORTED_MODULE_0__.format;
const getDebugLevel = ()=>{
    const level = process.env.TASKLESS_DEV_DEBUG ?? "info";
    return [
        "debug",
        "log",
        "info",
        "warn",
        "error"
    ].includes(level) ? level : "info";
};
const colorizedLabel = {
    info: chalk__WEBPACK_IMPORTED_MODULE_1__["default"].blue,
    warn: chalk__WEBPACK_IMPORTED_MODULE_1__["default"].yellow,
    error: chalk__WEBPACK_IMPORTED_MODULE_1__["default"].red
};
const logFormat = printf(({ level , message , label , job , timestamp: timestamp1  })=>{
    const colorizer = colorizedLabel[level] || colorizedLabel.info;
    const dt = luxon__WEBPACK_IMPORTED_MODULE_2__.DateTime.fromISO(timestamp1);
    const lbl = label ?? "root";
    const jobLabel = job ? `${lbl} ${job}` : null;
    const fullLabel = jobLabel ?? lbl;
    const ts = `${dt.toLocaleString(luxon__WEBPACK_IMPORTED_MODULE_2__.DateTime.TIME_24_WITH_SECONDS)}.${dt.get("millisecond")}`;
    return `${colorizer(level).padEnd(6, " ")} - ${chalk__WEBPACK_IMPORTED_MODULE_1__["default"].gray(`${ts} (${fullLabel})`)} ${message}`;
});
const logger = (0,winston__WEBPACK_IMPORTED_MODULE_0__.createLogger)({
    format: combine(timestamp(), logFormat),
    transports: [
        new winston__WEBPACK_IMPORTED_MODULE_0__.transports.Console({
            level: getDebugLevel()
        }), 
    ]
});

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 5290:
/***/ ((module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.a(module, async (__webpack_handle_async_dependencies__, __webpack_async_result__) => { try {
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "B": () => (/* binding */ start)
/* harmony export */ });
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2748);
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(luxon__WEBPACK_IMPORTED_MODULE_0__);
/* harmony import */ var winston_logger__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(674);
/* harmony import */ var phin__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(9456);
/* harmony import */ var phin__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(phin__WEBPACK_IMPORTED_MODULE_2__);
/* harmony import */ var mongo_db__WEBPACK_IMPORTED_MODULE_3__ = __webpack_require__(9306);
/* harmony import */ var _scheduler__WEBPACK_IMPORTED_MODULE_4__ = __webpack_require__(7979);
var __webpack_async_dependencies__ = __webpack_handle_async_dependencies__([winston_logger__WEBPACK_IMPORTED_MODULE_1__, mongo_db__WEBPACK_IMPORTED_MODULE_3__]);
([winston_logger__WEBPACK_IMPORTED_MODULE_1__, mongo_db__WEBPACK_IMPORTED_MODULE_3__] = __webpack_async_dependencies__.then ? (await __webpack_async_dependencies__)() : __webpack_async_dependencies__);





const WAIT_INTERVAL = 100;
const MAX_JOB_CHUNK = 10;
const log = winston_logger__WEBPACK_IMPORTED_MODULE_1__/* .logger.child */ .k.child({
    label: "worker/loop.ts"
});
/** Simple typeguard to discard null results */ function isMongoJob(v) {
    return v !== null;
}
let started = false;
/** Start the in-memory worker process. Protects itself against multiple instances */ const start = ()=>{
    if (started) {
        return;
    }
    started = true;
    setTimeout(tick, WAIT_INTERVAL);
    log.info("Started worker");
};
/** Find all jobs that are ready to run and asynchronously handle them */ const tick = async ()=>{
    let jobs = [];
    while(jobs.length < MAX_JOB_CHUNK){
        const next = await mongo_db__WEBPACK_IMPORTED_MODULE_3__/* .Job.findOneAndUpdate */ .oI.findOneAndUpdate({
            "schedule.next": {
                $lt: new Date()
            }
        }, {
            $set: {
                schedule: null
            }
        }, {
            new: true
        }).exec();
        if (!next) {
            break;
        }
        jobs.push(next);
    }
    if (jobs.length === 0) {
        // nothing to do
        setTimeout(tick, WAIT_INTERVAL);
        return;
    }
    log.debug(`Found ${jobs.length} item(s)`);
    await Promise.allSettled(jobs.filter(isMongoJob).map((job)=>handle(job)
    ));
    setTimeout(tick, WAIT_INTERVAL);
};
/** Handle a single instance of a job */ const handle = async (job)=>{
    if (job.enabled === false) {
        // disabled
        return;
    }
    const now = luxon__WEBPACK_IMPORTED_MODULE_0__.DateTime.now();
    const l = log.child({
        job: job.name
    });
    l.info("Handler invoked");
    let retry = false;
    const entry = new mongo_db__WEBPACK_IMPORTED_MODULE_3__/* .Log */ .Zb({
        job: job._id,
        jobId: job.v5id
    });
    const jjson = job.toJSON();
    const headers = {
        ...jjson.headers ?? {},
        "user-agent": "Taskless DevServer Worker",
        "content-type": "application/json"
    };
    const request = {
        url: job.endpoint,
        method: "POST",
        headers,
        // attach data if set
        ...job.body ? {
            data: job.body
        } : {},
        followRedirects: true,
        timeout: 15000
    };
    l.debug(`Phin Request: \n${JSON.stringify(request)}`);
    try {
        const resp = await phin__WEBPACK_IMPORTED_MODULE_2___default()({
            ...request,
            parse: "json"
        });
        entry.status = `${resp.statusCode}`.indexOf("2") === 0 ? "COMPLETED" : "FAILED";
        entry.statusCode = resp.statusCode ?? 200;
        entry.output = JSON.stringify(resp.body);
        if (`${resp.statusCode}`.indexOf("2") !== 0) {
            l.warn(`Non-2xx received from url: ${job.endpoint}`);
            retry = (job.schedule.attempt ?? 0) < (job.retries ?? 0) - 1;
        }
    } catch (e) {
        const msg = typeof e === "string" ? e : e instanceof Error ? e.message : `${e}`;
        l.error(`Error received: ${msg}`);
        retry = (job.schedule.attempt ?? 0) < (job.retries ?? 0) - 1;
        entry.status = "FAILED";
        entry.statusCode = 500;
        entry.output = JSON.stringify({
            "@taskless/dev": true,
            error: msg
        });
    }
    if (!job.logs) {
        job.logs = [];
    }
    job.logs.push(entry);
    // save log
    await entry.save();
    // next scheduling
    if (retry) {
        l.info("Linear rescheduling of job");
        job.schedule = new mongo_db__WEBPACK_IMPORTED_MODULE_3__/* .Schedule */ .Pf({
            next: now.plus({
                seconds: 3
            }).toJSDate(),
            attempt: (job.schedule.attempt ?? 0) + 1
        });
    }
    if (!retry && job.runEvery) {
        l.info("Scheduling next occurence");
        const runAt = luxon__WEBPACK_IMPORTED_MODULE_0__.DateTime.fromISO(job.runAt);
        const interval = luxon__WEBPACK_IMPORTED_MODULE_0__.Duration.fromISO(job.runEvery);
        const nextRun = (0,_scheduler__WEBPACK_IMPORTED_MODULE_4__/* .findNextTime */ .h)(runAt, interval, now);
        job.schedule = new mongo_db__WEBPACK_IMPORTED_MODULE_3__/* .Schedule */ .Pf({
            next: nextRun.toJSDate(),
            attempt: 0
        });
    }
    await job.save();
};

__webpack_async_result__();
} catch(e) { __webpack_async_result__(e); } });

/***/ }),

/***/ 7979:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "h": () => (/* binding */ findNextTime)
/* harmony export */ });
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(2748);
/* harmony import */ var luxon__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(luxon__WEBPACK_IMPORTED_MODULE_0__);

const findNextTime = (start, interval, until)=>{
    const sm = start.toMillis();
    const um = until.toMillis();
    const im = interval.toMillis();
    const passed = Math.floor((um - sm) / im);
    const closestStart = luxon__WEBPACK_IMPORTED_MODULE_0__.DateTime.fromMillis(sm + im * passed);
    return closestStart.plus(interval);
};


/***/ })

};
;

// load runtime
var __webpack_require__ = require("../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [306], () => (__webpack_exec__(5053)));
module.exports = __webpack_exports__;

})();