@athenna/cron 5.7.0 → 5.9.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@athenna/cron",
3
- "version": "5.7.0",
3
+ "version": "5.9.0",
4
4
  "description": "Athenna scheduler application. Built on top of node-cron.",
5
5
  "license": "MIT",
6
6
  "author": "João Lenon <lenon@athenna.io>",
@@ -57,26 +57,26 @@
57
57
  "node-cron": "^3.0.3"
58
58
  },
59
59
  "devDependencies": {
60
- "@athenna/artisan": "^5.6.0",
61
- "@athenna/common": "^5.7.0",
62
- "@athenna/config": "^5.3.0",
63
- "@athenna/ioc": "^5.1.0",
64
- "@athenna/logger": "^5.3.0",
65
- "@athenna/test": "^5.3.0",
60
+ "@athenna/artisan": "^5.7.0",
61
+ "@athenna/common": "^5.14.0",
62
+ "@athenna/config": "^5.4.0",
63
+ "@athenna/ioc": "^5.2.0",
64
+ "@athenna/logger": "^5.7.0",
65
+ "@athenna/test": "^5.5.0",
66
66
  "@athenna/tsconfig": "^5.0.0",
67
67
  "@athenna/view": "^5.3.0",
68
68
  "@types/node-cron": "^3.0.11",
69
- "@typescript-eslint/eslint-plugin": "^8.21.0",
70
- "@typescript-eslint/parser": "^8.21.0",
69
+ "@typescript-eslint/eslint-plugin": "^8.38.0",
70
+ "@typescript-eslint/parser": "^8.38.0",
71
71
  "cls-rtracer": "^2.6.3",
72
72
  "commitizen": "^4.3.1",
73
73
  "cz-conventional-changelog": "^3.3.0",
74
74
  "eslint": "^8.57.1",
75
- "eslint-config-prettier": "^8.10.0",
75
+ "eslint-config-prettier": "^8.10.2",
76
76
  "eslint-config-standard": "^17.1.0",
77
- "eslint-plugin-import": "^2.31.0",
77
+ "eslint-plugin-import": "^2.32.0",
78
78
  "eslint-plugin-n": "^15.7.0",
79
- "eslint-plugin-prettier": "^4.2.1",
79
+ "eslint-plugin-prettier": "^4.2.5",
80
80
  "eslint-plugin-promise": "^6.6.0",
81
81
  "husky": "^3.1.0",
82
82
  "lint-staged": "^12.5.0",
@@ -11,11 +11,16 @@ import { Macroable } from '@athenna/common';
11
11
  import type { CronExceptionHandler } from '#src/handlers/CronExceptionHandler';
12
12
  export declare class CronBuilder extends Macroable {
13
13
  static rTracerPlugin: any;
14
+ static loggerIsSet: boolean;
14
15
  static exceptionHandler: CronExceptionHandler;
15
16
  /**
16
17
  * Register the cls-rtracer plugin into cron handlers.
17
18
  */
18
19
  static registerRTracer(plugin: any): void;
20
+ /**
21
+ * Register the logger plugin into cron handlers.
22
+ */
23
+ static registerLogger(isToSetLogger: boolean): void;
19
24
  private cron;
20
25
  /**
21
26
  * Defines the CRON expression that will determine when
@@ -7,18 +7,26 @@
7
7
  * file that was distributed with this source code.
8
8
  */
9
9
  import { schedule } from 'node-cron';
10
+ import { Log } from '@athenna/logger';
10
11
  import { Macroable, Options } from '@athenna/common';
11
12
  export class CronBuilder extends Macroable {
12
13
  constructor() {
13
14
  super(...arguments);
14
15
  this.cron = {};
15
16
  }
17
+ static { this.loggerIsSet = false; }
16
18
  /**
17
19
  * Register the cls-rtracer plugin into cron handlers.
18
20
  */
19
21
  static registerRTracer(plugin) {
20
22
  this.rTracerPlugin = plugin;
21
23
  }
24
+ /**
25
+ * Register the logger plugin into cron handlers.
26
+ */
27
+ static registerLogger(isToSetLogger) {
28
+ this.loggerIsSet = isToSetLogger;
29
+ }
22
30
  /**
23
31
  * Defines the CRON expression that will determine when
24
32
  * the task will run.
@@ -69,42 +77,56 @@ export class CronBuilder extends Macroable {
69
77
  * ```
70
78
  */
71
79
  handler(handler) {
72
- const register = () => {
73
- const getCtx = () => ({
74
- name: this.cron.name,
75
- traceId: CronBuilder.rTracerPlugin
76
- ? CronBuilder.rTracerPlugin.id()
77
- : null,
78
- pattern: this.cron.pattern,
79
- timezone: this.cron.timezone,
80
- runOnInit: this.cron.runOnInit,
81
- recoverMissedExecutions: this.cron.recoverMissedExecutions
82
- });
83
- const options = Options.create({
84
- name: this.cron.name,
85
- timezone: this.cron.timezone,
86
- runOnInit: this.cron.runOnInit,
87
- scheduled: this.cron.scheduled,
88
- recoverMissedExecutions: this.cron.recoverMissedExecutions
89
- });
90
- if (CronBuilder.rTracerPlugin) {
91
- return schedule(this.cron.pattern, () => CronBuilder.rTracerPlugin.runWithId(() => this.cron.handler(getCtx())), options);
92
- }
93
- return schedule(this.cron.pattern, () => this.cron.handler(getCtx()), options);
80
+ const getCtx = () => ({
81
+ name: this.cron.name,
82
+ traceId: CronBuilder.rTracerPlugin
83
+ ? CronBuilder.rTracerPlugin.id()
84
+ : null,
85
+ pattern: this.cron.pattern,
86
+ timezone: this.cron.timezone,
87
+ runOnInit: this.cron.runOnInit,
88
+ recoverMissedExecutions: this.cron.recoverMissedExecutions
89
+ });
90
+ const options = Options.create({
91
+ name: this.cron.name,
92
+ timezone: this.cron.timezone,
93
+ runOnInit: this.cron.runOnInit,
94
+ scheduled: this.cron.scheduled,
95
+ recoverMissedExecutions: this.cron.recoverMissedExecutions
96
+ });
97
+ this.cron.handler = async (...args) => {
98
+ return handler(...args);
94
99
  };
95
- if (!CronBuilder.exceptionHandler) {
96
- this.cron.handler = handler;
97
- return register();
100
+ if (CronBuilder.exceptionHandler) {
101
+ this.cron.handler = async (...args) => {
102
+ try {
103
+ await handler(...args);
104
+ }
105
+ catch (err) {
106
+ CronBuilder.exceptionHandler.handle(err);
107
+ }
108
+ };
98
109
  }
99
- this.cron.handler = async (...args) => {
100
- try {
101
- await handler(...args);
102
- }
103
- catch (err) {
104
- CronBuilder.exceptionHandler.handle(err);
110
+ if (CronBuilder.rTracerPlugin) {
111
+ const rTracerHandler = () => {
112
+ return CronBuilder.rTracerPlugin.runWithId(() => {
113
+ const ctx = getCtx();
114
+ if (CronBuilder.loggerIsSet) {
115
+ Log.channelOrVanilla('cronjob').info(ctx);
116
+ }
117
+ return this.cron.handler(ctx);
118
+ });
119
+ };
120
+ return schedule(this.cron.pattern, rTracerHandler, options);
121
+ }
122
+ const basicHandler = () => {
123
+ const ctx = getCtx();
124
+ if (CronBuilder.loggerIsSet) {
125
+ Log.channelOrVanilla('cronjob').info(ctx);
105
126
  }
127
+ return this.cron.handler(ctx);
106
128
  };
107
- return register();
129
+ return schedule(this.cron.pattern, basicHandler, options);
108
130
  }
109
131
  /**
110
132
  * Defines if the task needs to be scheduled to run
@@ -32,6 +32,15 @@ export declare class CronImpl extends Macroable {
32
32
  * ```
33
33
  */
34
34
  validate(pattern: string): boolean;
35
+ /**
36
+ * Register the logger for all tasks.
37
+ *
38
+ * @example
39
+ * ```ts
40
+ * Cron.setLogger()
41
+ * ```
42
+ */
43
+ setLogger(isToSetLogger: boolean): this;
35
44
  /**
36
45
  * Register the error handler for all tasks.
37
46
  *
@@ -37,6 +37,18 @@ export class CronImpl extends Macroable {
37
37
  validate(pattern) {
38
38
  return validate(pattern);
39
39
  }
40
+ /**
41
+ * Register the logger for all tasks.
42
+ *
43
+ * @example
44
+ * ```ts
45
+ * Cron.setLogger()
46
+ * ```
47
+ */
48
+ setLogger(isToSetLogger) {
49
+ CronBuilder.loggerIsSet = isToSetLogger;
50
+ return this;
51
+ }
40
52
  /**
41
53
  * Register the error handler for all tasks.
42
54
  *
@@ -42,21 +42,11 @@ export class CronExceptionHandler {
42
42
  if (!this.canBeLogged(error)) {
43
43
  return;
44
44
  }
45
- const isUsingJsonFormatter = Config.is('logging.channels.exception.formatter', 'json');
46
- if (isUsingJsonFormatter) {
47
- Log.channelOrVanilla('exception').error({
48
- name: error.name,
49
- code: error.code,
50
- status: error.status,
51
- message: error.message,
52
- help: error.help,
53
- cause: error.cause,
54
- details: error.details,
55
- stack: error.stack
56
- });
45
+ if (Config.is('cron.logger.prettifyException', true)) {
46
+ Log.channelOrVanilla('exception').error(await error.prettify());
57
47
  return;
58
48
  }
59
- Log.channelOrVanilla('exception').error(await error.prettify());
49
+ Log.channelOrVanilla('exception').error(error);
60
50
  }
61
51
  /**
62
52
  * Return a boolean indicating if the error can be logged or not.
@@ -7,6 +7,10 @@ export declare class CronKernel {
7
7
  * Register the exception handler for all Cron tasks.
8
8
  */
9
9
  registerExceptionHandler(path?: string): Promise<void>;
10
+ /**
11
+ * Register the logger to all Cron tasks.
12
+ */
13
+ registerLogger(): Promise<void>;
10
14
  /**
11
15
  * Register all the schedulers found inside "rc.schedulers" config
12
16
  * inside the service provider.
@@ -32,6 +32,17 @@ export class CronKernel {
32
32
  const Handler = await Module.resolve(path, this.getParentURL());
33
33
  CronBuilder.exceptionHandler = new Handler();
34
34
  }
35
+ /**
36
+ * Register the logger to all Cron tasks.
37
+ */
38
+ async registerLogger() {
39
+ if (!Config.exists('cron.logger.enabled') ||
40
+ Config.is('cron.logger.enabled', false)) {
41
+ debug('Not able to register cronjob logger. Enable it in your cron.logger.enabled configuration.');
42
+ return;
43
+ }
44
+ CronBuilder.loggerIsSet = Config.is('cron.logger.enabled', true);
45
+ }
35
46
  /**
36
47
  * Register all the schedulers found inside "rc.schedulers" config
37
48
  * inside the service provider.