@athenna/cron 5.0.0 → 5.2.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.0.0",
3
+ "version": "5.2.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,7 +57,7 @@
57
57
  "node-cron": "^3.0.3"
58
58
  },
59
59
  "devDependencies": {
60
- "@athenna/artisan": "^5.0.0",
60
+ "@athenna/artisan": "^5.1.0",
61
61
  "@athenna/common": "^5.0.0",
62
62
  "@athenna/config": "^5.0.0",
63
63
  "@athenna/ioc": "^5.0.0",
@@ -51,6 +51,15 @@ export declare class CronImpl {
51
51
  * ```
52
52
  */
53
53
  getTasks(): Map<string, ScheduledTask>;
54
+ /**
55
+ * Run a scheduler by its name.
56
+ *
57
+ * @example
58
+ * ```ts
59
+ * await Cron.runByName('MySchedulerClassName')
60
+ * ```
61
+ */
62
+ runByName(name: string): Promise<void>;
54
63
  /**
55
64
  * Close all scheduled tasks.
56
65
  *
@@ -8,6 +8,7 @@
8
8
  */
9
9
  import { getTasks, validate } from 'node-cron';
10
10
  import { CronBuilder } from '#src/cron/CronBuilder';
11
+ import { NotFoundTaskNameException } from '#src/exceptions/NotFoundTaskNameException';
11
12
  export class CronImpl {
12
13
  /**
13
14
  * Creates a new instance of CronBuilder to register
@@ -60,6 +61,23 @@ export class CronImpl {
60
61
  getTasks() {
61
62
  return getTasks();
62
63
  }
64
+ /**
65
+ * Run a scheduler by its name.
66
+ *
67
+ * @example
68
+ * ```ts
69
+ * await Cron.runByName('MySchedulerClassName')
70
+ * ```
71
+ */
72
+ async runByName(name) {
73
+ const task = this.getTasks()?.get(name);
74
+ if (!task) {
75
+ throw new NotFoundTaskNameException(name);
76
+ }
77
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
78
+ // @ts-ignore
79
+ await task._task._execution();
80
+ }
63
81
  /**
64
82
  * Close all scheduled tasks.
65
83
  *
@@ -0,0 +1,4 @@
1
+ import { Exception } from '@athenna/common';
2
+ export declare class NotFoundTaskNameException extends Exception {
3
+ constructor(name: string);
4
+ }
@@ -0,0 +1,10 @@
1
+ import { Exception } from '@athenna/common';
2
+ export class NotFoundTaskNameException extends Exception {
3
+ constructor(name) {
4
+ const status = 500;
5
+ const help = `You need to define a scheduler with name ({yellow} "${name}") in your route file or set the name in your scheduler annotation.`;
6
+ const code = 'E_NOT_FOUND_TASK';
7
+ const message = `The scheduler with name ({yellow} "${name}") does not exist.`;
8
+ super({ code, help, status, message });
9
+ }
10
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @athenna/cron
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { TestScheduler } from '#src/testing/plugins/scheduler/TestScheduler';
10
+ declare module '@japa/runner/core' {
11
+ interface TestContext {
12
+ scheduler: TestScheduler;
13
+ }
14
+ }
15
+ export * from '#src/testing/plugins/scheduler/TestScheduler';
16
+ /**
17
+ * Scheduler plugin registers the scheduler macro to the test context.
18
+ */
19
+ export declare function scheduler(): () => void;
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @athenna/cron
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { TestContext } from '@japa/runner/core';
10
+ import { TestScheduler } from '#src/testing/plugins/scheduler/TestScheduler';
11
+ export * from '#src/testing/plugins/scheduler/TestScheduler';
12
+ /**
13
+ * Scheduler plugin registers the scheduler macro to the test context.
14
+ */
15
+ export function scheduler() {
16
+ return function () {
17
+ TestContext.macro('scheduler', new TestScheduler());
18
+ };
19
+ }
@@ -0,0 +1,19 @@
1
+ /**
2
+ * @athenna/cron
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ export declare class TestScheduler {
10
+ /**
11
+ * Run a specific scheduler by its name.
12
+ *
13
+ * @example
14
+ * ```js
15
+ * await scheduler.runByName('MySchedulerClassName')
16
+ * ```
17
+ */
18
+ runByName(name: string): Promise<void>;
19
+ }
@@ -0,0 +1,22 @@
1
+ /**
2
+ * @athenna/cron
3
+ *
4
+ * (c) João Lenon <lenon@athenna.io>
5
+ *
6
+ * For the full copyright and license information, please view the LICENSE
7
+ * file that was distributed with this source code.
8
+ */
9
+ import { Cron } from '@athenna/cron';
10
+ export class TestScheduler {
11
+ /**
12
+ * Run a specific scheduler by its name.
13
+ *
14
+ * @example
15
+ * ```js
16
+ * await scheduler.runByName('MySchedulerClassName')
17
+ * ```
18
+ */
19
+ async runByName(name) {
20
+ await Cron.runByName(name);
21
+ }
22
+ }