@kingsworld/plugin-cron 4.0.1-next.dffa437 → 4.0.1-next.e3432a7
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/CronTask-CWQGUBCQ.d.cts +84 -0
- package/dist/cjs/CronTask-CWQGUBCQ.d.cts.map +1 -0
- package/dist/cjs/CronTaskHandler-DI4ENcPU.d.cts +30 -0
- package/dist/cjs/CronTaskHandler-DI4ENcPU.d.cts.map +1 -0
- package/dist/cjs/CronTaskHandler-DWPv3gFR.cjs +64 -0
- package/dist/cjs/CronTaskHandler-DWPv3gFR.cjs.map +1 -0
- package/dist/cjs/CronTaskStore-RmZUHmVY.d.cts +57 -0
- package/dist/cjs/CronTaskStore-RmZUHmVY.d.cts.map +1 -0
- package/dist/cjs/CronTaskTypes-8bwitvv3.d.cts +30 -0
- package/dist/cjs/CronTaskTypes-8bwitvv3.d.cts.map +1 -0
- package/dist/cjs/index.cjs +16 -31
- package/dist/cjs/index.cjs.map +1 -1
- package/dist/cjs/index.d.cts +17 -187
- package/dist/cjs/index.d.cts.map +1 -0
- package/dist/cjs/lib/CronTaskHandler.cjs +3 -36
- package/dist/cjs/lib/CronTaskHandler.d.cts +3 -0
- package/dist/cjs/lib/structures/CronTask.cjs +79 -60
- package/dist/cjs/lib/structures/CronTask.cjs.map +1 -1
- package/dist/cjs/lib/structures/CronTask.d.cts +3 -0
- package/dist/cjs/lib/structures/CronTaskStore.cjs +121 -132
- package/dist/cjs/lib/structures/CronTaskStore.cjs.map +1 -1
- package/dist/cjs/lib/structures/CronTaskStore.d.cts +3 -0
- package/dist/cjs/lib/types/CronTaskTypes.cjs +0 -2
- package/dist/cjs/lib/types/CronTaskTypes.d.cts +3 -0
- package/dist/cjs/lib/utils/normalizePattern.cjs +33 -35
- package/dist/cjs/lib/utils/normalizePattern.cjs.map +1 -1
- package/dist/cjs/lib/utils/normalizePattern.d.cts +6 -0
- package/dist/cjs/lib/utils/normalizePattern.d.cts.map +1 -0
- package/dist/cjs/register.cjs +25 -25
- package/dist/cjs/register.cjs.map +1 -1
- package/dist/cjs/register.d.cts +10 -8
- package/dist/cjs/register.d.cts.map +1 -0
- package/dist/esm/CronTask-bjAF2VtU.d.mts +83 -0
- package/dist/esm/CronTask-bjAF2VtU.d.mts.map +1 -0
- package/dist/esm/CronTaskHandler-C2NIvWlD.d.mts +29 -0
- package/dist/esm/CronTaskHandler-C2NIvWlD.d.mts.map +1 -0
- package/dist/esm/CronTaskHandler-DfpLsyA6.mjs +56 -0
- package/dist/esm/CronTaskHandler-DfpLsyA6.mjs.map +1 -0
- package/dist/esm/CronTaskStore-CYzBM1td.d.mts +56 -0
- package/dist/esm/CronTaskStore-CYzBM1td.d.mts.map +1 -0
- package/dist/esm/CronTaskTypes-BOgAse0r.d.mts +29 -0
- package/dist/esm/CronTaskTypes-BOgAse0r.d.mts.map +1 -0
- package/dist/esm/index.d.mts +17 -188
- package/dist/esm/index.d.mts.map +1 -0
- package/dist/esm/index.mjs +13 -8
- package/dist/esm/index.mjs.map +1 -1
- package/dist/esm/lib/CronTaskHandler.d.mts +2 -0
- package/dist/esm/lib/CronTaskHandler.mjs +2 -33
- package/dist/esm/lib/structures/CronTask.d.mts +2 -0
- package/dist/esm/lib/structures/CronTask.mjs +78 -59
- package/dist/esm/lib/structures/CronTask.mjs.map +1 -1
- package/dist/esm/lib/structures/CronTaskStore.d.mts +2 -0
- package/dist/esm/lib/structures/CronTaskStore.mjs +120 -131
- package/dist/esm/lib/structures/CronTaskStore.mjs.map +1 -1
- package/dist/esm/lib/types/CronTaskTypes.d.mts +2 -0
- package/dist/esm/lib/types/CronTaskTypes.mjs +1 -3
- package/dist/esm/lib/utils/normalizePattern.d.mts +5 -0
- package/dist/esm/lib/utils/normalizePattern.d.mts.map +1 -0
- package/dist/esm/lib/utils/normalizePattern.mjs +32 -35
- package/dist/esm/lib/utils/normalizePattern.mjs.map +1 -1
- package/dist/esm/register.d.mts +9 -8
- package/dist/esm/register.d.mts.map +1 -0
- package/dist/esm/register.mjs +20 -20
- package/dist/esm/register.mjs.map +1 -1
- package/package.json +7 -15
- package/dist/cjs/lib/CronTaskHandler.cjs.map +0 -1
- package/dist/cjs/lib/types/CronTaskTypes.cjs.map +0 -1
- package/dist/esm/chunk-2JTKI4GS.mjs +0 -8
- package/dist/esm/chunk-2JTKI4GS.mjs.map +0 -1
- package/dist/esm/lib/CronTaskHandler.mjs.map +0 -1
- package/dist/esm/lib/types/CronTaskTypes.mjs.map +0 -1
|
@@ -1,34 +1,3 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { t as CronTaskHandler } from "../CronTaskHandler-DfpLsyA6.mjs";
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
var _CronTaskHandler = class _CronTaskHandler {
|
|
5
|
-
constructor(options) {
|
|
6
|
-
/**
|
|
7
|
-
* The default IANA/TZ timezone to use for all cron jobs.
|
|
8
|
-
* You can override this per task, using the timezone option.
|
|
9
|
-
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
10
|
-
*/
|
|
11
|
-
__publicField(this, "defaultTimezone");
|
|
12
|
-
/**
|
|
13
|
-
* The ability to opt-out of instrumenting cron jobs with Sentry.
|
|
14
|
-
* If you don't use Sentry, you can ignore this option.
|
|
15
|
-
* @see https://docs.sentry.io/product/crons/
|
|
16
|
-
* @default false
|
|
17
|
-
*/
|
|
18
|
-
__publicField(this, "disableSentry");
|
|
19
|
-
/**
|
|
20
|
-
* The Sentry instance to use for instrumenting cron jobs.
|
|
21
|
-
* This is only available when [`@sentry/node`](https://www.npmjs.com/package/@sentry/node)
|
|
22
|
-
* is installed and the {@linkcode disableSentry} option is set to false.
|
|
23
|
-
*/
|
|
24
|
-
__publicField(this, "sentry");
|
|
25
|
-
this.defaultTimezone = options?.defaultTimezone;
|
|
26
|
-
this.disableSentry = options?.disableSentry ?? false;
|
|
27
|
-
}
|
|
28
|
-
};
|
|
29
|
-
__name(_CronTaskHandler, "CronTaskHandler");
|
|
30
|
-
var CronTaskHandler = _CronTaskHandler;
|
|
31
|
-
|
|
32
|
-
export { CronTaskHandler };
|
|
33
|
-
//# sourceMappingURL=CronTaskHandler.mjs.map
|
|
34
|
-
//# sourceMappingURL=CronTaskHandler.mjs.map
|
|
3
|
+
export { CronTaskHandler };
|
|
@@ -1,64 +1,83 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { Piece } from '@sapphire/pieces';
|
|
1
|
+
import { Piece } from "@sapphire/pieces";
|
|
3
2
|
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
3
|
+
//#region src/lib/structures/CronTask.ts
|
|
4
|
+
/**
|
|
5
|
+
* @example
|
|
6
|
+
*
|
|
7
|
+
* ```typescript
|
|
8
|
+
* // ping.ts
|
|
9
|
+
* import { CronTask } from '@kingsworld/plugin-cron';
|
|
10
|
+
*
|
|
11
|
+
* export class PingPong extends CronTask {
|
|
12
|
+
* public constructor(context: CronTask.LoaderContext, options: CronTask.Options) {
|
|
13
|
+
* super(context, {
|
|
14
|
+
* ...options,
|
|
15
|
+
* pattern: '* * * * *'
|
|
16
|
+
* });
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* public run() {
|
|
20
|
+
* this.info('Ping Pong! 🏓'); // CronTask[ping] Ping Pong! 🏓
|
|
21
|
+
* }
|
|
22
|
+
* }
|
|
23
|
+
* ```
|
|
24
|
+
*/
|
|
25
|
+
var CronTask = class extends Piece {
|
|
26
|
+
constructor(context, options) {
|
|
27
|
+
super(context, options);
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A helper function to log messages with the `CronTask[${name}]` prefix.
|
|
31
|
+
* @param message The message to include after the prefix
|
|
32
|
+
* @param other Extra parameters to pass to the logger
|
|
33
|
+
* @example
|
|
34
|
+
* this.info('Hello world!'); // CronTask[my-task] Hello world!
|
|
35
|
+
*/
|
|
36
|
+
info(message, ...other) {
|
|
37
|
+
this.container.logger.info(`CronTask[${this.name}] ${message}`, ...other);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* A helper function to log messages with the `CronTask[${name}]` prefix.
|
|
41
|
+
* @param message The message to include after the prefix
|
|
42
|
+
* @param other Extra parameters to pass to the logger
|
|
43
|
+
* @example
|
|
44
|
+
* this.error('Something went wrong!'); // CronTask[my-task] Something went wrong!
|
|
45
|
+
*/
|
|
46
|
+
error(message, ...other) {
|
|
47
|
+
this.container.logger.error(`CronTask[${this.name}] ${message}`, ...other);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A helper function to log messages with the `CronTask[${name}]` prefix.
|
|
51
|
+
* @param message The message to include after the prefix
|
|
52
|
+
* @param other Extra parameters to pass to the logger
|
|
53
|
+
* @example
|
|
54
|
+
* this.warn('Something is not right!'); // CronTask[my-task] Something is not right!
|
|
55
|
+
*/
|
|
56
|
+
warn(message, ...other) {
|
|
57
|
+
this.container.logger.warn(`CronTask[${this.name}] ${message}`, ...other);
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* A helper function to log messages with the `CronTask[${name}]` prefix.
|
|
61
|
+
* @param message The message to include after the prefix
|
|
62
|
+
* @param other Extra parameters to pass to the logger
|
|
63
|
+
* @example
|
|
64
|
+
* this.debug('Something is happening!'); // CronTask[my-task] Something is happening!
|
|
65
|
+
*/
|
|
66
|
+
debug(message, ...other) {
|
|
67
|
+
this.container.logger.debug(`CronTask[${this.name}] ${message}`, ...other);
|
|
68
|
+
}
|
|
69
|
+
/**
|
|
70
|
+
* A helper function to log messages with the `CronTask[${name}]` prefix.
|
|
71
|
+
* @param message The message to include after the prefix
|
|
72
|
+
* @param other Extra parameters to pass to the logger
|
|
73
|
+
* @example
|
|
74
|
+
* this.trace('Loaded the file.'); // CronTask[my-task] Loaded the file.
|
|
75
|
+
*/
|
|
76
|
+
trace(message, ...other) {
|
|
77
|
+
this.container.logger.trace(`CronTask[${this.name}] ${message}`, ...other);
|
|
78
|
+
}
|
|
58
79
|
};
|
|
59
|
-
__name(_CronTask, "CronTask");
|
|
60
|
-
var CronTask = _CronTask;
|
|
61
80
|
|
|
81
|
+
//#endregion
|
|
62
82
|
export { CronTask };
|
|
63
|
-
//# sourceMappingURL=CronTask.mjs.map
|
|
64
83
|
//# sourceMappingURL=CronTask.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"CronTask.mjs","names":[],"sources":["../../../../src/lib/structures/CronTask.ts"],"sourcesContent":["import type { Awaitable } from '@sapphire/framework';\nimport { Piece } from '@sapphire/pieces';\nimport type { Cron } from 'croner';\nimport type { CronJobOptions } from '../types/CronTaskTypes';\n\n/**\n * @example\n *\n * ```typescript\n * // ping.ts\n * import { CronTask } from '@kingsworld/plugin-cron';\n *\n * export class PingPong extends CronTask {\n * \tpublic constructor(context: CronTask.LoaderContext, options: CronTask.Options) {\n * \t\tsuper(context, {\n * \t\t\t...options,\n * \t\t\tpattern: '* * * * *'\n * \t\t});\n * \t}\n *\n * \tpublic run() {\n * \t\tthis.info('Ping Pong! 🏓'); // CronTask[ping] Ping Pong! 🏓\n * \t}\n * }\n * ```\n */\nexport abstract class CronTask<Options extends CronTask.Options = CronTask.Options> extends Piece<Options, 'cron-tasks'> {\n\tdeclare public job: Cron;\n\n\tpublic constructor(context: CronTask.LoaderContext, options: Options) {\n\t\tsuper(context, options);\n\t}\n\n\tpublic abstract run(): Awaitable<unknown>;\n\n\tpublic protect?(job: Cron): Awaitable<unknown>;\n\n\tpublic catch?(error: unknown, job: Cron): Awaitable<unknown>;\n\n\t/**\n\t * A helper function to log messages with the `CronTask[${name}]` prefix.\n\t * @param message The message to include after the prefix\n\t * @param other Extra parameters to pass to the logger\n\t * @example\n\t * this.info('Hello world!'); // CronTask[my-task] Hello world!\n\t */\n\tpublic info(message: string, ...other: unknown[]) {\n\t\tthis.container.logger.info(`CronTask[${this.name}] ${message}`, ...other);\n\t}\n\n\t/**\n\t * A helper function to log messages with the `CronTask[${name}]` prefix.\n\t * @param message The message to include after the prefix\n\t * @param other Extra parameters to pass to the logger\n\t * @example\n\t * this.error('Something went wrong!'); // CronTask[my-task] Something went wrong!\n\t */\n\tpublic error(message: string, ...other: unknown[]) {\n\t\tthis.container.logger.error(`CronTask[${this.name}] ${message}`, ...other);\n\t}\n\n\t/**\n\t * A helper function to log messages with the `CronTask[${name}]` prefix.\n\t * @param message The message to include after the prefix\n\t * @param other Extra parameters to pass to the logger\n\t * @example\n\t * this.warn('Something is not right!'); // CronTask[my-task] Something is not right!\n\t */\n\tpublic warn(message: string, ...other: unknown[]) {\n\t\tthis.container.logger.warn(`CronTask[${this.name}] ${message}`, ...other);\n\t}\n\n\t/**\n\t * A helper function to log messages with the `CronTask[${name}]` prefix.\n\t * @param message The message to include after the prefix\n\t * @param other Extra parameters to pass to the logger\n\t * @example\n\t * this.debug('Something is happening!'); // CronTask[my-task] Something is happening!\n\t */\n\tpublic debug(message: string, ...other: unknown[]) {\n\t\tthis.container.logger.debug(`CronTask[${this.name}] ${message}`, ...other);\n\t}\n\n\t/**\n\t * A helper function to log messages with the `CronTask[${name}]` prefix.\n\t * @param message The message to include after the prefix\n\t * @param other Extra parameters to pass to the logger\n\t * @example\n\t * this.trace('Loaded the file.'); // CronTask[my-task] Loaded the file.\n\t */\n\tpublic trace(message: string, ...other: unknown[]) {\n\t\tthis.container.logger.trace(`CronTask[${this.name}] ${message}`, ...other);\n\t}\n}\n\nexport namespace CronTask {\n\texport type Options = Piece.Options & CronJobOptions;\n\t/** @deprecated Use {@linkcode LoaderContext} instead. */\n\texport type Context = LoaderContext;\n\texport type LoaderContext = Piece.LoaderContext<'cron-tasks'>;\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AA0BA,IAAA,WAAA,cAAA,MAAA;CAGA,AAAA,YAAA,SAAA,SAAA;EACA,MAAA,SAAA,OAAA;CACA;;;;;;;;CAeA,AAAA,KAAA,SAAA,GAAA,OAAA;EACA,KAAA,UAAA,OAAA,KAAA,YAAA,KAAA,KAAA,IAAA,WAAA,GAAA,KAAA;CACA;;;;;;;;CASA,AAAA,MAAA,SAAA,GAAA,OAAA;EACA,KAAA,UAAA,OAAA,MAAA,YAAA,KAAA,KAAA,IAAA,WAAA,GAAA,KAAA;CACA;;;;;;;;CASA,AAAA,KAAA,SAAA,GAAA,OAAA;EACA,KAAA,UAAA,OAAA,KAAA,YAAA,KAAA,KAAA,IAAA,WAAA,GAAA,KAAA;CACA;;;;;;;;CASA,AAAA,MAAA,SAAA,GAAA,OAAA;EACA,KAAA,UAAA,OAAA,MAAA,YAAA,KAAA,KAAA,IAAA,WAAA,GAAA,KAAA;CACA;;;;;;;;CASA,AAAA,MAAA,SAAA,GAAA,OAAA;EACA,KAAA,UAAA,OAAA,MAAA,YAAA,KAAA,KAAA,IAAA,WAAA,GAAA,KAAA;CACA;AACA"}
|
|
@@ -1,136 +1,125 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import { normalizePattern } from '../utils/normalizePattern.mjs';
|
|
1
|
+
import { CronTask } from "./CronTask.mjs";
|
|
2
|
+
import { normalizePattern } from "../utils/normalizePattern.mjs";
|
|
3
|
+
import { Store } from "@sapphire/pieces";
|
|
4
|
+
import { Cron } from "croner";
|
|
6
5
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
}
|
|
123
|
-
/**
|
|
124
|
-
* Stops all running cron jobs and clears the store.
|
|
125
|
-
*/
|
|
126
|
-
clear() {
|
|
127
|
-
this.stopAll();
|
|
128
|
-
return super.clear();
|
|
129
|
-
}
|
|
6
|
+
//#region src/lib/structures/CronTaskStore.ts
|
|
7
|
+
var CronTaskStore = class extends Store {
|
|
8
|
+
constructor() {
|
|
9
|
+
super(CronTask, { name: "cron-tasks" });
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Loops over all tasks and pauses those that are running.
|
|
13
|
+
*
|
|
14
|
+
* @remarks
|
|
15
|
+
* This method will only pause tasks that:
|
|
16
|
+
* - Are enabled
|
|
17
|
+
* - Are currently running
|
|
18
|
+
* - Have not been permanently stopped
|
|
19
|
+
*
|
|
20
|
+
* @returns CronTaskStore
|
|
21
|
+
*/
|
|
22
|
+
pauseAll() {
|
|
23
|
+
for (const task of this.values()) {
|
|
24
|
+
if (!task.enabled || !task.job.isRunning()) continue;
|
|
25
|
+
task.job.pause();
|
|
26
|
+
}
|
|
27
|
+
Store.logger?.(`[STORE => ${this.name}] [PAUSE] Paused all cronjob tasks.`);
|
|
28
|
+
return this;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Loops over all tasks and resumes those that are paused.
|
|
32
|
+
*
|
|
33
|
+
* @remarks
|
|
34
|
+
* This method will only resume tasks that:
|
|
35
|
+
* - Are enabled
|
|
36
|
+
* - Are not currently running
|
|
37
|
+
* - Have not been permanently stopped
|
|
38
|
+
*
|
|
39
|
+
* @returns CronTaskStore
|
|
40
|
+
*/
|
|
41
|
+
resumeAll() {
|
|
42
|
+
for (const task of this.values()) {
|
|
43
|
+
if (!task.enabled || task.job.isRunning() || task.job.isStopped()) continue;
|
|
44
|
+
task.job.resume();
|
|
45
|
+
}
|
|
46
|
+
Store.logger?.(`[STORE => ${this.name}] [RESUME] Resumed all cronjob tasks.`);
|
|
47
|
+
return this;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Loops over all tasks and stops those that are running.
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* This method will only stop tasks that:
|
|
54
|
+
* - Are enabled
|
|
55
|
+
* - Have not been permanently stopped
|
|
56
|
+
*
|
|
57
|
+
* ⚠️ Stopping jobs is **permanent** and cannot be resumed afterwards!
|
|
58
|
+
*
|
|
59
|
+
* @returns CronTaskStore
|
|
60
|
+
*/
|
|
61
|
+
stopAll() {
|
|
62
|
+
for (const task of this.values()) {
|
|
63
|
+
if (!task.enabled || task.job.isStopped()) continue;
|
|
64
|
+
task.job.stop();
|
|
65
|
+
}
|
|
66
|
+
Store.logger?.(`[STORE => ${this.name}] [STOP] Stopped all cronjob tasks.`);
|
|
67
|
+
return this;
|
|
68
|
+
}
|
|
69
|
+
set(key, value) {
|
|
70
|
+
const { pattern, timezone, protect, ...options } = value.options;
|
|
71
|
+
const { sentry, defaultTimezone } = this.container.cronTasks;
|
|
72
|
+
if (this.has(key)) {
|
|
73
|
+
Store.logger?.(`[STORE => ${this.name}] [SET] Stopping existing cronjob task before creating a new one.`);
|
|
74
|
+
this.get(key)?.job.stop();
|
|
75
|
+
}
|
|
76
|
+
const timeZone = timezone ?? defaultTimezone;
|
|
77
|
+
try {
|
|
78
|
+
Store.logger?.(`[STORE => ${this.name}] [SET] Creating cronjob for ${key} with '${pattern}' as the pattern and '${timeZone}' for the timezone`);
|
|
79
|
+
value.job = new Cron(pattern, {
|
|
80
|
+
name: key,
|
|
81
|
+
timezone: timeZone,
|
|
82
|
+
paused: true,
|
|
83
|
+
protect: value.protect?.bind(value) ?? protect,
|
|
84
|
+
catch: (error) => {
|
|
85
|
+
value.error("Encountered an error while running the cron job", error);
|
|
86
|
+
sentry?.captureException(error);
|
|
87
|
+
value.catch?.bind(value)(error, value.job);
|
|
88
|
+
},
|
|
89
|
+
...options
|
|
90
|
+
}, async () => {
|
|
91
|
+
if (sentry && typeof pattern === "string" && !pattern.includes(":")) await sentry.withMonitor(key, value.run.bind(value), {
|
|
92
|
+
schedule: {
|
|
93
|
+
type: "crontab",
|
|
94
|
+
value: normalizePattern(pattern)
|
|
95
|
+
},
|
|
96
|
+
timezone: timeZone
|
|
97
|
+
});
|
|
98
|
+
else await value.run.bind(value)();
|
|
99
|
+
});
|
|
100
|
+
} catch (error) {
|
|
101
|
+
value.error("Encountered an error while creating the cron job", error);
|
|
102
|
+
value.unload();
|
|
103
|
+
}
|
|
104
|
+
return super.set(key, value);
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Deletes a task from the store and stops it if it's running.
|
|
108
|
+
*/
|
|
109
|
+
delete(key) {
|
|
110
|
+
const task = this.get(key);
|
|
111
|
+
if (task && !task.job.isStopped()) task.job.stop();
|
|
112
|
+
return super.delete(key);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Stops all running cron jobs and clears the store.
|
|
116
|
+
*/
|
|
117
|
+
clear() {
|
|
118
|
+
this.stopAll();
|
|
119
|
+
return super.clear();
|
|
120
|
+
}
|
|
130
121
|
};
|
|
131
|
-
__name(_CronTaskStore, "CronTaskStore");
|
|
132
|
-
var CronTaskStore = _CronTaskStore;
|
|
133
122
|
|
|
123
|
+
//#endregion
|
|
134
124
|
export { CronTaskStore };
|
|
135
|
-
//# sourceMappingURL=CronTaskStore.mjs.map
|
|
136
125
|
//# sourceMappingURL=CronTaskStore.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"CronTaskStore.mjs","names":[],"sources":["../../../../src/lib/structures/CronTaskStore.ts"],"sourcesContent":["import { Store } from '@sapphire/pieces';\nimport { Cron } from 'croner';\nimport { CronTask } from './CronTask';\nimport { normalizePattern } from '../utils/normalizePattern';\n\nexport class CronTaskStore extends Store<CronTask, 'cron-tasks'> {\n\tpublic constructor() {\n\t\tsuper(CronTask, { name: 'cron-tasks' });\n\t}\n\n\t/**\n\t * Loops over all tasks and pauses those that are running.\n\t *\n\t * @remarks\n\t * This method will only pause tasks that:\n\t * - Are enabled\n\t * - Are currently running\n\t * - Have not been permanently stopped\n\t *\n\t * @returns CronTaskStore\n\t */\n\tpublic pauseAll() {\n\t\tfor (const task of this.values()) {\n\t\t\tif (!task.enabled || !task.job.isRunning()) continue;\n\t\t\ttask.job.pause();\n\t\t}\n\n\t\tStore.logger?.(`[STORE => ${this.name}] [PAUSE] Paused all cronjob tasks.`);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Loops over all tasks and resumes those that are paused.\n\t *\n\t * @remarks\n\t * This method will only resume tasks that:\n\t * - Are enabled\n\t * - Are not currently running\n\t * - Have not been permanently stopped\n\t *\n\t * @returns CronTaskStore\n\t */\n\tpublic resumeAll() {\n\t\tfor (const task of this.values()) {\n\t\t\tif (!task.enabled || task.job.isRunning() || task.job.isStopped()) continue;\n\t\t\ttask.job.resume();\n\t\t}\n\n\t\tStore.logger?.(`[STORE => ${this.name}] [RESUME] Resumed all cronjob tasks.`);\n\t\treturn this;\n\t}\n\n\t/**\n\t * Loops over all tasks and stops those that are running.\n\t *\n\t * @remarks\n\t * This method will only stop tasks that:\n\t * - Are enabled\n\t * - Have not been permanently stopped\n\t *\n\t * ⚠️ Stopping jobs is **permanent** and cannot be resumed afterwards!\n\t *\n\t * @returns CronTaskStore\n\t */\n\tpublic stopAll() {\n\t\tfor (const task of this.values()) {\n\t\t\tif (!task.enabled || task.job.isStopped()) continue;\n\t\t\ttask.job.stop();\n\t\t}\n\n\t\tStore.logger?.(`[STORE => ${this.name}] [STOP] Stopped all cronjob tasks.`);\n\t\treturn this;\n\t}\n\n\tpublic override set(key: string, value: CronTask): this {\n\t\tconst { pattern, timezone, protect, ...options } = value.options;\n\t\tconst { sentry, defaultTimezone } = this.container.cronTasks;\n\n\t\t// if a task with the same key already exists, stop it before creating a new one\n\t\tif (this.has(key)) {\n\t\t\tStore.logger?.(`[STORE => ${this.name}] [SET] Stopping existing cronjob task before creating a new one.`);\n\t\t\tthis.get(key)?.job.stop();\n\t\t}\n\n\t\tconst timeZone = timezone ?? defaultTimezone;\n\n\t\ttry {\n\t\t\tStore.logger?.(\n\t\t\t\t`[STORE => ${this.name}] [SET] Creating cronjob for ${key} with '${pattern}' as the pattern and '${timeZone}' for the timezone`\n\t\t\t);\n\n\t\t\tvalue.job = new Cron(\n\t\t\t\tpattern,\n\t\t\t\t{\n\t\t\t\t\tname: key,\n\t\t\t\t\ttimezone: timeZone,\n\t\t\t\t\tpaused: true, // we start the job manually once the client is ready\n\t\t\t\t\tprotect: value.protect?.bind(value) ?? protect,\n\t\t\t\t\tcatch: (error) => {\n\t\t\t\t\t\tvalue.error('Encountered an error while running the cron job', error);\n\t\t\t\t\t\tsentry?.captureException(error);\n\t\t\t\t\t\tvalue.catch?.bind(value)(error, value.job);\n\t\t\t\t\t},\n\t\t\t\t\t...options\n\t\t\t\t},\n\t\t\t\tasync () => {\n\t\t\t\t\t// we only want to monitor cron patterns and not single-use tasks that croner supports\n\t\t\t\t\tif (sentry && typeof pattern === 'string' && !pattern.includes(':')) {\n\t\t\t\t\t\tawait sentry.withMonitor(key, value.run.bind(value), {\n\t\t\t\t\t\t\tschedule: { type: 'crontab', value: normalizePattern(pattern) },\n\t\t\t\t\t\t\ttimezone: timeZone\n\t\t\t\t\t\t});\n\t\t\t\t\t} else {\n\t\t\t\t\t\tawait value.run.bind(value)();\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t);\n\t\t} catch (error) {\n\t\t\tvalue.error('Encountered an error while creating the cron job', error);\n\t\t\tvoid value.unload();\n\t\t}\n\n\t\treturn super.set(key, value);\n\t}\n\n\t/**\n\t * Deletes a task from the store and stops it if it's running.\n\t */\n\tpublic override delete(key: string) {\n\t\tconst task = this.get(key);\n\t\tif (task && !task.job.isStopped()) {\n\t\t\ttask.job.stop();\n\t\t}\n\n\t\treturn super.delete(key);\n\t}\n\n\t/**\n\t * Stops all running cron jobs and clears the store.\n\t */\n\tpublic override clear() {\n\t\tthis.stopAll();\n\t\treturn super.clear();\n\t}\n}\n"],"mappings":";;;;;;AAKA,IAAA,gBAAA,cAAA,MAAA;CACA,AAAA,cAAA;EACA,MAAA,UAAA,EAAA,MAAA,aAAA,CAAA;CACA;;;;;;;;;;;;CAaA,AAAA,WAAA;EACA,KAAA,MAAA,QAAA,KAAA,OAAA,GAAA;GACA,IAAA,CAAA,KAAA,WAAA,CAAA,KAAA,IAAA,UAAA,GAAA;GACA,KAAA,IAAA,MAAA;EACA;EAEA,MAAA,SAAA,aAAA,KAAA,KAAA,oCAAA;EACA,OAAA;CACA;;;;;;;;;;;;CAaA,AAAA,YAAA;EACA,KAAA,MAAA,QAAA,KAAA,OAAA,GAAA;GACA,IAAA,CAAA,KAAA,WAAA,KAAA,IAAA,UAAA,KAAA,KAAA,IAAA,UAAA,GAAA;GACA,KAAA,IAAA,OAAA;EACA;EAEA,MAAA,SAAA,aAAA,KAAA,KAAA,sCAAA;EACA,OAAA;CACA;;;;;;;;;;;;;CAcA,AAAA,UAAA;EACA,KAAA,MAAA,QAAA,KAAA,OAAA,GAAA;GACA,IAAA,CAAA,KAAA,WAAA,KAAA,IAAA,UAAA,GAAA;GACA,KAAA,IAAA,KAAA;EACA;EAEA,MAAA,SAAA,aAAA,KAAA,KAAA,oCAAA;EACA,OAAA;CACA;CAEA,AAAA,IAAA,KAAA,OAAA;EACA,MAAA,EAAA,SAAA,UAAA,SAAA,GAAA,YAAA,MAAA;EACA,MAAA,EAAA,QAAA,oBAAA,KAAA,UAAA;EAGA,IAAA,KAAA,IAAA,GAAA,GAAA;GACA,MAAA,SAAA,aAAA,KAAA,KAAA,kEAAA;GACA,KAAA,IAAA,GAAA,CAAA,EAAA,IAAA,KAAA;EACA;EAEA,MAAA,WAAA,YAAA;EAEA,IAAA;GACA,MAAA,SACA,aAAA,KAAA,KAAA,+BAAA,IAAA,SAAA,QAAA,wBAAA,SAAA,mBACA;GAEA,MAAA,MAAA,IAAA,KACA,SACA;IACA,MAAA;IACA,UAAA;IACA,QAAA;IACA,SAAA,MAAA,SAAA,KAAA,KAAA,KAAA;IACA,QAAA,UAAA;KACA,MAAA,MAAA,mDAAA,KAAA;KACA,QAAA,iBAAA,KAAA;KACA,MAAA,OAAA,KAAA,KAAA,CAAA,CAAA,OAAA,MAAA,GAAA;IACA;IACA,GAAA;GACA,GACA,YAAA;IAEA,IAAA,UAAA,OAAA,YAAA,YAAA,CAAA,QAAA,SAAA,GAAA,GACA,MAAA,OAAA,YAAA,KAAA,MAAA,IAAA,KAAA,KAAA,GAAA;KACA,UAAA;MAAA,MAAA;MAAA,OAAA,iBAAA,OAAA;KAAA;KACA,UAAA;IACA,CAAA;SAEA,MAAA,MAAA,IAAA,KAAA,KAAA,CAAA,CAAA;GAEA,CACA;EACA,SAAA,OAAA;GACA,MAAA,MAAA,oDAAA,KAAA;GACA,AAAA,MAAA,OAAA;EACA;EAEA,OAAA,MAAA,IAAA,KAAA,KAAA;CACA;;;;CAKA,AAAA,OAAA,KAAA;EACA,MAAA,OAAA,KAAA,IAAA,GAAA;EACA,IAAA,QAAA,CAAA,KAAA,IAAA,UAAA,GACA,KAAA,IAAA,KAAA;EAGA,OAAA,MAAA,OAAA,GAAA;CACA;;;;CAKA,AAAA,QAAA;EACA,KAAA,QAAA;EACA,OAAA,MAAA,MAAA;CACA;AACA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalizePattern.d.mts","names":[],"sources":["../../../../src/lib/utils/normalizePattern.ts"],"mappings":";iBAuCA,gBAAA,CAAA,OAAA"}
|
|
@@ -1,42 +1,39 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
"@daily": "0 0 * * *",
|
|
10
|
-
"@hourly": "0 * * * *"
|
|
1
|
+
//#region src/lib/utils/normalizePattern.ts
|
|
2
|
+
const predefined = {
|
|
3
|
+
"@annually": "0 0 1 1 *",
|
|
4
|
+
"@yearly": "0 0 1 1 *",
|
|
5
|
+
"@monthly": "0 0 1 * *",
|
|
6
|
+
"@weekly": "0 0 * * 0",
|
|
7
|
+
"@daily": "0 0 * * *",
|
|
8
|
+
"@hourly": "0 * * * *"
|
|
11
9
|
};
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
10
|
+
const cronTokens = {
|
|
11
|
+
jan: 1,
|
|
12
|
+
feb: 2,
|
|
13
|
+
mar: 3,
|
|
14
|
+
apr: 4,
|
|
15
|
+
may: 5,
|
|
16
|
+
jun: 6,
|
|
17
|
+
jul: 7,
|
|
18
|
+
aug: 8,
|
|
19
|
+
sep: 9,
|
|
20
|
+
oct: 10,
|
|
21
|
+
nov: 11,
|
|
22
|
+
dec: 12,
|
|
23
|
+
sun: 0,
|
|
24
|
+
mon: 1,
|
|
25
|
+
tue: 2,
|
|
26
|
+
wed: 3,
|
|
27
|
+
thu: 4,
|
|
28
|
+
fri: 5,
|
|
29
|
+
sat: 6
|
|
32
30
|
};
|
|
33
|
-
|
|
31
|
+
const tokensRegex = new RegExp(Object.keys(cronTokens).join("|"), "g");
|
|
34
32
|
function normalizePattern(pattern) {
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
if (Reflect.has(predefined, pattern)) return Reflect.get(predefined, pattern);
|
|
34
|
+
return pattern.replaceAll(tokensRegex, (match) => String(Reflect.get(cronTokens, match)));
|
|
37
35
|
}
|
|
38
|
-
__name(normalizePattern, "normalizePattern");
|
|
39
36
|
|
|
37
|
+
//#endregion
|
|
40
38
|
export { normalizePattern };
|
|
41
|
-
//# sourceMappingURL=normalizePattern.mjs.map
|
|
42
39
|
//# sourceMappingURL=normalizePattern.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"normalizePattern.mjs","names":[],"sources":["../../../../src/lib/utils/normalizePattern.ts"],"sourcesContent":["/*\n * Credits to Sapphire for this\n * https://github.com/sapphiredev/utilities/blob/main/packages/cron/src/lib/constants.ts#L26-L57\n * https://github.com/sapphiredev/utilities/blob/main/packages/cron/src/lib/Cron.ts#L91\n */\n\nconst predefined = {\n\t'@annually': '0 0 1 1 *',\n\t'@yearly': '0 0 1 1 *',\n\t'@monthly': '0 0 1 * *',\n\t'@weekly': '0 0 * * 0',\n\t'@daily': '0 0 * * *',\n\t'@hourly': '0 * * * *'\n} as const;\n\nconst cronTokens: Record<string, number> = {\n\tjan: 1,\n\tfeb: 2,\n\tmar: 3,\n\tapr: 4,\n\tmay: 5,\n\tjun: 6,\n\tjul: 7,\n\taug: 8,\n\tsep: 9,\n\toct: 10,\n\tnov: 11,\n\tdec: 12,\n\tsun: 0,\n\tmon: 1,\n\ttue: 2,\n\twed: 3,\n\tthu: 4,\n\tfri: 5,\n\tsat: 6\n} as const;\n\nconst tokensRegex = new RegExp(Object.keys(cronTokens).join('|'), 'g');\n\nexport function normalizePattern(pattern: string): string {\n\tif (Reflect.has(predefined, pattern)) return Reflect.get(predefined, pattern);\n\treturn pattern.replaceAll(tokensRegex, (match) => String(Reflect.get(cronTokens, match)));\n}\n"],"mappings":";AAMA,MAAA,aAAA;CACA,aAAA;CACA,WAAA;CACA,YAAA;CACA,WAAA;CACA,UAAA;CACA,WAAA;AACA;AAEA,MAAA,aAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;CACA,KAAA;AACA;AAEA,MAAA,cAAA,IAAA,OAAA,OAAA,KAAA,UAAA,CAAA,CAAA,KAAA,GAAA,GAAA,GAAA;AAEA,SAAA,iBAAA,SAAA;CACA,IAAA,QAAA,IAAA,YAAA,OAAA,GAAA,OAAA,QAAA,IAAA,YAAA,OAAA;CACA,OAAA,QAAA,WAAA,cAAA,UAAA,OAAA,QAAA,IAAA,YAAA,KAAA,CAAA,CAAA;AACA"}
|