@sapphire/plugin-scheduled-tasks 10.0.2-next.ee64834.0 → 10.0.2-next.f9230eb.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/index.cjs +1 -8
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +1 -3
- package/dist/cjs/lib/ScheduledTaskHandler.cjs +0 -2
- package/dist/cjs/lib/ScheduledTaskHandler.cjs.map +1 -1
- package/dist/cjs/lib/types/ScheduledTaskTypes.cjs.map +1 -1
- package/dist/esm/index.d.mts +1 -3
- package/dist/esm/index.mjs +1 -2
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/lib/ScheduledTaskHandler.mjs +0 -2
- package/dist/esm/lib/ScheduledTaskHandler.mjs.map +1 -1
- package/dist/esm/lib/types/ScheduledTaskTypes.mjs.map +1 -1
- package/package.json +7 -7
package/dist/cjs/index.cjs
CHANGED
|
@@ -4,11 +4,10 @@ var ScheduledTaskHandler_cjs = require('./lib/ScheduledTaskHandler.cjs');
|
|
|
4
4
|
var ScheduledTask_cjs = require('./lib/structures/ScheduledTask.cjs');
|
|
5
5
|
var ScheduledTaskStore_cjs = require('./lib/structures/ScheduledTaskStore.cjs');
|
|
6
6
|
var ScheduledTaskEvents_cjs = require('./lib/types/ScheduledTaskEvents.cjs');
|
|
7
|
-
var ScheduledTaskTypes_cjs = require('./lib/types/ScheduledTaskTypes.cjs');
|
|
8
7
|
var _load_cjs = require('./listeners/_load.cjs');
|
|
9
8
|
|
|
10
9
|
// src/index.ts
|
|
11
|
-
var version = "10.0.2-next.
|
|
10
|
+
var version = "10.0.2-next.f9230eb.0";
|
|
12
11
|
|
|
13
12
|
Object.defineProperty(exports, "loadListeners", {
|
|
14
13
|
enumerable: true,
|
|
@@ -39,11 +38,5 @@ Object.keys(ScheduledTaskEvents_cjs).forEach(function (k) {
|
|
|
39
38
|
get: function () { return ScheduledTaskEvents_cjs[k]; }
|
|
40
39
|
});
|
|
41
40
|
});
|
|
42
|
-
Object.keys(ScheduledTaskTypes_cjs).forEach(function (k) {
|
|
43
|
-
if (k !== 'default' && !Object.prototype.hasOwnProperty.call(exports, k)) Object.defineProperty(exports, k, {
|
|
44
|
-
enumerable: true,
|
|
45
|
-
get: function () { return ScheduledTaskTypes_cjs[k]; }
|
|
46
|
-
});
|
|
47
|
-
});
|
|
48
41
|
//# sourceMappingURL=out.js.map
|
|
49
42
|
//# sourceMappingURL=index.cjs.map
|
package/dist/cjs/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";AAIA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";AAIA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AAGd,SAAS,qBAAqB;AA8BvB,IAAM,UAAkB","sourcesContent":["import type { ScheduledTaskHandler } from './lib/ScheduledTaskHandler';\nimport type { ScheduledTaskStore } from './lib/structures/ScheduledTaskStore';\nimport type { ScheduledTaskHandlerOptions } from './lib/types/ScheduledTaskTypes';\n\nexport * from './lib/ScheduledTaskHandler';\nexport * from './lib/structures/ScheduledTask';\nexport * from './lib/structures/ScheduledTaskStore';\nexport * from './lib/types/ScheduledTaskEvents';\nexport type * from './lib/types/ScheduledTaskTypes';\n\nexport { loadListeners } from './listeners/_load';\n\ndeclare module '@sapphire/pieces' {\n\tinterface Container {\n\t\ttasks: ScheduledTaskHandler;\n\t}\n\n\tinterface StoreRegistryEntries {\n\t\t'scheduled-tasks': ScheduledTaskStore;\n\t}\n}\n\ndeclare module 'discord.js' {\n\texport interface ClientOptions {\n\t\ttasks: ScheduledTaskHandlerOptions;\n\t\t/**\n\t\t * If the the pre-included scheduled task error listeners should be loaded\n\t\t * @default true\n\t\t */\n\t\tloadScheduledTaskErrorListeners?: boolean;\n\t}\n}\n\n/**\n * The [@sapphire/plugin-scheduled-tasks](https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks) version that you are currently using.\n * An example use of this is showing it of in a bot information command.\n *\n * Note to Sapphire developers: This needs to explicitly be `string` so it is not typed as the string that gets replaced by esbuild\n */\n// eslint-disable-next-line @typescript-eslint/no-inferrable-types\nexport const version: string = '10.0.2-next.f9230eb.0';\n"]}
|
package/dist/cjs/index.d.cts
CHANGED
|
@@ -201,7 +201,6 @@ declare class ScheduledTaskHandler {
|
|
|
201
201
|
* Creates a scheduled task.
|
|
202
202
|
*
|
|
203
203
|
* @param task - The task to be scheduled.
|
|
204
|
-
* @param payload - The payload for the task.
|
|
205
204
|
* @param options - The options for the task.
|
|
206
205
|
*/
|
|
207
206
|
create<T extends ScheduledTasksResolvable>(task: T, //
|
|
@@ -240,7 +239,6 @@ declare class ScheduledTaskHandler {
|
|
|
240
239
|
* Runs a scheduled task with the given name and payload.
|
|
241
240
|
*
|
|
242
241
|
* @param task - The name of the scheduled task to run.
|
|
243
|
-
* @param payload - The payload to pass to the scheduled task.
|
|
244
242
|
*
|
|
245
243
|
* @remarks `undefined` will be returned if the task was not found.
|
|
246
244
|
*/
|
|
@@ -358,4 +356,4 @@ declare module 'discord.js' {
|
|
|
358
356
|
*/
|
|
359
357
|
declare const version: string;
|
|
360
358
|
|
|
361
|
-
export { type BullClient, ScheduledTask, type ScheduledTaskCreateRepeatedTask, type ScheduledTaskCustomJobOptions, ScheduledTaskEvents, ScheduledTaskHandler, type ScheduledTaskHandlerOptions, type ScheduledTaskListOptions, type ScheduledTaskListRepeatedOptions, type ScheduledTaskListRepeatedReturnType, type ScheduledTaskOptions, ScheduledTaskStore, type ScheduledTasks, type ScheduledTasksJob, type ScheduledTasksKeys, type ScheduledTasksKeysNoPayload, type ScheduledTasksPayload, type ScheduledTasksResolvable, type ScheduledTasksResolvablePayload, type ScheduledTasksTaskOptions, loadListeners, version };
|
|
359
|
+
export { type BullClient, ScheduledTask, type ScheduledTaskCreateRepeatedTask, type ScheduledTaskCustomJobOptions, ScheduledTaskEvents, ScheduledTaskHandler, type ScheduledTaskHandlerOptions, type ScheduledTaskListOptions, type ScheduledTaskListRepeatedOptions, type ScheduledTaskListRepeatedReturnType, type ScheduledTaskOptions, ScheduledTaskStore, type ScheduledTasks, type ScheduledTasksJob, type ScheduledTasksKeys, type ScheduledTasksKeysNoPayload, type ScheduledTasksPayload, type ScheduledTasksResolvable, type ScheduledTasksResolvableNoPayload, type ScheduledTasksResolvablePayload, type ScheduledTasksTaskOptions, loadListeners, version };
|
|
@@ -85,7 +85,6 @@ var _ScheduledTaskHandler = class _ScheduledTaskHandler {
|
|
|
85
85
|
* Creates a scheduled task.
|
|
86
86
|
*
|
|
87
87
|
* @param task - The task to be scheduled.
|
|
88
|
-
* @param payload - The payload for the task.
|
|
89
88
|
* @param options - The options for the task.
|
|
90
89
|
*/
|
|
91
90
|
async create(task, options) {
|
|
@@ -181,7 +180,6 @@ var _ScheduledTaskHandler = class _ScheduledTaskHandler {
|
|
|
181
180
|
* Runs a scheduled task with the given name and payload.
|
|
182
181
|
*
|
|
183
182
|
* @param task - The name of the scheduled task to run.
|
|
184
|
-
* @param payload - The payload to pass to the scheduled task.
|
|
185
183
|
*
|
|
186
184
|
* @remarks `undefined` will be returned if the task was not found.
|
|
187
185
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/lib/ScheduledTaskHandler.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,WAAW,cAAc;AAClC,SAAS,iBAAiB;AAC1B,SAAc,OAAO,QAA6C,4BAA4B;AAE9F,SAAS,2BAA2B;AAgBpC,SAAS,iBAAiB;AApB1B;AAsBO,IAAM,wBAAN,MAAM,sBAAqB;AAAA,EAc1B,YAAY,SAAsC;AAVzD;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAEhB;AACA;AAGC,SAAK,QAAQ,QAAQ,SAA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YAAY,MAAiE;AACpF,QAAI,OAAO,SAAS,UAAU;AAC7B,aAAO,EAAE,MAAM,MAAM,SAAS,OAAU;AAAA,IACzC;AAEA,QAAI,aAAa,MAAM;AACtB,aAAO,EAAE,MAAM,KAAK,MAAM,SAAS,KAAK,QAAQ;AAAA,IACjD;AAEA,WAAO,EAAE,MAAM,KAAK,MAAM,SAAS,OAAU;AAAA,EAC9C;AACD;AAxNC;AACA;AAZiC;AAA3B,IAAM,uBAAN","sourcesContent":["import { container, Result } from '@sapphire/framework';\nimport { Stopwatch } from '@sapphire/stopwatch';\nimport { Job, Queue, Worker, type JobsOptions, type QueueOptions, isNotConnectionError } from 'bullmq';\nimport type { ScheduledTaskStore } from './structures/ScheduledTaskStore';\nimport { ScheduledTaskEvents } from './types/ScheduledTaskEvents';\nimport type {\n\tBullClient,\n\tScheduledTaskCreateRepeatedTask,\n\tScheduledTaskHandlerOptions,\n\tScheduledTaskListOptions,\n\tScheduledTaskListRepeatedOptions,\n\tScheduledTaskListRepeatedReturnType,\n\tScheduledTasksResolvable,\n\tScheduledTasksKeys,\n\tScheduledTasksTaskOptions,\n\tScheduledTasksKeysNoPayload,\n\tScheduledTasksJob,\n\tScheduledTasksResolvablePayload,\n\tScheduledTasksPayload\n} from './types/ScheduledTaskTypes';\nimport { isNullish } from '@sapphire/utilities';\n\nexport class ScheduledTaskHandler {\n\t/**\n\t * The queue options for the scheduled task handler.\n\t */\n\tpublic readonly options: QueueOptions;\n\n\t/**\n\t * The name of the queue associated with the scheduled task handler.\n\t */\n\tpublic readonly queue: string;\n\n\t#client: BullClient;\n\t#worker: Worker;\n\n\tpublic constructor(options: ScheduledTaskHandlerOptions) {\n\t\tthis.queue = options.queue ?? 'scheduled-tasks';\n\t\tthis.options = options.bull;\n\n\t\tthis.#client = new Queue(this.queue, this.options);\n\t\tthis.#worker = new Worker(\n\t\t\tthis.queue, //\n\t\t\tasync (job) => this.run({ name: job.name as ScheduledTasksKeys, payload: job.data }),\n\t\t\t{ connection: this.options.connection }\n\t\t);\n\n\t\tthis.#client.on('error', (error) => {\n\t\t\tif (isNotConnectionError(error)) {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyClientError, error);\n\t\t\t} else {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyConnectError, error);\n\t\t\t}\n\t\t});\n\t\tthis.#worker.on('error', (error) => {\n\t\t\tif (isNotConnectionError(error)) {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyWorkerError, error);\n\t\t\t} else {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyConnectError, error);\n\t\t\t}\n\t\t});\n\t}\n\n\tpublic get client(): BullClient {\n\t\treturn this.#client;\n\t}\n\n\t/**\n\t * Closes the internal client and worker.\n\t */\n\tpublic async close(): Promise<void> {\n\t\tawait Promise.all([\n\t\t\tthis.#client.close(), //\n\t\t\tthis.#worker.close()\n\t\t]);\n\t}\n\n\t/**\n\t * Creates a scheduled task.\n\t *\n\t * @param task - The task to be scheduled.\n\t * @param payload - The payload for the task.\n\t * @param options - The options for the task.\n\t */\n\tpublic async create<T extends ScheduledTasksResolvable>(\n\t\ttask: T, //\n\t\toptions?: ScheduledTasksTaskOptions | number\n\t): Promise<ScheduledTasksJob<T>> {\n\t\tconst { name: taskName, payload } = this.resolveTask(task);\n\n\t\tif (isNullish(options)) {\n\t\t\treturn this.#client.add(taskName, payload) as Promise<ScheduledTasksJob<T>>;\n\t\t}\n\n\t\tif (typeof options === 'number') {\n\t\t\toptions = {\n\t\t\t\trepeated: false,\n\t\t\t\tdelay: options\n\t\t\t};\n\t\t}\n\n\t\tconst { repeated, pattern, interval, delay, customJobOptions, timezone } = options;\n\t\tlet jobOptions: JobsOptions = {\n\t\t\tdelay,\n\t\t\t...customJobOptions\n\t\t};\n\n\t\tif (repeated) {\n\t\t\tjobOptions = {\n\t\t\t\t...jobOptions,\n\t\t\t\trepeat: interval //\n\t\t\t\t\t? { every: interval }\n\t\t\t\t\t: { pattern, tz: timezone }\n\t\t\t};\n\t\t}\n\n\t\treturn this.#client.add(taskName, payload, jobOptions) as Promise<ScheduledTasksJob<T>>;\n\t}\n\n\t/**\n\t * Creates repeated tasks.\n\t *\n\t * @param tasks - An optional array of tasks to create. If not provided, it will create tasks based on the stored repeated tasks.\n\t */\n\tpublic async createRepeated(tasks?: ScheduledTaskCreateRepeatedTask[]): Promise<void> {\n\t\tif (tasks === undefined) {\n\t\t\tconst repeatedTasks: ScheduledTaskCreateRepeatedTask[] = this.store.repeatedTasks.map((piece) => ({\n\t\t\t\tname: piece.name as ScheduledTasksKeysNoPayload,\n\t\t\t\toptions: {\n\t\t\t\t\trepeated: true,\n\t\t\t\t\t...(piece.interval\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tinterval: piece.interval,\n\t\t\t\t\t\t\t\tcustomJobOptions: piece.customJobOptions\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tpattern: piece.pattern!,\n\t\t\t\t\t\t\t\ttimezone: piece.timezone,\n\t\t\t\t\t\t\t\tcustomJobOptions: piece.customJobOptions\n\t\t\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}));\n\n\t\t\ttasks = repeatedTasks;\n\t\t}\n\n\t\tfor (const task of tasks) {\n\t\t\tawait this.create(task.name, task.options);\n\t\t}\n\t}\n\n\t/**\n\t * Deletes a scheduled task by its ID.\n\t *\n\t * @param id - The ID of the task to delete.\n\t */\n\tpublic async delete(id: string): Promise<void> {\n\t\tconst job = (await this.get(id as ScheduledTasksKeys)) as unknown as Job<unknown> | undefined;\n\t\treturn job?.remove();\n\t}\n\n\t/**\n\t * Retrieves a list of scheduled tasks based on the provided options.\n\t *\n\t * @param options - The options for filtering the list of scheduled tasks.\n\t */\n\tpublic list(options: ScheduledTaskListOptions): Promise<Job<unknown>[]> {\n\t\tconst { types, start, end, asc } = options;\n\n\t\treturn this.#client.getJobs(types, start, end, asc);\n\t}\n\n\t/**\n\t * Retrieves a list of repeated scheduled tasks based on the provided options.\n\t *\n\t * @param options - The options for filtering the list of repeated scheduled tasks.\n\t */\n\tpublic listRepeated(options: ScheduledTaskListRepeatedOptions): Promise<ScheduledTaskListRepeatedReturnType> {\n\t\tconst { start, end, asc } = options;\n\n\t\treturn this.#client.getRepeatableJobs(start, end, asc);\n\t}\n\n\t/**\n\t * Retrieves a scheduled task by its ID.\n\t *\n\t * @param id - The ID of the scheduled task to retrieve.\n\t */\n\tpublic async get<T extends ScheduledTasksKeys>(id: T): Promise<Job<ScheduledTasksPayload<T>> | undefined> {\n\t\tconst job = await this.#client.getJob(id);\n\t\tif (isNullish(job)) return undefined;\n\n\t\treturn job as Job<ScheduledTasksPayload<T>>;\n\t}\n\n\t/**\n\t * Runs a scheduled task with the given name and payload.\n\t *\n\t * @param task - The name of the scheduled task to run.\n\t * @param payload - The payload to pass to the scheduled task.\n\t *\n\t * @remarks `undefined` will be returned if the task was not found.\n\t */\n\tpublic async run(task: ScheduledTasksResolvable): Promise<number | null | undefined> {\n\t\tconst { name: taskName, payload } = this.resolveTask(task);\n\t\tconst piece = this.store.get(task.name);\n\n\t\tif (!piece) {\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskNotFound, taskName, payload);\n\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst result = await Result.fromAsync(async () => {\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskRun, piece, payload);\n\n\t\t\tconst stopwatch = new Stopwatch();\n\t\t\tconst taskRunResult = await piece.run(payload);\n\t\t\tconst { duration } = stopwatch.stop();\n\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskSuccess, piece, payload, taskRunResult, duration);\n\n\t\t\treturn duration;\n\t\t});\n\n\t\tresult.inspectErr((error) => container.client.emit(ScheduledTaskEvents.ScheduledTaskError, error, piece, payload));\n\n\t\tconst value = result.unwrapOr(null);\n\n\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskFinished, piece, value, payload);\n\n\t\treturn value;\n\t}\n\n\tprivate get store(): ScheduledTaskStore {\n\t\treturn container.client.stores.get('scheduled-tasks');\n\t}\n\n\tprivate resolveTask(task: ScheduledTasksResolvable): ScheduledTasksResolvablePayload {\n\t\tif (typeof task === 'string') {\n\t\t\treturn { name: task, payload: undefined };\n\t\t}\n\n\t\tif ('payload' in task) {\n\t\t\treturn { name: task.name, payload: task.payload };\n\t\t}\n\n\t\treturn { name: task.name, payload: undefined };\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/ScheduledTaskHandler.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,WAAW,cAAc;AAClC,SAAS,iBAAiB;AAC1B,SAAc,OAAO,QAA6C,4BAA4B;AAE9F,SAAS,2BAA2B;AAgBpC,SAAS,iBAAiB;AApB1B;AAsBO,IAAM,wBAAN,MAAM,sBAAqB;AAAA,EAc1B,YAAY,SAAsC;AAVzD;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAEhB;AACA;AAGC,SAAK,QAAQ,QAAQ,SAAS;AAC9B,SAAK,UAAU,QAAQ;AAEvB,uBAAK,SAAU,IAAI,MAAM,KAAK,OAAO,KAAK,OAAO;AACjD,uBAAK,SAAU,IAAI;AAAA,MAClB,KAAK;AAAA;AAAA,MACL,OAAO,QAAQ,KAAK,IAAI,EAAE,MAAM,IAAI,MAA4B,SAAS,IAAI,KAAK,CAAC;AAAA,MACnF,EAAE,YAAY,KAAK,QAAQ,WAAW;AAAA,IACvC;AAEA,uBAAK,SAAQ,GAAG,SAAS,CAAC,UAAU;AACnC,UAAI,qBAAqB,KAAK,GAAG;AAChC,kBAAU,OAAO,KAAK,oBAAoB,kCAAkC,KAAK;AAAA,MAClF,OAAO;AACN,kBAAU,OAAO,KAAK,oBAAoB,mCAAmC,KAAK;AAAA,MACnF;AAA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,IAAI,KAAK,IAAI;AAEtC,QAAI,CAAC,OAAO;AACX,gBAAU,OAAO,KAAK,oBAAoB,uBAAuB,UAAU,OAAO;AAElF,aAAO;AAAA,IACR;AAEA,UAAM,SAAS,MAAM,OAAO,UAAU,YAAY;AACjD,gBAAU,OAAO,KAAK,oBAAoB,kBAAkB,OAAO,OAAO;AAE1E,YAAM,YAAY,IAAI,UAAU;AAChC,YAAM,gBAAgB,MAAM,MAAM,IAAI,OAAO;AAC7C,YAAM,EAAE,SAAS,IAAI,UAAU,KAAK;AAEpC,gBAAU,OAAO,KAAK,oBAAoB,sBAAsB,OAAO,SAAS,eAAe,QAAQ;AAEvG,aAAO;AAAA,IACR,CAAC;AAED,WAAO,WAAW,CAAC,UAAU,UAAU,OAAO,KAAK,oBAAoB,oBAAoB,OAAO,OAAO,OAAO,CAAC;AAEjH,UAAM,QAAQ,OAAO,SAAS,IAAI;AAElC,cAAU,OAAO,KAAK,oBAAoB,uBAAuB,OAAO,OAAO,OAAO;AAEtF,WAAO;AAAA,EACR;AAAA,EAEA,IAAY,QAA4B;AACvC,WAAO,UAAU,OAAO,OAAO,IAAI,iBAAiB;AAAA,EACrD;AAAA,EAEQ,YAAY,MAAiE;AACpF,QAAI,OAAO,SAAS,UAAU;AAC7B,aAAO,EAAE,MAAM,MAAM,SAAS,OAAU;AAAA,IACzC;AAEA,QAAI,aAAa,MAAM;AACtB,aAAO,EAAE,MAAM,KAAK,MAAM,SAAS,KAAK,QAAQ;AAAA,IACjD;AAEA,WAAO,EAAE,MAAM,KAAK,MAAM,SAAS,OAAU;AAAA,EAC9C;AACD;AAtNC;AACA;AAZiC;AAA3B,IAAM,uBAAN","sourcesContent":["import { container, Result } from '@sapphire/framework';\nimport { Stopwatch } from '@sapphire/stopwatch';\nimport { Job, Queue, Worker, type JobsOptions, type QueueOptions, isNotConnectionError } from 'bullmq';\nimport type { ScheduledTaskStore } from './structures/ScheduledTaskStore';\nimport { ScheduledTaskEvents } from './types/ScheduledTaskEvents';\nimport type {\n\tBullClient,\n\tScheduledTaskCreateRepeatedTask,\n\tScheduledTaskHandlerOptions,\n\tScheduledTaskListOptions,\n\tScheduledTaskListRepeatedOptions,\n\tScheduledTaskListRepeatedReturnType,\n\tScheduledTasksResolvable,\n\tScheduledTasksKeys,\n\tScheduledTasksTaskOptions,\n\tScheduledTasksKeysNoPayload,\n\tScheduledTasksJob,\n\tScheduledTasksResolvablePayload,\n\tScheduledTasksPayload\n} from './types/ScheduledTaskTypes';\nimport { isNullish } from '@sapphire/utilities';\n\nexport class ScheduledTaskHandler {\n\t/**\n\t * The queue options for the scheduled task handler.\n\t */\n\tpublic readonly options: QueueOptions;\n\n\t/**\n\t * The name of the queue associated with the scheduled task handler.\n\t */\n\tpublic readonly queue: string;\n\n\t#client: BullClient;\n\t#worker: Worker;\n\n\tpublic constructor(options: ScheduledTaskHandlerOptions) {\n\t\tthis.queue = options.queue ?? 'scheduled-tasks';\n\t\tthis.options = options.bull;\n\n\t\tthis.#client = new Queue(this.queue, this.options);\n\t\tthis.#worker = new Worker(\n\t\t\tthis.queue, //\n\t\t\tasync (job) => this.run({ name: job.name as ScheduledTasksKeys, payload: job.data }),\n\t\t\t{ connection: this.options.connection }\n\t\t);\n\n\t\tthis.#client.on('error', (error) => {\n\t\t\tif (isNotConnectionError(error)) {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyClientError, error);\n\t\t\t} else {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyConnectError, error);\n\t\t\t}\n\t\t});\n\t\tthis.#worker.on('error', (error) => {\n\t\t\tif (isNotConnectionError(error)) {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyWorkerError, error);\n\t\t\t} else {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyConnectError, error);\n\t\t\t}\n\t\t});\n\t}\n\n\tpublic get client(): BullClient {\n\t\treturn this.#client;\n\t}\n\n\t/**\n\t * Closes the internal client and worker.\n\t */\n\tpublic async close(): Promise<void> {\n\t\tawait Promise.all([\n\t\t\tthis.#client.close(), //\n\t\t\tthis.#worker.close()\n\t\t]);\n\t}\n\n\t/**\n\t * Creates a scheduled task.\n\t *\n\t * @param task - The task to be scheduled.\n\t * @param options - The options for the task.\n\t */\n\tpublic async create<T extends ScheduledTasksResolvable>(\n\t\ttask: T, //\n\t\toptions?: ScheduledTasksTaskOptions | number\n\t): Promise<ScheduledTasksJob<T>> {\n\t\tconst { name: taskName, payload } = this.resolveTask(task);\n\n\t\tif (isNullish(options)) {\n\t\t\treturn this.#client.add(taskName, payload) as Promise<ScheduledTasksJob<T>>;\n\t\t}\n\n\t\tif (typeof options === 'number') {\n\t\t\toptions = {\n\t\t\t\trepeated: false,\n\t\t\t\tdelay: options\n\t\t\t};\n\t\t}\n\n\t\tconst { repeated, pattern, interval, delay, customJobOptions, timezone } = options;\n\t\tlet jobOptions: JobsOptions = {\n\t\t\tdelay,\n\t\t\t...customJobOptions\n\t\t};\n\n\t\tif (repeated) {\n\t\t\tjobOptions = {\n\t\t\t\t...jobOptions,\n\t\t\t\trepeat: interval //\n\t\t\t\t\t? { every: interval }\n\t\t\t\t\t: { pattern, tz: timezone }\n\t\t\t};\n\t\t}\n\n\t\treturn this.#client.add(taskName, payload, jobOptions) as Promise<ScheduledTasksJob<T>>;\n\t}\n\n\t/**\n\t * Creates repeated tasks.\n\t *\n\t * @param tasks - An optional array of tasks to create. If not provided, it will create tasks based on the stored repeated tasks.\n\t */\n\tpublic async createRepeated(tasks?: ScheduledTaskCreateRepeatedTask[]): Promise<void> {\n\t\tif (tasks === undefined) {\n\t\t\tconst repeatedTasks: ScheduledTaskCreateRepeatedTask[] = this.store.repeatedTasks.map((piece) => ({\n\t\t\t\tname: piece.name as ScheduledTasksKeysNoPayload,\n\t\t\t\toptions: {\n\t\t\t\t\trepeated: true,\n\t\t\t\t\t...(piece.interval\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tinterval: piece.interval,\n\t\t\t\t\t\t\t\tcustomJobOptions: piece.customJobOptions\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tpattern: piece.pattern!,\n\t\t\t\t\t\t\t\ttimezone: piece.timezone,\n\t\t\t\t\t\t\t\tcustomJobOptions: piece.customJobOptions\n\t\t\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}));\n\n\t\t\ttasks = repeatedTasks;\n\t\t}\n\n\t\tfor (const task of tasks) {\n\t\t\tawait this.create(task.name, task.options);\n\t\t}\n\t}\n\n\t/**\n\t * Deletes a scheduled task by its ID.\n\t *\n\t * @param id - The ID of the task to delete.\n\t */\n\tpublic async delete(id: string): Promise<void> {\n\t\tconst job = (await this.get(id as ScheduledTasksKeys)) as unknown as Job<unknown> | undefined;\n\t\treturn job?.remove();\n\t}\n\n\t/**\n\t * Retrieves a list of scheduled tasks based on the provided options.\n\t *\n\t * @param options - The options for filtering the list of scheduled tasks.\n\t */\n\tpublic list(options: ScheduledTaskListOptions): Promise<Job<unknown>[]> {\n\t\tconst { types, start, end, asc } = options;\n\n\t\treturn this.#client.getJobs(types, start, end, asc);\n\t}\n\n\t/**\n\t * Retrieves a list of repeated scheduled tasks based on the provided options.\n\t *\n\t * @param options - The options for filtering the list of repeated scheduled tasks.\n\t */\n\tpublic listRepeated(options: ScheduledTaskListRepeatedOptions): Promise<ScheduledTaskListRepeatedReturnType> {\n\t\tconst { start, end, asc } = options;\n\n\t\treturn this.#client.getRepeatableJobs(start, end, asc);\n\t}\n\n\t/**\n\t * Retrieves a scheduled task by its ID.\n\t *\n\t * @param id - The ID of the scheduled task to retrieve.\n\t */\n\tpublic async get<T extends ScheduledTasksKeys>(id: T): Promise<Job<ScheduledTasksPayload<T>> | undefined> {\n\t\tconst job = await this.#client.getJob(id);\n\t\tif (isNullish(job)) return undefined;\n\n\t\treturn job as Job<ScheduledTasksPayload<T>>;\n\t}\n\n\t/**\n\t * Runs a scheduled task with the given name and payload.\n\t *\n\t * @param task - The name of the scheduled task to run.\n\t *\n\t * @remarks `undefined` will be returned if the task was not found.\n\t */\n\tpublic async run(task: ScheduledTasksResolvable): Promise<number | null | undefined> {\n\t\tconst { name: taskName, payload } = this.resolveTask(task);\n\t\tconst piece = this.store.get(task.name);\n\n\t\tif (!piece) {\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskNotFound, taskName, payload);\n\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst result = await Result.fromAsync(async () => {\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskRun, piece, payload);\n\n\t\t\tconst stopwatch = new Stopwatch();\n\t\t\tconst taskRunResult = await piece.run(payload);\n\t\t\tconst { duration } = stopwatch.stop();\n\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskSuccess, piece, payload, taskRunResult, duration);\n\n\t\t\treturn duration;\n\t\t});\n\n\t\tresult.inspectErr((error) => container.client.emit(ScheduledTaskEvents.ScheduledTaskError, error, piece, payload));\n\n\t\tconst value = result.unwrapOr(null);\n\n\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskFinished, piece, value, payload);\n\n\t\treturn value;\n\t}\n\n\tprivate get store(): ScheduledTaskStore {\n\t\treturn container.client.stores.get('scheduled-tasks');\n\t}\n\n\tprivate resolveTask(task: ScheduledTasksResolvable): ScheduledTasksResolvablePayload {\n\t\tif (typeof task === 'string') {\n\t\t\treturn { name: task, payload: undefined };\n\t\t}\n\n\t\tif ('payload' in task) {\n\t\t\treturn { name: task.name, payload: task.payload };\n\t\t}\n\n\t\treturn { name: task.name, payload: undefined };\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/lib/types/ScheduledTaskTypes.ts"],"names":[],"mappings":";AAAA,OAAkE","sourcesContent":["import { Queue, type JobState, type QueueOptions, type Job } from 'bullmq';\nimport type { ScheduledTaskCustomJobOptions } from '../structures/ScheduledTask';\n\n/**\n * Options for a scheduled task handler.\n */\nexport interface ScheduledTaskHandlerOptions {\n\tqueue?: string;\n\tbull: QueueOptions;\n}\n\n/**\n * Options for listing repeated scheduled tasks.\n */\nexport interface ScheduledTaskListRepeatedOptions {\n\tstart?: number;\n\tend?: number;\n\tasc?: boolean;\n}\n\n/**\n * Options for listing scheduled tasks.\n */\nexport interface ScheduledTaskListOptions extends ScheduledTaskListRepeatedOptions {\n\ttypes: JobState[];\n}\n\n/**\n * A Bull queue client that can be used to schedule and manage scheduled tasks.\n */\nexport type BullClient = Queue<unknown>;\n\n/**\n * The return type of the `getRepeatableJobs` method of a Bull client.\n * If the return type of `getRepeatableJobs` is a Promise, this type extracts the resolved type.\n */\nexport type ScheduledTaskListRepeatedReturnType = ReturnType<BullClient['getRepeatableJobs']> extends Promise<infer U> ? U : never;\n\n/**\n * Options for a scheduled task.\n */\nexport type ScheduledTasksTaskOptions = {\n\trepeated: boolean;\n} & (\n\t| { delay: number; interval?: never; pattern?: never; timezone?: never; customJobOptions?: ScheduledTaskCustomJobOptions }\n\t| { delay?: never; interval: number; pattern?: never; timezone?: never; customJobOptions?: ScheduledTaskCustomJobOptions }\n\t| { delay?: never; interval?: never; pattern: string; timezone: string; customJobOptions?: ScheduledTaskCustomJobOptions }\n);\n\n/**\n * Represents a scheduled task that will be repeated at a specified interval.\n */\nexport interface ScheduledTaskCreateRepeatedTask {\n\tname: ScheduledTasksKeysNoPayload;\n\toptions: ScheduledTasksTaskOptions;\n}\n\n/**\n * The registered tasks and their payload types. When registering new ones, it is recommended to use\n * [module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation) so\n * custom ones are registered.\n *\n * @remarks\n * - If the type is `never` or `undefined`, that means that there is no payload associated with that task.\n * - If the type is a union that contains `undefined`, then the payload will be optional.\n *\n * @example\n * ```typescript\n * declare module '@sapphire/plugin-scheduled-tasks' {\n * interface ScheduledTasks {\n * // A task named `Mute` which requires no payload:\n * Mute: never;\n *\n * // A task named `Unmute` which requires a payload with a `userId` property:\n * Unmute: {\n * userId: string;\n * };\n *\n * // A task named `Ban` which has a payload with an optional `moderatorId` property:\n * LogAction: {\n * moderatorId?: string;\n * };\n * }\n * }\n * ```\n */\nexport interface ScheduledTasks {}\n\n/**\n * The keys of {@link ScheduledTasks}.\n */\nexport type ScheduledTasksKeys = keyof ScheduledTasks;\n\n/**\n * The keys of {@link ScheduledTasks} with an optional or no payload.\n */\nexport type ScheduledTasksKeysNoPayload = {\n\t[K in ScheduledTasksKeys]: ScheduledTasks[K] extends never //\n\t\t? K\n\t\t: undefined extends ScheduledTasks[K] // Check if the type is a union that contains `undefined`\n\t\t\t? K\n\t\t\t: never;\n}[ScheduledTasksKeys];\n\n/**\n * The payload type of a {@link ScheduledTasks} entry.\n */\nexport type ScheduledTasksPayload<\n\tK extends ScheduledTasksKeys = ScheduledTasksKeys //\n> = ScheduledTasksKeys extends never // Need to check `never` due to empty interfaces\n\t? undefined\n\t: ScheduledTasks[K] extends never // If the value is `never`, then there is no payload\n\t\t? undefined\n\t\t: ScheduledTasks[K];\n\n/**\n * An entry in {@link ScheduledTasks} that has no payload.\n */\
|
|
1
|
+
{"version":3,"sources":["../../../../src/lib/types/ScheduledTaskTypes.ts"],"names":[],"mappings":";AAAA,OAAkE","sourcesContent":["import { Queue, type JobState, type QueueOptions, type Job } from 'bullmq';\nimport type { ScheduledTaskCustomJobOptions } from '../structures/ScheduledTask';\n\n/**\n * Options for a scheduled task handler.\n */\nexport interface ScheduledTaskHandlerOptions {\n\tqueue?: string;\n\tbull: QueueOptions;\n}\n\n/**\n * Options for listing repeated scheduled tasks.\n */\nexport interface ScheduledTaskListRepeatedOptions {\n\tstart?: number;\n\tend?: number;\n\tasc?: boolean;\n}\n\n/**\n * Options for listing scheduled tasks.\n */\nexport interface ScheduledTaskListOptions extends ScheduledTaskListRepeatedOptions {\n\ttypes: JobState[];\n}\n\n/**\n * A Bull queue client that can be used to schedule and manage scheduled tasks.\n */\nexport type BullClient = Queue<unknown>;\n\n/**\n * The return type of the `getRepeatableJobs` method of a Bull client.\n * If the return type of `getRepeatableJobs` is a Promise, this type extracts the resolved type.\n */\nexport type ScheduledTaskListRepeatedReturnType = ReturnType<BullClient['getRepeatableJobs']> extends Promise<infer U> ? U : never;\n\n/**\n * Options for a scheduled task.\n */\nexport type ScheduledTasksTaskOptions = {\n\trepeated: boolean;\n} & (\n\t| { delay: number; interval?: never; pattern?: never; timezone?: never; customJobOptions?: ScheduledTaskCustomJobOptions }\n\t| { delay?: never; interval: number; pattern?: never; timezone?: never; customJobOptions?: ScheduledTaskCustomJobOptions }\n\t| { delay?: never; interval?: never; pattern: string; timezone: string; customJobOptions?: ScheduledTaskCustomJobOptions }\n);\n\n/**\n * Represents a scheduled task that will be repeated at a specified interval.\n */\nexport interface ScheduledTaskCreateRepeatedTask {\n\tname: ScheduledTasksKeysNoPayload;\n\toptions: ScheduledTasksTaskOptions;\n}\n\n/**\n * The registered tasks and their payload types. When registering new ones, it is recommended to use\n * [module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation) so\n * custom ones are registered.\n *\n * @remarks\n * - If the type is `never` or `undefined`, that means that there is no payload associated with that task.\n * - If the type is a union that contains `undefined`, then the payload will be optional.\n *\n * @example\n * ```typescript\n * declare module '@sapphire/plugin-scheduled-tasks' {\n * interface ScheduledTasks {\n * // A task named `Mute` which requires no payload:\n * Mute: never;\n *\n * // A task named `Unmute` which requires a payload with a `userId` property:\n * Unmute: {\n * userId: string;\n * };\n *\n * // A task named `Ban` which has a payload with an optional `moderatorId` property:\n * LogAction: {\n * moderatorId?: string;\n * };\n * }\n * }\n * ```\n */\nexport interface ScheduledTasks {}\n\n/**\n * The keys of {@link ScheduledTasks}.\n */\nexport type ScheduledTasksKeys = keyof ScheduledTasks;\n\n/**\n * The keys of {@link ScheduledTasks} with an optional or no payload.\n */\nexport type ScheduledTasksKeysNoPayload = {\n\t[K in ScheduledTasksKeys]: ScheduledTasks[K] extends never //\n\t\t? K\n\t\t: undefined extends ScheduledTasks[K] // Check if the type is a union that contains `undefined`\n\t\t\t? K\n\t\t\t: never;\n}[ScheduledTasksKeys];\n\n/**\n * The payload type of a {@link ScheduledTasks} entry.\n */\nexport type ScheduledTasksPayload<\n\tK extends ScheduledTasksKeys = ScheduledTasksKeys //\n> = ScheduledTasksKeys extends never // Need to check `never` due to empty interfaces\n\t? undefined\n\t: ScheduledTasks[K] extends never // If the value is `never`, then there is no payload\n\t\t? undefined\n\t\t: ScheduledTasks[K];\n\n/**\n * An entry in {@link ScheduledTasks} that has no payload.\n */\nexport interface ScheduledTasksResolvableNoPayload<\n\tK extends ScheduledTasksKeysNoPayload = ScheduledTasksKeysNoPayload //\n> {\n\tname: K;\n}\n\n/**\n * An entry in {@link ScheduledTasks} with it's associated payload.\n */\nexport interface ScheduledTasksResolvablePayload<\n\tK extends ScheduledTasksKeys = ScheduledTasksKeys //\n> {\n\tname: K;\n\tpayload: ScheduledTasksPayload<K>;\n}\n\nexport type ScheduledTasksResolvable =\n\t| ScheduledTasksKeysNoPayload //\n\t| ScheduledTasksResolvableNoPayload\n\t| ScheduledTasksResolvablePayload;\n\nexport type ScheduledTasksJob<T> =\n\tT extends ScheduledTasksResolvableNoPayload<infer R>\n\t\t? Job<ScheduledTasksPayload<R>>\n\t\t: T extends ScheduledTasksResolvablePayload<infer R>\n\t\t\t? Job<ScheduledTasksPayload<R>>\n\t\t\t: Job<undefined>;\n"]}
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -201,7 +201,6 @@ declare class ScheduledTaskHandler {
|
|
|
201
201
|
* Creates a scheduled task.
|
|
202
202
|
*
|
|
203
203
|
* @param task - The task to be scheduled.
|
|
204
|
-
* @param payload - The payload for the task.
|
|
205
204
|
* @param options - The options for the task.
|
|
206
205
|
*/
|
|
207
206
|
create<T extends ScheduledTasksResolvable>(task: T, //
|
|
@@ -240,7 +239,6 @@ declare class ScheduledTaskHandler {
|
|
|
240
239
|
* Runs a scheduled task with the given name and payload.
|
|
241
240
|
*
|
|
242
241
|
* @param task - The name of the scheduled task to run.
|
|
243
|
-
* @param payload - The payload to pass to the scheduled task.
|
|
244
242
|
*
|
|
245
243
|
* @remarks `undefined` will be returned if the task was not found.
|
|
246
244
|
*/
|
|
@@ -358,4 +356,4 @@ declare module 'discord.js' {
|
|
|
358
356
|
*/
|
|
359
357
|
declare const version: string;
|
|
360
358
|
|
|
361
|
-
export { type BullClient, ScheduledTask, type ScheduledTaskCreateRepeatedTask, type ScheduledTaskCustomJobOptions, ScheduledTaskEvents, ScheduledTaskHandler, type ScheduledTaskHandlerOptions, type ScheduledTaskListOptions, type ScheduledTaskListRepeatedOptions, type ScheduledTaskListRepeatedReturnType, type ScheduledTaskOptions, ScheduledTaskStore, type ScheduledTasks, type ScheduledTasksJob, type ScheduledTasksKeys, type ScheduledTasksKeysNoPayload, type ScheduledTasksPayload, type ScheduledTasksResolvable, type ScheduledTasksResolvablePayload, type ScheduledTasksTaskOptions, loadListeners, version };
|
|
359
|
+
export { type BullClient, ScheduledTask, type ScheduledTaskCreateRepeatedTask, type ScheduledTaskCustomJobOptions, ScheduledTaskEvents, ScheduledTaskHandler, type ScheduledTaskHandlerOptions, type ScheduledTaskListOptions, type ScheduledTaskListRepeatedOptions, type ScheduledTaskListRepeatedReturnType, type ScheduledTaskOptions, ScheduledTaskStore, type ScheduledTasks, type ScheduledTasksJob, type ScheduledTasksKeys, type ScheduledTasksKeysNoPayload, type ScheduledTasksPayload, type ScheduledTasksResolvable, type ScheduledTasksResolvableNoPayload, type ScheduledTasksResolvablePayload, type ScheduledTasksTaskOptions, loadListeners, version };
|
package/dist/esm/index.mjs
CHANGED
|
@@ -3,10 +3,9 @@ export * from './lib/ScheduledTaskHandler.mjs';
|
|
|
3
3
|
export * from './lib/structures/ScheduledTask.mjs';
|
|
4
4
|
export * from './lib/structures/ScheduledTaskStore.mjs';
|
|
5
5
|
export * from './lib/types/ScheduledTaskEvents.mjs';
|
|
6
|
-
export * from './lib/types/ScheduledTaskTypes.mjs';
|
|
7
6
|
export { loadListeners } from './listeners/_load.mjs';
|
|
8
7
|
|
|
9
|
-
var version = "10.0.2-next.
|
|
8
|
+
var version = "10.0.2-next.f9230eb.0";
|
|
10
9
|
|
|
11
10
|
export { version };
|
|
12
11
|
//# sourceMappingURL=out.js.map
|
package/dist/esm/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";;;AAIA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";;;AAIA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AAGd,SAAS,qBAAqB;AA8BvB,IAAM,UAAkB","sourcesContent":["import type { ScheduledTaskHandler } from './lib/ScheduledTaskHandler';\nimport type { ScheduledTaskStore } from './lib/structures/ScheduledTaskStore';\nimport type { ScheduledTaskHandlerOptions } from './lib/types/ScheduledTaskTypes';\n\nexport * from './lib/ScheduledTaskHandler';\nexport * from './lib/structures/ScheduledTask';\nexport * from './lib/structures/ScheduledTaskStore';\nexport * from './lib/types/ScheduledTaskEvents';\nexport type * from './lib/types/ScheduledTaskTypes';\n\nexport { loadListeners } from './listeners/_load';\n\ndeclare module '@sapphire/pieces' {\n\tinterface Container {\n\t\ttasks: ScheduledTaskHandler;\n\t}\n\n\tinterface StoreRegistryEntries {\n\t\t'scheduled-tasks': ScheduledTaskStore;\n\t}\n}\n\ndeclare module 'discord.js' {\n\texport interface ClientOptions {\n\t\ttasks: ScheduledTaskHandlerOptions;\n\t\t/**\n\t\t * If the the pre-included scheduled task error listeners should be loaded\n\t\t * @default true\n\t\t */\n\t\tloadScheduledTaskErrorListeners?: boolean;\n\t}\n}\n\n/**\n * The [@sapphire/plugin-scheduled-tasks](https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks) version that you are currently using.\n * An example use of this is showing it of in a bot information command.\n *\n * Note to Sapphire developers: This needs to explicitly be `string` so it is not typed as the string that gets replaced by esbuild\n */\n// eslint-disable-next-line @typescript-eslint/no-inferrable-types\nexport const version: string = '10.0.2-next.f9230eb.0';\n"]}
|
|
@@ -59,7 +59,6 @@ var _ScheduledTaskHandler = class _ScheduledTaskHandler {
|
|
|
59
59
|
* Creates a scheduled task.
|
|
60
60
|
*
|
|
61
61
|
* @param task - The task to be scheduled.
|
|
62
|
-
* @param payload - The payload for the task.
|
|
63
62
|
* @param options - The options for the task.
|
|
64
63
|
*/
|
|
65
64
|
async create(task, options) {
|
|
@@ -155,7 +154,6 @@ var _ScheduledTaskHandler = class _ScheduledTaskHandler {
|
|
|
155
154
|
* Runs a scheduled task with the given name and payload.
|
|
156
155
|
*
|
|
157
156
|
* @param task - The name of the scheduled task to run.
|
|
158
|
-
* @param payload - The payload to pass to the scheduled task.
|
|
159
157
|
*
|
|
160
158
|
* @remarks `undefined` will be returned if the task was not found.
|
|
161
159
|
*/
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../src/lib/ScheduledTaskHandler.ts"],"names":[],"mappings":";;;;;;;;;AAAA,SAAS,WAAW,cAAc;AAClC,SAAS,iBAAiB;AAC1B,SAAc,OAAO,QAA6C,4BAA4B;AAE9F,SAAS,2BAA2B;AAgBpC,SAAS,iBAAiB;AApB1B;AAsBO,IAAM,wBAAN,MAAM,sBAAqB;AAAA,EAc1B,YAAY,SAAsC;AAVzD;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAEhB;AACA;AAGC,SAAK,QAAQ,QAAQ,SAAS;AAC9B,SAAK,UAAU,QAAQ;AAEvB,uBAAK,SAAU,IAAI,MAAM,KAAK,OAAO,KAAK,OAAO;AACjD,uBAAK,SAAU,IAAI;AAAA,MAClB,KAAK;AAAA;AAAA,MACL,OAAO,QAAQ,KAAK,IAAI,EAAE,MAAM,IAAI,MAA4B,SAAS,IAAI,KAAK,CAAC;AAAA,MACn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S,OAAU;AAAA,EAC9C;AACD;AAxNC;AACA;AAZiC;AAA3B,IAAM,uBAAN","sourcesContent":["import { container, Result } from '@sapphire/framework';\nimport { Stopwatch } from '@sapphire/stopwatch';\nimport { Job, Queue, Worker, type JobsOptions, type QueueOptions, isNotConnectionError } from 'bullmq';\nimport type { ScheduledTaskStore } from './structures/ScheduledTaskStore';\nimport { ScheduledTaskEvents } from './types/ScheduledTaskEvents';\nimport type {\n\tBullClient,\n\tScheduledTaskCreateRepeatedTask,\n\tScheduledTaskHandlerOptions,\n\tScheduledTaskListOptions,\n\tScheduledTaskListRepeatedOptions,\n\tScheduledTaskListRepeatedReturnType,\n\tScheduledTasksResolvable,\n\tScheduledTasksKeys,\n\tScheduledTasksTaskOptions,\n\tScheduledTasksKeysNoPayload,\n\tScheduledTasksJob,\n\tScheduledTasksResolvablePayload,\n\tScheduledTasksPayload\n} from './types/ScheduledTaskTypes';\nimport { isNullish } from '@sapphire/utilities';\n\nexport class ScheduledTaskHandler {\n\t/**\n\t * The queue options for the scheduled task handler.\n\t */\n\tpublic readonly options: QueueOptions;\n\n\t/**\n\t * The name of the queue associated with the scheduled task handler.\n\t */\n\tpublic readonly queue: string;\n\n\t#client: BullClient;\n\t#worker: Worker;\n\n\tpublic constructor(options: ScheduledTaskHandlerOptions) {\n\t\tthis.queue = options.queue ?? 'scheduled-tasks';\n\t\tthis.options = options.bull;\n\n\t\tthis.#client = new Queue(this.queue, this.options);\n\t\tthis.#worker = new Worker(\n\t\t\tthis.queue, //\n\t\t\tasync (job) => this.run({ name: job.name as ScheduledTasksKeys, payload: job.data }),\n\t\t\t{ connection: this.options.connection }\n\t\t);\n\n\t\tthis.#client.on('error', (error) => {\n\t\t\tif (isNotConnectionError(error)) {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyClientError, error);\n\t\t\t} else {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyConnectError, error);\n\t\t\t}\n\t\t});\n\t\tthis.#worker.on('error', (error) => {\n\t\t\tif (isNotConnectionError(error)) {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyWorkerError, error);\n\t\t\t} else {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyConnectError, error);\n\t\t\t}\n\t\t});\n\t}\n\n\tpublic get client(): BullClient {\n\t\treturn this.#client;\n\t}\n\n\t/**\n\t * Closes the internal client and worker.\n\t */\n\tpublic async close(): Promise<void> {\n\t\tawait Promise.all([\n\t\t\tthis.#client.close(), //\n\t\t\tthis.#worker.close()\n\t\t]);\n\t}\n\n\t/**\n\t * Creates a scheduled task.\n\t *\n\t * @param task - The task to be scheduled.\n\t * @param payload - The payload for the task.\n\t * @param options - The options for the task.\n\t */\n\tpublic async create<T extends ScheduledTasksResolvable>(\n\t\ttask: T, //\n\t\toptions?: ScheduledTasksTaskOptions | number\n\t): Promise<ScheduledTasksJob<T>> {\n\t\tconst { name: taskName, payload } = this.resolveTask(task);\n\n\t\tif (isNullish(options)) {\n\t\t\treturn this.#client.add(taskName, payload) as Promise<ScheduledTasksJob<T>>;\n\t\t}\n\n\t\tif (typeof options === 'number') {\n\t\t\toptions = {\n\t\t\t\trepeated: false,\n\t\t\t\tdelay: options\n\t\t\t};\n\t\t}\n\n\t\tconst { repeated, pattern, interval, delay, customJobOptions, timezone } = options;\n\t\tlet jobOptions: JobsOptions = {\n\t\t\tdelay,\n\t\t\t...customJobOptions\n\t\t};\n\n\t\tif (repeated) {\n\t\t\tjobOptions = {\n\t\t\t\t...jobOptions,\n\t\t\t\trepeat: interval //\n\t\t\t\t\t? { every: interval }\n\t\t\t\t\t: { pattern, tz: timezone }\n\t\t\t};\n\t\t}\n\n\t\treturn this.#client.add(taskName, payload, jobOptions) as Promise<ScheduledTasksJob<T>>;\n\t}\n\n\t/**\n\t * Creates repeated tasks.\n\t *\n\t * @param tasks - An optional array of tasks to create. If not provided, it will create tasks based on the stored repeated tasks.\n\t */\n\tpublic async createRepeated(tasks?: ScheduledTaskCreateRepeatedTask[]): Promise<void> {\n\t\tif (tasks === undefined) {\n\t\t\tconst repeatedTasks: ScheduledTaskCreateRepeatedTask[] = this.store.repeatedTasks.map((piece) => ({\n\t\t\t\tname: piece.name as ScheduledTasksKeysNoPayload,\n\t\t\t\toptions: {\n\t\t\t\t\trepeated: true,\n\t\t\t\t\t...(piece.interval\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tinterval: piece.interval,\n\t\t\t\t\t\t\t\tcustomJobOptions: piece.customJobOptions\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tpattern: piece.pattern!,\n\t\t\t\t\t\t\t\ttimezone: piece.timezone,\n\t\t\t\t\t\t\t\tcustomJobOptions: piece.customJobOptions\n\t\t\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}));\n\n\t\t\ttasks = repeatedTasks;\n\t\t}\n\n\t\tfor (const task of tasks) {\n\t\t\tawait this.create(task.name, task.options);\n\t\t}\n\t}\n\n\t/**\n\t * Deletes a scheduled task by its ID.\n\t *\n\t * @param id - The ID of the task to delete.\n\t */\n\tpublic async delete(id: string): Promise<void> {\n\t\tconst job = (await this.get(id as ScheduledTasksKeys)) as unknown as Job<unknown> | undefined;\n\t\treturn job?.remove();\n\t}\n\n\t/**\n\t * Retrieves a list of scheduled tasks based on the provided options.\n\t *\n\t * @param options - The options for filtering the list of scheduled tasks.\n\t */\n\tpublic list(options: ScheduledTaskListOptions): Promise<Job<unknown>[]> {\n\t\tconst { types, start, end, asc } = options;\n\n\t\treturn this.#client.getJobs(types, start, end, asc);\n\t}\n\n\t/**\n\t * Retrieves a list of repeated scheduled tasks based on the provided options.\n\t *\n\t * @param options - The options for filtering the list of repeated scheduled tasks.\n\t */\n\tpublic listRepeated(options: ScheduledTaskListRepeatedOptions): Promise<ScheduledTaskListRepeatedReturnType> {\n\t\tconst { start, end, asc } = options;\n\n\t\treturn this.#client.getRepeatableJobs(start, end, asc);\n\t}\n\n\t/**\n\t * Retrieves a scheduled task by its ID.\n\t *\n\t * @param id - The ID of the scheduled task to retrieve.\n\t */\n\tpublic async get<T extends ScheduledTasksKeys>(id: T): Promise<Job<ScheduledTasksPayload<T>> | undefined> {\n\t\tconst job = await this.#client.getJob(id);\n\t\tif (isNullish(job)) return undefined;\n\n\t\treturn job as Job<ScheduledTasksPayload<T>>;\n\t}\n\n\t/**\n\t * Runs a scheduled task with the given name and payload.\n\t *\n\t * @param task - The name of the scheduled task to run.\n\t * @param payload - The payload to pass to the scheduled task.\n\t *\n\t * @remarks `undefined` will be returned if the task was not found.\n\t */\n\tpublic async run(task: ScheduledTasksResolvable): Promise<number | null | undefined> {\n\t\tconst { name: taskName, payload } = this.resolveTask(task);\n\t\tconst piece = this.store.get(task.name);\n\n\t\tif (!piece) {\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskNotFound, taskName, payload);\n\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst result = await Result.fromAsync(async () => {\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskRun, piece, payload);\n\n\t\t\tconst stopwatch = new Stopwatch();\n\t\t\tconst taskRunResult = await piece.run(payload);\n\t\t\tconst { duration } = stopwatch.stop();\n\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskSuccess, piece, payload, taskRunResult, duration);\n\n\t\t\treturn duration;\n\t\t});\n\n\t\tresult.inspectErr((error) => container.client.emit(ScheduledTaskEvents.ScheduledTaskError, error, piece, payload));\n\n\t\tconst value = result.unwrapOr(null);\n\n\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskFinished, piece, value, payload);\n\n\t\treturn value;\n\t}\n\n\tprivate get store(): ScheduledTaskStore {\n\t\treturn container.client.stores.get('scheduled-tasks');\n\t}\n\n\tprivate resolveTask(task: ScheduledTasksResolvable): ScheduledTasksResolvablePayload {\n\t\tif (typeof task === 'string') {\n\t\t\treturn { name: task, payload: undefined };\n\t\t}\n\n\t\tif ('payload' in task) {\n\t\t\treturn { name: task.name, payload: task.payload };\n\t\t}\n\n\t\treturn { name: task.name, payload: undefined };\n\t}\n}\n"]}
|
|
1
|
+
{"version":3,"sources":["../../../src/lib/ScheduledTaskHandler.ts"],"names":[],"mappings":";;;;;;;;;AAAA,SAAS,WAAW,cAAc;AAClC,SAAS,iBAAiB;AAC1B,SAAc,OAAO,QAA6C,4BAA4B;AAE9F,SAAS,2BAA2B;AAgBpC,SAAS,iBAAiB;AApB1B;AAsBO,IAAM,wBAAN,MAAM,sBAAqB;AAAA,EAc1B,YAAY,SAAsC;AAVzD;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAEhB;AACA;AAGC,SAAK,QAAQ,QAAQ,SAAS;AAC9B,SAAK,UAAU,QAAQ;AAEvB,uBAAK,SAAU,IAAI,MAAM,KAAK,OAAO,KAAK,OAAO;AACjD,uBAAK,SAAU,IAAI;AAAA,MAClB,KAAK;AAAA;AAAA,MACL,OAAO,QAAQ,KAAK,IAAI,EAAE,MAAM,IAAI,MAA4B,SAAS,IAAI,KAAK,CAAC;AAAA,MACnF,EAAE,YAAY,KAAK,QAAQ,WAAW;AAAA,IACvC;AAEA,uBAAK,SAAQ,GAAG,SAAS,CAAC,UAAU;AACnC,UAAI,qBAAqB,KAAK,GAAG;AAChC,kBAAU,OAAO,KAAK,oBAAoB,kCAAkC,KAAK;AAAA,MAClF,OAAO;AACN,kBAAU,OAAO,KAAK,oBAAoB,mCAAmC,KAAK;AAAA,MACnF;AAAA,IACD,CAAC;AACD,uBAAK,SAAQ,GAAG,SAAS,CAAC,UAAU;AACnC,UAAI,qBAAqB,KAAK,GAAG;AAChC,kBAAU,OAAO,KAAK,oBAAoB,kCAAkC,KAAK;AAAA,MAClF,OAAO;AACN,kBAAU,OAAO,KAAK,oBAAoB,mCAAmC,KAAK;AAAA,MACnF;AAAA,IACD,CAAC;AAAA,EACF;AAAA,EAE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C,YAAM,gBAAgB,MAAM,MAAM,IAAI,OAAO;AAC7C,YAAM,EAAE,SAAS,IAAI,UAAU,KAAK;AAEpC,gBAAU,OAAO,KAAK,oBAAoB,sBAAsB,OAAO,SAAS,eAAe,QAAQ;AAEvG,aAAO;AAAA,IACR,CAAC;AAED,WAAO,WAAW,CAAC,UAAU,UAAU,OAAO,KAAK,oBAAoB,oBAAoB,OAAO,OAAO,OAAO,CAAC;AAEjH,UAAM,QAAQ,OAAO,SAAS,IAAI;AAElC,cAAU,OAAO,KAAK,oBAAoB,uBAAuB,OAAO,OAAO,OAAO;AAEtF,WAAO;AAAA,EACR;AAAA,EAEA,IAAY,QAA4B;AACvC,WAAO,UAAU,OAAO,OAAO,IAAI,iBAAiB;AAAA,EACrD;AAAA,EAEQ,YAAY,MAAiE;AACpF,QAAI,OAAO,SAAS,UAAU;AAC7B,aAAO,EAAE,MAAM,MAAM,SAAS,OAAU;AAAA,IACzC;AAEA,QAAI,aAAa,MAAM;AACtB,aAAO,EAAE,MAAM,KAAK,MAAM,SAAS,KAAK,QAAQ;AAAA,IACjD;AAEA,WAAO,EAAE,MAAM,KAAK,MAAM,SAAS,OAAU;AAAA,EAC9C;AACD;AAtNC;AACA;AAZiC;AAA3B,IAAM,uBAAN","sourcesContent":["import { container, Result } from '@sapphire/framework';\nimport { Stopwatch } from '@sapphire/stopwatch';\nimport { Job, Queue, Worker, type JobsOptions, type QueueOptions, isNotConnectionError } from 'bullmq';\nimport type { ScheduledTaskStore } from './structures/ScheduledTaskStore';\nimport { ScheduledTaskEvents } from './types/ScheduledTaskEvents';\nimport type {\n\tBullClient,\n\tScheduledTaskCreateRepeatedTask,\n\tScheduledTaskHandlerOptions,\n\tScheduledTaskListOptions,\n\tScheduledTaskListRepeatedOptions,\n\tScheduledTaskListRepeatedReturnType,\n\tScheduledTasksResolvable,\n\tScheduledTasksKeys,\n\tScheduledTasksTaskOptions,\n\tScheduledTasksKeysNoPayload,\n\tScheduledTasksJob,\n\tScheduledTasksResolvablePayload,\n\tScheduledTasksPayload\n} from './types/ScheduledTaskTypes';\nimport { isNullish } from '@sapphire/utilities';\n\nexport class ScheduledTaskHandler {\n\t/**\n\t * The queue options for the scheduled task handler.\n\t */\n\tpublic readonly options: QueueOptions;\n\n\t/**\n\t * The name of the queue associated with the scheduled task handler.\n\t */\n\tpublic readonly queue: string;\n\n\t#client: BullClient;\n\t#worker: Worker;\n\n\tpublic constructor(options: ScheduledTaskHandlerOptions) {\n\t\tthis.queue = options.queue ?? 'scheduled-tasks';\n\t\tthis.options = options.bull;\n\n\t\tthis.#client = new Queue(this.queue, this.options);\n\t\tthis.#worker = new Worker(\n\t\t\tthis.queue, //\n\t\t\tasync (job) => this.run({ name: job.name as ScheduledTasksKeys, payload: job.data }),\n\t\t\t{ connection: this.options.connection }\n\t\t);\n\n\t\tthis.#client.on('error', (error) => {\n\t\t\tif (isNotConnectionError(error)) {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyClientError, error);\n\t\t\t} else {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyConnectError, error);\n\t\t\t}\n\t\t});\n\t\tthis.#worker.on('error', (error) => {\n\t\t\tif (isNotConnectionError(error)) {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyWorkerError, error);\n\t\t\t} else {\n\t\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskStrategyConnectError, error);\n\t\t\t}\n\t\t});\n\t}\n\n\tpublic get client(): BullClient {\n\t\treturn this.#client;\n\t}\n\n\t/**\n\t * Closes the internal client and worker.\n\t */\n\tpublic async close(): Promise<void> {\n\t\tawait Promise.all([\n\t\t\tthis.#client.close(), //\n\t\t\tthis.#worker.close()\n\t\t]);\n\t}\n\n\t/**\n\t * Creates a scheduled task.\n\t *\n\t * @param task - The task to be scheduled.\n\t * @param options - The options for the task.\n\t */\n\tpublic async create<T extends ScheduledTasksResolvable>(\n\t\ttask: T, //\n\t\toptions?: ScheduledTasksTaskOptions | number\n\t): Promise<ScheduledTasksJob<T>> {\n\t\tconst { name: taskName, payload } = this.resolveTask(task);\n\n\t\tif (isNullish(options)) {\n\t\t\treturn this.#client.add(taskName, payload) as Promise<ScheduledTasksJob<T>>;\n\t\t}\n\n\t\tif (typeof options === 'number') {\n\t\t\toptions = {\n\t\t\t\trepeated: false,\n\t\t\t\tdelay: options\n\t\t\t};\n\t\t}\n\n\t\tconst { repeated, pattern, interval, delay, customJobOptions, timezone } = options;\n\t\tlet jobOptions: JobsOptions = {\n\t\t\tdelay,\n\t\t\t...customJobOptions\n\t\t};\n\n\t\tif (repeated) {\n\t\t\tjobOptions = {\n\t\t\t\t...jobOptions,\n\t\t\t\trepeat: interval //\n\t\t\t\t\t? { every: interval }\n\t\t\t\t\t: { pattern, tz: timezone }\n\t\t\t};\n\t\t}\n\n\t\treturn this.#client.add(taskName, payload, jobOptions) as Promise<ScheduledTasksJob<T>>;\n\t}\n\n\t/**\n\t * Creates repeated tasks.\n\t *\n\t * @param tasks - An optional array of tasks to create. If not provided, it will create tasks based on the stored repeated tasks.\n\t */\n\tpublic async createRepeated(tasks?: ScheduledTaskCreateRepeatedTask[]): Promise<void> {\n\t\tif (tasks === undefined) {\n\t\t\tconst repeatedTasks: ScheduledTaskCreateRepeatedTask[] = this.store.repeatedTasks.map((piece) => ({\n\t\t\t\tname: piece.name as ScheduledTasksKeysNoPayload,\n\t\t\t\toptions: {\n\t\t\t\t\trepeated: true,\n\t\t\t\t\t...(piece.interval\n\t\t\t\t\t\t? {\n\t\t\t\t\t\t\t\tinterval: piece.interval,\n\t\t\t\t\t\t\t\tcustomJobOptions: piece.customJobOptions\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t: {\n\t\t\t\t\t\t\t\tpattern: piece.pattern!,\n\t\t\t\t\t\t\t\ttimezone: piece.timezone,\n\t\t\t\t\t\t\t\tcustomJobOptions: piece.customJobOptions\n\t\t\t\t\t\t\t})\n\t\t\t\t}\n\t\t\t}));\n\n\t\t\ttasks = repeatedTasks;\n\t\t}\n\n\t\tfor (const task of tasks) {\n\t\t\tawait this.create(task.name, task.options);\n\t\t}\n\t}\n\n\t/**\n\t * Deletes a scheduled task by its ID.\n\t *\n\t * @param id - The ID of the task to delete.\n\t */\n\tpublic async delete(id: string): Promise<void> {\n\t\tconst job = (await this.get(id as ScheduledTasksKeys)) as unknown as Job<unknown> | undefined;\n\t\treturn job?.remove();\n\t}\n\n\t/**\n\t * Retrieves a list of scheduled tasks based on the provided options.\n\t *\n\t * @param options - The options for filtering the list of scheduled tasks.\n\t */\n\tpublic list(options: ScheduledTaskListOptions): Promise<Job<unknown>[]> {\n\t\tconst { types, start, end, asc } = options;\n\n\t\treturn this.#client.getJobs(types, start, end, asc);\n\t}\n\n\t/**\n\t * Retrieves a list of repeated scheduled tasks based on the provided options.\n\t *\n\t * @param options - The options for filtering the list of repeated scheduled tasks.\n\t */\n\tpublic listRepeated(options: ScheduledTaskListRepeatedOptions): Promise<ScheduledTaskListRepeatedReturnType> {\n\t\tconst { start, end, asc } = options;\n\n\t\treturn this.#client.getRepeatableJobs(start, end, asc);\n\t}\n\n\t/**\n\t * Retrieves a scheduled task by its ID.\n\t *\n\t * @param id - The ID of the scheduled task to retrieve.\n\t */\n\tpublic async get<T extends ScheduledTasksKeys>(id: T): Promise<Job<ScheduledTasksPayload<T>> | undefined> {\n\t\tconst job = await this.#client.getJob(id);\n\t\tif (isNullish(job)) return undefined;\n\n\t\treturn job as Job<ScheduledTasksPayload<T>>;\n\t}\n\n\t/**\n\t * Runs a scheduled task with the given name and payload.\n\t *\n\t * @param task - The name of the scheduled task to run.\n\t *\n\t * @remarks `undefined` will be returned if the task was not found.\n\t */\n\tpublic async run(task: ScheduledTasksResolvable): Promise<number | null | undefined> {\n\t\tconst { name: taskName, payload } = this.resolveTask(task);\n\t\tconst piece = this.store.get(task.name);\n\n\t\tif (!piece) {\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskNotFound, taskName, payload);\n\n\t\t\treturn undefined;\n\t\t}\n\n\t\tconst result = await Result.fromAsync(async () => {\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskRun, piece, payload);\n\n\t\t\tconst stopwatch = new Stopwatch();\n\t\t\tconst taskRunResult = await piece.run(payload);\n\t\t\tconst { duration } = stopwatch.stop();\n\n\t\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskSuccess, piece, payload, taskRunResult, duration);\n\n\t\t\treturn duration;\n\t\t});\n\n\t\tresult.inspectErr((error) => container.client.emit(ScheduledTaskEvents.ScheduledTaskError, error, piece, payload));\n\n\t\tconst value = result.unwrapOr(null);\n\n\t\tcontainer.client.emit(ScheduledTaskEvents.ScheduledTaskFinished, piece, value, payload);\n\n\t\treturn value;\n\t}\n\n\tprivate get store(): ScheduledTaskStore {\n\t\treturn container.client.stores.get('scheduled-tasks');\n\t}\n\n\tprivate resolveTask(task: ScheduledTasksResolvable): ScheduledTasksResolvablePayload {\n\t\tif (typeof task === 'string') {\n\t\t\treturn { name: task, payload: undefined };\n\t\t}\n\n\t\tif ('payload' in task) {\n\t\t\treturn { name: task.name, payload: task.payload };\n\t\t}\n\n\t\treturn { name: task.name, payload: undefined };\n\t}\n}\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../../../src/lib/types/ScheduledTaskTypes.ts"],"names":[],"mappings":";AAAA,OAAkE","sourcesContent":["import { Queue, type JobState, type QueueOptions, type Job } from 'bullmq';\nimport type { ScheduledTaskCustomJobOptions } from '../structures/ScheduledTask';\n\n/**\n * Options for a scheduled task handler.\n */\nexport interface ScheduledTaskHandlerOptions {\n\tqueue?: string;\n\tbull: QueueOptions;\n}\n\n/**\n * Options for listing repeated scheduled tasks.\n */\nexport interface ScheduledTaskListRepeatedOptions {\n\tstart?: number;\n\tend?: number;\n\tasc?: boolean;\n}\n\n/**\n * Options for listing scheduled tasks.\n */\nexport interface ScheduledTaskListOptions extends ScheduledTaskListRepeatedOptions {\n\ttypes: JobState[];\n}\n\n/**\n * A Bull queue client that can be used to schedule and manage scheduled tasks.\n */\nexport type BullClient = Queue<unknown>;\n\n/**\n * The return type of the `getRepeatableJobs` method of a Bull client.\n * If the return type of `getRepeatableJobs` is a Promise, this type extracts the resolved type.\n */\nexport type ScheduledTaskListRepeatedReturnType = ReturnType<BullClient['getRepeatableJobs']> extends Promise<infer U> ? U : never;\n\n/**\n * Options for a scheduled task.\n */\nexport type ScheduledTasksTaskOptions = {\n\trepeated: boolean;\n} & (\n\t| { delay: number; interval?: never; pattern?: never; timezone?: never; customJobOptions?: ScheduledTaskCustomJobOptions }\n\t| { delay?: never; interval: number; pattern?: never; timezone?: never; customJobOptions?: ScheduledTaskCustomJobOptions }\n\t| { delay?: never; interval?: never; pattern: string; timezone: string; customJobOptions?: ScheduledTaskCustomJobOptions }\n);\n\n/**\n * Represents a scheduled task that will be repeated at a specified interval.\n */\nexport interface ScheduledTaskCreateRepeatedTask {\n\tname: ScheduledTasksKeysNoPayload;\n\toptions: ScheduledTasksTaskOptions;\n}\n\n/**\n * The registered tasks and their payload types. When registering new ones, it is recommended to use\n * [module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation) so\n * custom ones are registered.\n *\n * @remarks\n * - If the type is `never` or `undefined`, that means that there is no payload associated with that task.\n * - If the type is a union that contains `undefined`, then the payload will be optional.\n *\n * @example\n * ```typescript\n * declare module '@sapphire/plugin-scheduled-tasks' {\n * interface ScheduledTasks {\n * // A task named `Mute` which requires no payload:\n * Mute: never;\n *\n * // A task named `Unmute` which requires a payload with a `userId` property:\n * Unmute: {\n * userId: string;\n * };\n *\n * // A task named `Ban` which has a payload with an optional `moderatorId` property:\n * LogAction: {\n * moderatorId?: string;\n * };\n * }\n * }\n * ```\n */\nexport interface ScheduledTasks {}\n\n/**\n * The keys of {@link ScheduledTasks}.\n */\nexport type ScheduledTasksKeys = keyof ScheduledTasks;\n\n/**\n * The keys of {@link ScheduledTasks} with an optional or no payload.\n */\nexport type ScheduledTasksKeysNoPayload = {\n\t[K in ScheduledTasksKeys]: ScheduledTasks[K] extends never //\n\t\t? K\n\t\t: undefined extends ScheduledTasks[K] // Check if the type is a union that contains `undefined`\n\t\t\t? K\n\t\t\t: never;\n}[ScheduledTasksKeys];\n\n/**\n * The payload type of a {@link ScheduledTasks} entry.\n */\nexport type ScheduledTasksPayload<\n\tK extends ScheduledTasksKeys = ScheduledTasksKeys //\n> = ScheduledTasksKeys extends never // Need to check `never` due to empty interfaces\n\t? undefined\n\t: ScheduledTasks[K] extends never // If the value is `never`, then there is no payload\n\t\t? undefined\n\t\t: ScheduledTasks[K];\n\n/**\n * An entry in {@link ScheduledTasks} that has no payload.\n */\
|
|
1
|
+
{"version":3,"sources":["../../../../src/lib/types/ScheduledTaskTypes.ts"],"names":[],"mappings":";AAAA,OAAkE","sourcesContent":["import { Queue, type JobState, type QueueOptions, type Job } from 'bullmq';\nimport type { ScheduledTaskCustomJobOptions } from '../structures/ScheduledTask';\n\n/**\n * Options for a scheduled task handler.\n */\nexport interface ScheduledTaskHandlerOptions {\n\tqueue?: string;\n\tbull: QueueOptions;\n}\n\n/**\n * Options for listing repeated scheduled tasks.\n */\nexport interface ScheduledTaskListRepeatedOptions {\n\tstart?: number;\n\tend?: number;\n\tasc?: boolean;\n}\n\n/**\n * Options for listing scheduled tasks.\n */\nexport interface ScheduledTaskListOptions extends ScheduledTaskListRepeatedOptions {\n\ttypes: JobState[];\n}\n\n/**\n * A Bull queue client that can be used to schedule and manage scheduled tasks.\n */\nexport type BullClient = Queue<unknown>;\n\n/**\n * The return type of the `getRepeatableJobs` method of a Bull client.\n * If the return type of `getRepeatableJobs` is a Promise, this type extracts the resolved type.\n */\nexport type ScheduledTaskListRepeatedReturnType = ReturnType<BullClient['getRepeatableJobs']> extends Promise<infer U> ? U : never;\n\n/**\n * Options for a scheduled task.\n */\nexport type ScheduledTasksTaskOptions = {\n\trepeated: boolean;\n} & (\n\t| { delay: number; interval?: never; pattern?: never; timezone?: never; customJobOptions?: ScheduledTaskCustomJobOptions }\n\t| { delay?: never; interval: number; pattern?: never; timezone?: never; customJobOptions?: ScheduledTaskCustomJobOptions }\n\t| { delay?: never; interval?: never; pattern: string; timezone: string; customJobOptions?: ScheduledTaskCustomJobOptions }\n);\n\n/**\n * Represents a scheduled task that will be repeated at a specified interval.\n */\nexport interface ScheduledTaskCreateRepeatedTask {\n\tname: ScheduledTasksKeysNoPayload;\n\toptions: ScheduledTasksTaskOptions;\n}\n\n/**\n * The registered tasks and their payload types. When registering new ones, it is recommended to use\n * [module augmentation](https://www.typescriptlang.org/docs/handbook/declaration-merging.html#module-augmentation) so\n * custom ones are registered.\n *\n * @remarks\n * - If the type is `never` or `undefined`, that means that there is no payload associated with that task.\n * - If the type is a union that contains `undefined`, then the payload will be optional.\n *\n * @example\n * ```typescript\n * declare module '@sapphire/plugin-scheduled-tasks' {\n * interface ScheduledTasks {\n * // A task named `Mute` which requires no payload:\n * Mute: never;\n *\n * // A task named `Unmute` which requires a payload with a `userId` property:\n * Unmute: {\n * userId: string;\n * };\n *\n * // A task named `Ban` which has a payload with an optional `moderatorId` property:\n * LogAction: {\n * moderatorId?: string;\n * };\n * }\n * }\n * ```\n */\nexport interface ScheduledTasks {}\n\n/**\n * The keys of {@link ScheduledTasks}.\n */\nexport type ScheduledTasksKeys = keyof ScheduledTasks;\n\n/**\n * The keys of {@link ScheduledTasks} with an optional or no payload.\n */\nexport type ScheduledTasksKeysNoPayload = {\n\t[K in ScheduledTasksKeys]: ScheduledTasks[K] extends never //\n\t\t? K\n\t\t: undefined extends ScheduledTasks[K] // Check if the type is a union that contains `undefined`\n\t\t\t? K\n\t\t\t: never;\n}[ScheduledTasksKeys];\n\n/**\n * The payload type of a {@link ScheduledTasks} entry.\n */\nexport type ScheduledTasksPayload<\n\tK extends ScheduledTasksKeys = ScheduledTasksKeys //\n> = ScheduledTasksKeys extends never // Need to check `never` due to empty interfaces\n\t? undefined\n\t: ScheduledTasks[K] extends never // If the value is `never`, then there is no payload\n\t\t? undefined\n\t\t: ScheduledTasks[K];\n\n/**\n * An entry in {@link ScheduledTasks} that has no payload.\n */\nexport interface ScheduledTasksResolvableNoPayload<\n\tK extends ScheduledTasksKeysNoPayload = ScheduledTasksKeysNoPayload //\n> {\n\tname: K;\n}\n\n/**\n * An entry in {@link ScheduledTasks} with it's associated payload.\n */\nexport interface ScheduledTasksResolvablePayload<\n\tK extends ScheduledTasksKeys = ScheduledTasksKeys //\n> {\n\tname: K;\n\tpayload: ScheduledTasksPayload<K>;\n}\n\nexport type ScheduledTasksResolvable =\n\t| ScheduledTasksKeysNoPayload //\n\t| ScheduledTasksResolvableNoPayload\n\t| ScheduledTasksResolvablePayload;\n\nexport type ScheduledTasksJob<T> =\n\tT extends ScheduledTasksResolvableNoPayload<infer R>\n\t\t? Job<ScheduledTasksPayload<R>>\n\t\t: T extends ScheduledTasksResolvablePayload<infer R>\n\t\t\t? Job<ScheduledTasksPayload<R>>\n\t\t\t: Job<undefined>;\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sapphire/plugin-scheduled-tasks",
|
|
3
|
-
"version": "10.0.2-next.
|
|
3
|
+
"version": "10.0.2-next.f9230eb.0",
|
|
4
4
|
"description": "Plugin for @sapphire/framework to have scheduled tasks",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -51,17 +51,17 @@
|
|
|
51
51
|
"dependencies": {
|
|
52
52
|
"@sapphire/stopwatch": "^1.5.2",
|
|
53
53
|
"@sapphire/utilities": "^3.15.3",
|
|
54
|
-
"bullmq": "5.
|
|
54
|
+
"bullmq": "5.7.8"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|
|
57
|
-
"@favware/cliff-jumper": "^
|
|
57
|
+
"@favware/cliff-jumper": "^3.0.3",
|
|
58
58
|
"@favware/rollup-type-bundler": "^3.3.0",
|
|
59
59
|
"concurrently": "^8.2.2",
|
|
60
60
|
"tsup": "^8.0.2",
|
|
61
|
-
"tsx": "^4.
|
|
62
|
-
"typedoc": "^0.25.
|
|
63
|
-
"typedoc-json-parser": "^
|
|
64
|
-
"typescript": "^5.
|
|
61
|
+
"tsx": "^4.9.1",
|
|
62
|
+
"typedoc": "^0.25.13",
|
|
63
|
+
"typedoc-json-parser": "^10.0.0",
|
|
64
|
+
"typescript": "^5.4.5"
|
|
65
65
|
},
|
|
66
66
|
"repository": {
|
|
67
67
|
"type": "git",
|