@sapphire/plugin-scheduled-tasks 10.0.0-next.9fbf980.0 → 10.0.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/CHANGELOG.md +15 -0
- package/UPGRADING-v9-v10.md +8 -0
- package/dist/cjs/index.cjs +1 -1
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.ts +3 -3
- package/dist/cjs/lib/ScheduledTaskHandler.cjs +2 -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 +3 -3
- package/dist/esm/index.mjs +1 -1
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/lib/ScheduledTaskHandler.mjs +2 -2
- package/dist/esm/lib/ScheduledTaskHandler.mjs.map +1 -1
- package/dist/esm/lib/types/ScheduledTaskTypes.mjs.map +1 -1
- package/package.json +2 -2
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,21 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
|
|
5
|
+
# [@sapphire/plugin-scheduled-tasks@10.0.0](https://github.com/sapphiredev/plugins/compare/@sapphire/plugin-scheduled-tasks@10.0.0...@sapphire/plugin-scheduled-tasks@10.0.0) - (2023-12-27)
|
|
6
|
+
|
|
7
|
+
## 🚀 Features
|
|
8
|
+
|
|
9
|
+
- Bullmq v5 (#529) ([4278ab0](https://github.com/sapphiredev/plugins/commit/4278ab035989ec99bf0f7233f98e4ff75b57f71d))
|
|
10
|
+
- 💥 **BREAKING CHANGE:** This plugin now uses bullmq v5.1.0 (previously v3.15.8). Refer to the bullmq breaking changes from v4.0.0 onwards for any breaking changes that they have included https://github.com/taskforcesh/bullmq/blob/master/docs/gitbook/changelog.md#400-2023-06-21
|
|
11
|
+
- **scheduled-tasks:** Enforce payload types, clearer Redis/BullMQ errors (#509) ([9fbf980](https://github.com/sapphiredev/plugins/commit/9fbf980e603022c3e1cd61a9ad742cd43a60e7fa))
|
|
12
|
+
- 💥 **BREAKING CHANGE:** Refer to https://github.com/sapphiredev/plugins/blob/main/packages/scheduled-tasks/UPGRADING-v9-v10.md for upgrading instructions.
|
|
13
|
+
- 💥 **BREAKING CHANGE:** payload types are now enforced, you may need to update your code to match.
|
|
14
|
+
- 💥 **BREAKING CHANGE:** The `ScheduledTaskJob` interface has been removed in favor of defining types on `ScheduledTasks`.
|
|
15
|
+
- 💥 **BREAKING CHANGE:** Due to the removal of `ScheduledTaskJob`, the `BullClient` will now be typed as `unknown` since the Job types in the Queue can not _really_ be known. So you will need to do validation when interacting directly with the client.
|
|
16
|
+
- 💥 **BREAKING CHANGE:** The included error listeners are now enabled by default. If you want them to be disabled, just set `loadScheduledTaskErrorListeners` to false in the `SapphireClient` options.
|
|
17
|
+
- 💥 **BREAKING CHANGE:** The internal BullMQ client does not actually throw any errors, it just emits them from the client. As such, those error events will now be sent to the corresponding error listener registered by the plugin.
|
|
18
|
+
- 💥 **BREAKING CHANGE:** The error listeners previously only returned the name of the task when an error was emitted, but now the event will provide the associated Piece.
|
|
19
|
+
|
|
5
20
|
# [@sapphire/plugin-scheduled-tasks@9.1.0](https://github.com/sapphiredev/plugins/compare/@sapphire/plugin-scheduled-tasks@9.1.0...@sapphire/plugin-scheduled-tasks@9.1.0) - (2023-12-18)
|
|
6
21
|
|
|
7
22
|
## 🚀 Features
|
package/UPGRADING-v9-v10.md
CHANGED
|
@@ -66,3 +66,11 @@ The internal BullMQ client does not actually throw any errors, it just emits the
|
|
|
66
66
|
### Error payload types
|
|
67
67
|
|
|
68
68
|
The error listeners previously only returned the name of the task when an error was emitted, but now the event will provide the associated Piece.
|
|
69
|
+
|
|
70
|
+
## BullMQ v5
|
|
71
|
+
|
|
72
|
+
The BullMQ dependency is being updated to v5 from v3. If you depend on the internal BullMQ client in any way, check out their releases for breaking changes.
|
|
73
|
+
|
|
74
|
+
### Required `tasks` client property
|
|
75
|
+
|
|
76
|
+
The `tasks` property for options relating to this plugin are now required. They were previously optional which was an oversight, as a connection string is always needed.
|
package/dist/cjs/index.cjs
CHANGED
|
@@ -8,7 +8,7 @@ var ScheduledTaskTypes_cjs = require('./lib/types/ScheduledTaskTypes.cjs');
|
|
|
8
8
|
var _load_cjs = require('./listeners/_load.cjs');
|
|
9
9
|
|
|
10
10
|
// src/index.ts
|
|
11
|
-
var version = "10.0.0
|
|
11
|
+
var version = "10.0.0";
|
|
12
12
|
|
|
13
13
|
Object.defineProperty(exports, 'loadListeners', {
|
|
14
14
|
enumerable: true,
|
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;AACd,cAAc;AAEd,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 * 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
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";AAIA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AAEd,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 * 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.0';\n"]}
|
package/dist/cjs/index.d.ts
CHANGED
|
@@ -64,7 +64,7 @@ declare namespace ScheduledTask {
|
|
|
64
64
|
*/
|
|
65
65
|
interface ScheduledTaskHandlerOptions {
|
|
66
66
|
queue?: string;
|
|
67
|
-
bull
|
|
67
|
+
bull: QueueOptions;
|
|
68
68
|
}
|
|
69
69
|
/**
|
|
70
70
|
* Options for listing repeated scheduled tasks.
|
|
@@ -191,7 +191,7 @@ declare class ScheduledTaskHandler {
|
|
|
191
191
|
* The name of the queue associated with the scheduled task handler.
|
|
192
192
|
*/
|
|
193
193
|
readonly queue: string;
|
|
194
|
-
constructor(options
|
|
194
|
+
constructor(options: ScheduledTaskHandlerOptions);
|
|
195
195
|
get client(): BullClient;
|
|
196
196
|
/**
|
|
197
197
|
* Closes the internal client and worker.
|
|
@@ -342,7 +342,7 @@ declare module '@sapphire/pieces' {
|
|
|
342
342
|
}
|
|
343
343
|
declare module 'discord.js' {
|
|
344
344
|
interface ClientOptions {
|
|
345
|
-
tasks
|
|
345
|
+
tasks: ScheduledTaskHandlerOptions;
|
|
346
346
|
/**
|
|
347
347
|
* If the the pre-included scheduled task error listeners should be loaded
|
|
348
348
|
* @default true
|
|
@@ -44,8 +44,8 @@ var _ScheduledTaskHandler = class _ScheduledTaskHandler {
|
|
|
44
44
|
__publicField(this, "queue");
|
|
45
45
|
__privateAdd(this, _client, void 0);
|
|
46
46
|
__privateAdd(this, _worker, void 0);
|
|
47
|
-
this.queue = options
|
|
48
|
-
this.options = options
|
|
47
|
+
this.queue = options.queue ?? "scheduled-tasks";
|
|
48
|
+
this.options = options.bull;
|
|
49
49
|
__privateSet(this, _client, new bullmq.Queue(this.queue, this.options));
|
|
50
50
|
__privateSet(this, _worker, new bullmq.Worker(
|
|
51
51
|
this.queue,
|
|
@@ -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,SAAuC;AAV1D;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAEhB;AACA;AAGC,SAAK,QAAQ,SAAS,SAAS;AAC/B,SAAK,UAAU,SAAS,QAAQ,CAAC;AAEjC,uBAAK,SAAU,IAAI,MAAM,KAAK,OAAO,KAAK,OAAO;AACjD,uBA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zC;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;AAAA,IACD,CAAC;AACD,uBAAK,SAAQ,GAAG,SAAS,CAAC,UAAU;AACnC,UAAI,qBAAqB,KAAK,GAA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B,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;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 +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
|
|
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 */\ninterface 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> = T extends ScheduledTasksResolvableNoPayload<infer R>\n\t? Job<ScheduledTasksPayload<R>>\n\t: T extends ScheduledTasksResolvablePayload<infer R>\n\t\t? Job<ScheduledTasksPayload<R>>\n\t\t: Job<undefined>;\n"]}
|
package/dist/esm/index.d.mts
CHANGED
|
@@ -64,7 +64,7 @@ declare namespace ScheduledTask {
|
|
|
64
64
|
*/
|
|
65
65
|
interface ScheduledTaskHandlerOptions {
|
|
66
66
|
queue?: string;
|
|
67
|
-
bull
|
|
67
|
+
bull: QueueOptions;
|
|
68
68
|
}
|
|
69
69
|
/**
|
|
70
70
|
* Options for listing repeated scheduled tasks.
|
|
@@ -191,7 +191,7 @@ declare class ScheduledTaskHandler {
|
|
|
191
191
|
* The name of the queue associated with the scheduled task handler.
|
|
192
192
|
*/
|
|
193
193
|
readonly queue: string;
|
|
194
|
-
constructor(options
|
|
194
|
+
constructor(options: ScheduledTaskHandlerOptions);
|
|
195
195
|
get client(): BullClient;
|
|
196
196
|
/**
|
|
197
197
|
* Closes the internal client and worker.
|
|
@@ -342,7 +342,7 @@ declare module '@sapphire/pieces' {
|
|
|
342
342
|
}
|
|
343
343
|
declare module 'discord.js' {
|
|
344
344
|
interface ClientOptions {
|
|
345
|
-
tasks
|
|
345
|
+
tasks: ScheduledTaskHandlerOptions;
|
|
346
346
|
/**
|
|
347
347
|
* If the the pre-included scheduled task error listeners should be loaded
|
|
348
348
|
* @default true
|
package/dist/esm/index.mjs
CHANGED
|
@@ -6,7 +6,7 @@ export * from './lib/types/ScheduledTaskEvents.mjs';
|
|
|
6
6
|
export * from './lib/types/ScheduledTaskTypes.mjs';
|
|
7
7
|
export { loadListeners } from './listeners/_load.mjs';
|
|
8
8
|
|
|
9
|
-
var version = "10.0.0
|
|
9
|
+
var version = "10.0.0";
|
|
10
10
|
|
|
11
11
|
export { version };
|
|
12
12
|
//# 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;AACd,cAAc;AAEd,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 * 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
|
|
1
|
+
{"version":3,"sources":["../../src/index.ts"],"names":[],"mappings":";;;AAIA,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AACd,cAAc;AAEd,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 * 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.0';\n"]}
|
|
@@ -18,8 +18,8 @@ var _ScheduledTaskHandler = class _ScheduledTaskHandler {
|
|
|
18
18
|
__publicField(this, "queue");
|
|
19
19
|
__privateAdd(this, _client, void 0);
|
|
20
20
|
__privateAdd(this, _worker, void 0);
|
|
21
|
-
this.queue = options
|
|
22
|
-
this.options = options
|
|
21
|
+
this.queue = options.queue ?? "scheduled-tasks";
|
|
22
|
+
this.options = options.bull;
|
|
23
23
|
__privateSet(this, _client, new Queue(this.queue, this.options));
|
|
24
24
|
__privateSet(this, _worker, new Worker(
|
|
25
25
|
this.queue,
|
|
@@ -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,SAAuC;AAV1D;AAAA;AAAA;AAAA,wBAAgB;AAKhB;AAAA;AAAA;AAAA,wBAAgB;AAEhB;AACA;AAGC,SAAK,QAAQ,SAAS,SA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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;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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,KAAK,MAAM,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;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 +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
|
|
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 */\ninterface 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> = T extends ScheduledTasksResolvableNoPayload<infer R>\n\t? Job<ScheduledTasksPayload<R>>\n\t: T extends ScheduledTasksResolvablePayload<infer R>\n\t\t? Job<ScheduledTasksPayload<R>>\n\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.0
|
|
3
|
+
"version": "10.0.0",
|
|
4
4
|
"description": "Plugin for @sapphire/framework to have scheduled tasks",
|
|
5
5
|
"author": "@sapphire",
|
|
6
6
|
"license": "MIT",
|
|
@@ -50,7 +50,7 @@
|
|
|
50
50
|
"dependencies": {
|
|
51
51
|
"@sapphire/stopwatch": "^1.5.1",
|
|
52
52
|
"@sapphire/utilities": "^3.14.0",
|
|
53
|
-
"bullmq": "
|
|
53
|
+
"bullmq": "5.1.0",
|
|
54
54
|
"tslib": "^2.6.2"
|
|
55
55
|
},
|
|
56
56
|
"devDependencies": {
|