@kingsworld/plugin-cron 4.0.1-next.dffa437 → 4.0.1-next.e3791ca
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,67 +1,86 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
let _sapphire_pieces = require("@sapphire/pieces");
|
|
4
5
|
|
|
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
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
6
|
+
//#region src/lib/structures/CronTask.ts
|
|
7
|
+
/**
|
|
8
|
+
* @example
|
|
9
|
+
*
|
|
10
|
+
* ```typescript
|
|
11
|
+
* // ping.ts
|
|
12
|
+
* import { CronTask } from '@kingsworld/plugin-cron';
|
|
13
|
+
*
|
|
14
|
+
* export class PingPong extends CronTask {
|
|
15
|
+
* public constructor(context: CronTask.LoaderContext, options: CronTask.Options) {
|
|
16
|
+
* super(context, {
|
|
17
|
+
* ...options,
|
|
18
|
+
* pattern: '* * * * *'
|
|
19
|
+
* });
|
|
20
|
+
* }
|
|
21
|
+
*
|
|
22
|
+
* public run() {
|
|
23
|
+
* this.info('Ping Pong! 🏓'); // CronTask[ping] Ping Pong! 🏓
|
|
24
|
+
* }
|
|
25
|
+
* }
|
|
26
|
+
* ```
|
|
27
|
+
*/
|
|
28
|
+
var CronTask = class extends _sapphire_pieces.Piece {
|
|
29
|
+
constructor(context, options) {
|
|
30
|
+
super(context, options);
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* A helper function to log messages with the `CronTask[${name}]` prefix.
|
|
34
|
+
* @param message The message to include after the prefix
|
|
35
|
+
* @param other Extra parameters to pass to the logger
|
|
36
|
+
* @example
|
|
37
|
+
* this.info('Hello world!'); // CronTask[my-task] Hello world!
|
|
38
|
+
*/
|
|
39
|
+
info(message, ...other) {
|
|
40
|
+
this.container.logger.info(`CronTask[${this.name}] ${message}`, ...other);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* A helper function to log messages with the `CronTask[${name}]` prefix.
|
|
44
|
+
* @param message The message to include after the prefix
|
|
45
|
+
* @param other Extra parameters to pass to the logger
|
|
46
|
+
* @example
|
|
47
|
+
* this.error('Something went wrong!'); // CronTask[my-task] Something went wrong!
|
|
48
|
+
*/
|
|
49
|
+
error(message, ...other) {
|
|
50
|
+
this.container.logger.error(`CronTask[${this.name}] ${message}`, ...other);
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* A helper function to log messages with the `CronTask[${name}]` prefix.
|
|
54
|
+
* @param message The message to include after the prefix
|
|
55
|
+
* @param other Extra parameters to pass to the logger
|
|
56
|
+
* @example
|
|
57
|
+
* this.warn('Something is not right!'); // CronTask[my-task] Something is not right!
|
|
58
|
+
*/
|
|
59
|
+
warn(message, ...other) {
|
|
60
|
+
this.container.logger.warn(`CronTask[${this.name}] ${message}`, ...other);
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* A helper function to log messages with the `CronTask[${name}]` prefix.
|
|
64
|
+
* @param message The message to include after the prefix
|
|
65
|
+
* @param other Extra parameters to pass to the logger
|
|
66
|
+
* @example
|
|
67
|
+
* this.debug('Something is happening!'); // CronTask[my-task] Something is happening!
|
|
68
|
+
*/
|
|
69
|
+
debug(message, ...other) {
|
|
70
|
+
this.container.logger.debug(`CronTask[${this.name}] ${message}`, ...other);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* A helper function to log messages with the `CronTask[${name}]` prefix.
|
|
74
|
+
* @param message The message to include after the prefix
|
|
75
|
+
* @param other Extra parameters to pass to the logger
|
|
76
|
+
* @example
|
|
77
|
+
* this.trace('Loaded the file.'); // CronTask[my-task] Loaded the file.
|
|
78
|
+
*/
|
|
79
|
+
trace(message, ...other) {
|
|
80
|
+
this.container.logger.trace(`CronTask[${this.name}] ${message}`, ...other);
|
|
81
|
+
}
|
|
61
82
|
};
|
|
62
|
-
__name(_CronTask, "CronTask");
|
|
63
|
-
var CronTask = _CronTask;
|
|
64
83
|
|
|
84
|
+
//#endregion
|
|
65
85
|
exports.CronTask = CronTask;
|
|
66
|
-
//# sourceMappingURL=CronTask.cjs.map
|
|
67
86
|
//# sourceMappingURL=CronTask.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"CronTask.cjs","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,uBAAA;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,139 +1,128 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
4
|
+
const require_lib_structures_CronTask = require('./CronTask.cjs');
|
|
5
|
+
const require_lib_utils_normalizePattern = require('../utils/normalizePattern.cjs');
|
|
6
|
+
let _sapphire_pieces = require("@sapphire/pieces");
|
|
7
|
+
let croner = require("croner");
|
|
7
8
|
|
|
8
|
-
|
|
9
|
-
var
|
|
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
|
-
return super.delete(key);
|
|
125
|
-
}
|
|
126
|
-
/**
|
|
127
|
-
* Stops all running cron jobs and clears the store.
|
|
128
|
-
*/
|
|
129
|
-
clear() {
|
|
130
|
-
this.stopAll();
|
|
131
|
-
return super.clear();
|
|
132
|
-
}
|
|
9
|
+
//#region src/lib/structures/CronTaskStore.ts
|
|
10
|
+
var CronTaskStore = class extends _sapphire_pieces.Store {
|
|
11
|
+
constructor() {
|
|
12
|
+
super(require_lib_structures_CronTask.CronTask, { name: "cron-tasks" });
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Loops over all tasks and pauses those that are running.
|
|
16
|
+
*
|
|
17
|
+
* @remarks
|
|
18
|
+
* This method will only pause tasks that:
|
|
19
|
+
* - Are enabled
|
|
20
|
+
* - Are currently running
|
|
21
|
+
* - Have not been permanently stopped
|
|
22
|
+
*
|
|
23
|
+
* @returns CronTaskStore
|
|
24
|
+
*/
|
|
25
|
+
pauseAll() {
|
|
26
|
+
for (const task of this.values()) {
|
|
27
|
+
if (!task.enabled || !task.job.isRunning()) continue;
|
|
28
|
+
task.job.pause();
|
|
29
|
+
}
|
|
30
|
+
_sapphire_pieces.Store.logger?.(`[STORE => ${this.name}] [PAUSE] Paused all cronjob tasks.`);
|
|
31
|
+
return this;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Loops over all tasks and resumes those that are paused.
|
|
35
|
+
*
|
|
36
|
+
* @remarks
|
|
37
|
+
* This method will only resume tasks that:
|
|
38
|
+
* - Are enabled
|
|
39
|
+
* - Are not currently running
|
|
40
|
+
* - Have not been permanently stopped
|
|
41
|
+
*
|
|
42
|
+
* @returns CronTaskStore
|
|
43
|
+
*/
|
|
44
|
+
resumeAll() {
|
|
45
|
+
for (const task of this.values()) {
|
|
46
|
+
if (!task.enabled || task.job.isRunning() || task.job.isStopped()) continue;
|
|
47
|
+
task.job.resume();
|
|
48
|
+
}
|
|
49
|
+
_sapphire_pieces.Store.logger?.(`[STORE => ${this.name}] [RESUME] Resumed all cronjob tasks.`);
|
|
50
|
+
return this;
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Loops over all tasks and stops those that are running.
|
|
54
|
+
*
|
|
55
|
+
* @remarks
|
|
56
|
+
* This method will only stop tasks that:
|
|
57
|
+
* - Are enabled
|
|
58
|
+
* - Have not been permanently stopped
|
|
59
|
+
*
|
|
60
|
+
* ⚠️ Stopping jobs is **permanent** and cannot be resumed afterwards!
|
|
61
|
+
*
|
|
62
|
+
* @returns CronTaskStore
|
|
63
|
+
*/
|
|
64
|
+
stopAll() {
|
|
65
|
+
for (const task of this.values()) {
|
|
66
|
+
if (!task.enabled || task.job.isStopped()) continue;
|
|
67
|
+
task.job.stop();
|
|
68
|
+
}
|
|
69
|
+
_sapphire_pieces.Store.logger?.(`[STORE => ${this.name}] [STOP] Stopped all cronjob tasks.`);
|
|
70
|
+
return this;
|
|
71
|
+
}
|
|
72
|
+
set(key, value) {
|
|
73
|
+
const { pattern, timezone, protect, ...options } = value.options;
|
|
74
|
+
const { sentry, defaultTimezone } = this.container.cronTasks;
|
|
75
|
+
if (this.has(key)) {
|
|
76
|
+
_sapphire_pieces.Store.logger?.(`[STORE => ${this.name}] [SET] Stopping existing cronjob task before creating a new one.`);
|
|
77
|
+
this.get(key)?.job.stop();
|
|
78
|
+
}
|
|
79
|
+
const timeZone = timezone ?? defaultTimezone;
|
|
80
|
+
try {
|
|
81
|
+
_sapphire_pieces.Store.logger?.(`[STORE => ${this.name}] [SET] Creating cronjob for ${key} with '${pattern}' as the pattern and '${timeZone}' for the timezone`);
|
|
82
|
+
value.job = new croner.Cron(pattern, {
|
|
83
|
+
name: key,
|
|
84
|
+
timezone: timeZone,
|
|
85
|
+
paused: true,
|
|
86
|
+
protect: value.protect?.bind(value) ?? protect,
|
|
87
|
+
catch: (error) => {
|
|
88
|
+
value.error("Encountered an error while running the cron job", error);
|
|
89
|
+
sentry?.captureException(error);
|
|
90
|
+
value.catch?.bind(value)(error, value.job);
|
|
91
|
+
},
|
|
92
|
+
...options
|
|
93
|
+
}, async () => {
|
|
94
|
+
if (sentry && typeof pattern === "string" && !pattern.includes(":")) await sentry.withMonitor(key, value.run.bind(value), {
|
|
95
|
+
schedule: {
|
|
96
|
+
type: "crontab",
|
|
97
|
+
value: require_lib_utils_normalizePattern.normalizePattern(pattern)
|
|
98
|
+
},
|
|
99
|
+
timezone: timeZone
|
|
100
|
+
});
|
|
101
|
+
else await value.run.bind(value)();
|
|
102
|
+
});
|
|
103
|
+
} catch (error) {
|
|
104
|
+
value.error("Encountered an error while creating the cron job", error);
|
|
105
|
+
value.unload();
|
|
106
|
+
}
|
|
107
|
+
return super.set(key, value);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Deletes a task from the store and stops it if it's running.
|
|
111
|
+
*/
|
|
112
|
+
delete(key) {
|
|
113
|
+
const task = this.get(key);
|
|
114
|
+
if (task && !task.job.isStopped()) task.job.stop();
|
|
115
|
+
return super.delete(key);
|
|
116
|
+
}
|
|
117
|
+
/**
|
|
118
|
+
* Stops all running cron jobs and clears the store.
|
|
119
|
+
*/
|
|
120
|
+
clear() {
|
|
121
|
+
this.stopAll();
|
|
122
|
+
return super.clear();
|
|
123
|
+
}
|
|
133
124
|
};
|
|
134
|
-
__name(_CronTaskStore, "CronTaskStore");
|
|
135
|
-
var CronTaskStore = _CronTaskStore;
|
|
136
125
|
|
|
126
|
+
//#endregion
|
|
137
127
|
exports.CronTaskStore = CronTaskStore;
|
|
138
|
-
//# sourceMappingURL=CronTaskStore.cjs.map
|
|
139
128
|
//# sourceMappingURL=CronTaskStore.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"CronTaskStore.cjs","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,uBAAA;CACA,AAAA,cAAA;EACA,MAAA,0CAAA,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,uBAAA,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,uBAAA,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,uBAAA,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,uBAAA,SAAA,aAAA,KAAA,KAAA,kEAAA;GACA,KAAA,IAAA,GAAA,CAAA,EAAA,IAAA,KAAA;EACA;EAEA,MAAA,WAAA,YAAA;EAEA,IAAA;GACA,uBAAA,SACA,aAAA,KAAA,KAAA,+BAAA,IAAA,SAAA,QAAA,wBAAA,SAAA,mBACA;GAEA,MAAA,MAAA,IAAA,YACA,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,oDAAA,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"}
|
|
@@ -1,45 +1,43 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
3
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
|
|
5
4
|
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
5
|
+
//#region src/lib/utils/normalizePattern.ts
|
|
6
|
+
const predefined = {
|
|
7
|
+
"@annually": "0 0 1 1 *",
|
|
8
|
+
"@yearly": "0 0 1 1 *",
|
|
9
|
+
"@monthly": "0 0 1 * *",
|
|
10
|
+
"@weekly": "0 0 * * 0",
|
|
11
|
+
"@daily": "0 0 * * *",
|
|
12
|
+
"@hourly": "0 * * * *"
|
|
14
13
|
};
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
14
|
+
const cronTokens = {
|
|
15
|
+
jan: 1,
|
|
16
|
+
feb: 2,
|
|
17
|
+
mar: 3,
|
|
18
|
+
apr: 4,
|
|
19
|
+
may: 5,
|
|
20
|
+
jun: 6,
|
|
21
|
+
jul: 7,
|
|
22
|
+
aug: 8,
|
|
23
|
+
sep: 9,
|
|
24
|
+
oct: 10,
|
|
25
|
+
nov: 11,
|
|
26
|
+
dec: 12,
|
|
27
|
+
sun: 0,
|
|
28
|
+
mon: 1,
|
|
29
|
+
tue: 2,
|
|
30
|
+
wed: 3,
|
|
31
|
+
thu: 4,
|
|
32
|
+
fri: 5,
|
|
33
|
+
sat: 6
|
|
35
34
|
};
|
|
36
|
-
|
|
35
|
+
const tokensRegex = new RegExp(Object.keys(cronTokens).join("|"), "g");
|
|
37
36
|
function normalizePattern(pattern) {
|
|
38
|
-
|
|
39
|
-
|
|
37
|
+
if (Reflect.has(predefined, pattern)) return Reflect.get(predefined, pattern);
|
|
38
|
+
return pattern.replaceAll(tokensRegex, (match) => String(Reflect.get(cronTokens, match)));
|
|
40
39
|
}
|
|
41
|
-
__name(normalizePattern, "normalizePattern");
|
|
42
40
|
|
|
41
|
+
//#endregion
|
|
43
42
|
exports.normalizePattern = normalizePattern;
|
|
44
|
-
//# sourceMappingURL=normalizePattern.cjs.map
|
|
45
43
|
//# sourceMappingURL=normalizePattern.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"normalizePattern.cjs","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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"normalizePattern.d.cts","names":[],"sources":["../../../../src/lib/utils/normalizePattern.ts"],"mappings":";;iBAuCgB,gBAAA,CAAiB,OAAe"}
|