UNPKG

@kingsworld/plugin-cron

Version:

A simple @sapphire/framework plugin that aims to make use of the croner package and allow users to make cron jobs within their Sapphire discord bot.

139 lines (136 loc) 4.37 kB
'use strict'; var pieces = require('@sapphire/pieces'); var croner = require('croner'); var CronTask_cjs = require('./CronTask.cjs'); var normalizePattern_cjs = require('../utils/normalizePattern.cjs'); var __defProp = Object.defineProperty; var __name = (target, value) => __defProp(target, "name", { value, configurable: true }); var _CronTaskStore = class _CronTaskStore extends pieces.Store { constructor() { super(CronTask_cjs.CronTask, { name: "cron-tasks" }); } /** * Loops over all tasks and pauses those that are running. * * @remarks * This method will only pause tasks that: * - Are enabled * - Are currently running * - Have not been permanently stopped * * @returns CronTaskStore */ pauseAll() { for (const task of this.values()) { if (!task.enabled || !task.job.isRunning()) continue; task.job.pause(); } pieces.Store.logger?.(`[STORE => ${this.name}] [PAUSE] Paused all cronjob tasks.`); return this; } /** * Loops over all tasks and resumes those that are paused. * * @remarks * This method will only resume tasks that: * - Are enabled * - Are not currently running * - Have not been permanently stopped * * @returns CronTaskStore */ resumeAll() { for (const task of this.values()) { if (!task.enabled || task.job.isRunning() || task.job.isStopped()) continue; task.job.resume(); } pieces.Store.logger?.(`[STORE => ${this.name}] [RESUME] Resumed all cronjob tasks.`); return this; } /** * Loops over all tasks and stops those that are running. * * @remarks * This method will only stop tasks that: * - Are enabled * - Have not been permanently stopped * * ⚠️ Stopping jobs is **permanent** and cannot be resumed afterwards! * * @returns CronTaskStore */ stopAll() { for (const task of this.values()) { if (!task.enabled || task.job.isStopped()) continue; task.job.stop(); } pieces.Store.logger?.(`[STORE => ${this.name}] [STOP] Stopped all cronjob tasks.`); return this; } set(key, value) { const { pattern, timezone, protect, ...options } = value.options; const { sentry, defaultTimezone } = this.container.cronTasks; if (this.has(key)) { pieces.Store.logger?.(`[STORE => ${this.name}] [SET] Stopping existing cronjob task before creating a new one.`); this.get(key)?.job.stop(); } const timeZone = timezone ?? defaultTimezone; try { pieces.Store.logger?.( `[STORE => ${this.name}] [SET] Creating cronjob for ${key} with '${pattern}' as the pattern and '${timeZone}' for the timezone` ); value.job = new croner.Cron( pattern, { name: key, timezone: timeZone, paused: true, // we start the job manually once the client is ready protect: value.protect?.bind(value) ?? protect, catch: /* @__PURE__ */ __name((error) => { value.error("Encountered an error while running the cron job", error); sentry?.captureException(error); value.catch?.bind(value)(error, value.job); }, "catch"), ...options }, async () => { if (sentry && typeof pattern === "string" && !pattern.includes(":")) { await sentry.withMonitor(key, value.run.bind(value), { schedule: { type: "crontab", value: normalizePattern_cjs.normalizePattern(pattern) }, timezone: timeZone }); } else { await value.run.bind(value)(); } } ); } catch (error) { value.error("Encountered an error while creating the cron job", error); void value.unload(); } return super.set(key, value); } /** * Deletes a task from the store and stops it if it's running. */ delete(key) { const task = this.get(key); if (task && !task.job.isStopped()) { task.job.stop(); } return super.delete(key); } /** * Stops all running cron jobs and clears the store. */ clear() { this.stopAll(); return super.clear(); } }; __name(_CronTaskStore, "CronTaskStore"); var CronTaskStore = _CronTaskStore; exports.CronTaskStore = CronTaskStore; //# sourceMappingURL=CronTaskStore.cjs.map //# sourceMappingURL=CronTaskStore.cjs.map