{"version":3,"sources":["../src/scheduler.ts"],"sourcesContent":["/* --------------------------------------------------------------------------\n\n  MIT License\n\n  Copyright (c) 2024 Rami Pellumbi\n\n  Permission is hereby granted, free of charge, to any person obtaining a copy\n  of this software and associated documentation files (the \"Software\"), to deal\n  in the Software without restriction, including without limitation the rights\n  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell\n  copies of the Software, and to permit persons to whom the Software is\n  furnished to do so, subject to the following conditions:\n\n  The above copyright notice and this permission notice shall be included in all\n  copies or substantial portions of the Software.\n\n  THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR\n  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,\n  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE\n  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER\n  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,\n  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE\n  SOFTWARE.\n-----------------------------------------------------------------------------*/\n\nimport type { Clujo } from \"./clujo\";\n\n/**\n * Scheduler class for managing and running Clujo jobs.\n * This class allows adding, starting, and stopping multiple Clujo jobs in a centralized manner.\n */\nexport class Scheduler {\n    // biome-ignore lint/suspicious/noExplicitAny: want any here\n    readonly #jobs: Clujo<any>[] = [];\n\n    /**\n     * Adds a Clujo job to the scheduler.\n     * @param input - Object containing the job and optional completion handler.\n     * @param input.job - The Clujo job to be added.\n     * @param input.completionHandler - Optional function to invoke after the job completes.\n     */\n\n    // biome-ignore lint/suspicious/noExplicitAny: want any here\n    addJob(job: Clujo<any>) {\n        if (this.#jobs.some((addedJob) => addedJob.id === job.id)) {\n            throw new Error(`Job with id ${job.id} is already added to the scheduler.`);\n        }\n        this.#jobs.push(job);\n    }\n\n    /**\n     * Starts all added jobs in the scheduler.\n     *\n     * @param redis - Optional Redis instance to be passed to the jobs. If provided, enables distributed locking.\n     */\n    start() {\n        for (const job of this.#jobs) {\n            job.start();\n        }\n    }\n    /**\n     * Stops all running jobs in the scheduler.\n     *\n     * @param timeout - The maximum time (in milliseconds) to wait for jobs to stop. Defaults to 5000ms.\n     * @returns A promise that resolves when all jobs have stopped or the timeout is reached.\n     */\n    async stop(timeout = 5000) {\n        await Promise.all(this.#jobs.map((job) => job.stop(timeout)));\n    }\n\n    /**\n     * Returns the list of jobs added to the scheduler.\n     */\n\n    // biome-ignore lint/suspicious/noExplicitAny: want any here\n    get jobs(): Clujo<any>[] {\n        return this.#jobs;\n    }\n}\n"],"mappings":";AA+BO,IAAM,YAAN,MAAgB;AAAA;AAAA,EAEV,QAAsB,CAAC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUhC,OAAO,KAAiB;AACpB,QAAI,KAAK,MAAM,KAAK,CAAC,aAAa,SAAS,OAAO,IAAI,EAAE,GAAG;AACvD,YAAM,IAAI,MAAM,eAAe,IAAI,EAAE,qCAAqC;AAAA,IAC9E;AACA,SAAK,MAAM,KAAK,GAAG;AAAA,EACvB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,QAAQ;AACJ,eAAW,OAAO,KAAK,OAAO;AAC1B,UAAI,MAAM;AAAA,IACd;AAAA,EACJ;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,KAAK,UAAU,KAAM;AACvB,UAAM,QAAQ,IAAI,KAAK,MAAM,IAAI,CAAC,QAAQ,IAAI,KAAK,OAAO,CAAC,CAAC;AAAA,EAChE;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,IAAI,OAAqB;AACrB,WAAO,KAAK;AAAA,EAChB;AACJ;","names":[]}