@outloud/adonis-scheduler 1.0.6 → 1.0.7
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/README.md +6 -1
- package/build/{chunk-FOVOD6FP.js → chunk-6VW5PUDE.js} +25 -2
- package/build/{chunk-7P6J5U6O.js → chunk-OIXFFNQU.js} +4 -1
- package/build/{command.task-GUNK3QFY.js → command.task-GALKWNLQ.js} +1 -1
- package/build/commands/scheduler_run.d.ts +1 -1
- package/build/commands/scheduler_run.js +2 -1
- package/build/index.d.ts +2 -2
- package/build/index.js +2 -2
- package/build/providers/scheduler.provider.d.ts +1 -1
- package/build/providers/scheduler.provider.js +2 -1
- package/build/{scheduler-DO35E0Fu.d.ts → scheduler-vBDqaysB.d.ts} +5 -1
- package/build/services/main.d.ts +1 -1
- package/build/stubs/config/scheduler.stub +1 -0
- package/package.json +2 -3
package/README.md
CHANGED
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
- Cancellation support for long-running tasks.
|
|
24
24
|
- Graceful shutdown.
|
|
25
25
|
- Global and task-level error handling.
|
|
26
|
+
- Auto-discovery of tasks.
|
|
26
27
|
|
|
27
28
|
## Getting Started
|
|
28
29
|
|
|
@@ -57,8 +58,12 @@ export default class TestTask extends Task {
|
|
|
57
58
|
```
|
|
58
59
|
|
|
59
60
|
### Register a task
|
|
61
|
+
For task to run it must be registered in the scheduler.
|
|
60
62
|
|
|
61
|
-
|
|
63
|
+
> [!NOTE]
|
|
64
|
+
> By default tasks are auto-discovered using the locations defined in config.
|
|
65
|
+
|
|
66
|
+
If you want to register tasks manually, you can register tasks in two ways: using the `start/scheduler.ts` preloaded file or in a provider's `start` method.
|
|
62
67
|
|
|
63
68
|
Using `start/scheduler.ts` file.
|
|
64
69
|
|
|
@@ -1,4 +1,7 @@
|
|
|
1
|
+
import { Task } from './chunk-OIXFFNQU.js';
|
|
1
2
|
import { __name } from './chunk-SHUYVCID.js';
|
|
3
|
+
import { glob } from 'fs/promises';
|
|
4
|
+
import { resolve } from 'path';
|
|
2
5
|
import { Cron } from 'croner';
|
|
3
6
|
import timers from 'timers/promises';
|
|
4
7
|
|
|
@@ -40,15 +43,18 @@ var Scheduler = class {
|
|
|
40
43
|
options.command
|
|
41
44
|
];
|
|
42
45
|
Object.assign(definition, options);
|
|
43
|
-
definition.loader = () => import('./command.task-
|
|
46
|
+
definition.loader = () => import('./command.task-GALKWNLQ.js').then((module) => {
|
|
44
47
|
return class extends module.CommandTask {
|
|
45
48
|
static command = command;
|
|
46
49
|
};
|
|
47
50
|
});
|
|
51
|
+
} else if (Task.isTask(options)) {
|
|
52
|
+
definition.task = options;
|
|
53
|
+
Object.assign(definition, options.options ?? {});
|
|
48
54
|
} else {
|
|
49
55
|
definition.loader = options;
|
|
50
56
|
}
|
|
51
|
-
if (!definition.loader) {
|
|
57
|
+
if (!definition.task && !definition.loader) {
|
|
52
58
|
throw new Error("Task definition must have either a command or a task defined.");
|
|
53
59
|
}
|
|
54
60
|
this.definitions.push(definition);
|
|
@@ -69,6 +75,9 @@ var Scheduler = class {
|
|
|
69
75
|
return;
|
|
70
76
|
}
|
|
71
77
|
this.setState("starting");
|
|
78
|
+
if (this.config.locations) {
|
|
79
|
+
await Promise.all(this.config.locations.map((location) => this.registerFromGlob(location)));
|
|
80
|
+
}
|
|
72
81
|
await Promise.all(this.definitions.map((definition) => this.schedule(definition)));
|
|
73
82
|
if (!this.definitions.length) {
|
|
74
83
|
this.logger.warn("No tasks registered, scheduler will not run any jobs.");
|
|
@@ -94,6 +103,20 @@ var Scheduler = class {
|
|
|
94
103
|
this.errorHandler = callback;
|
|
95
104
|
return this;
|
|
96
105
|
}
|
|
106
|
+
async registerFromGlob(pattern) {
|
|
107
|
+
const normalizedPattern = pattern.replace(/\.(?:js|ts)$/, ".{js,ts}");
|
|
108
|
+
for await (const file of glob(normalizedPattern)) {
|
|
109
|
+
try {
|
|
110
|
+
const absolutePath = resolve(file);
|
|
111
|
+
const module = await import(`file://${absolutePath}`);
|
|
112
|
+
if (Task.isTask(module.default)) {
|
|
113
|
+
this.register(module.default);
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
console.warn(`Failed to load task from ${file}:`, error);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
97
120
|
async load(definition) {
|
|
98
121
|
if (definition.loader) {
|
|
99
122
|
const module = await definition.loader();
|
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import { __name } from './chunk-SHUYVCID.js';
|
|
2
2
|
|
|
3
3
|
// src/task.ts
|
|
4
|
-
var Task = class {
|
|
4
|
+
var Task = class _Task {
|
|
5
5
|
static {
|
|
6
6
|
__name(this, "Task");
|
|
7
7
|
}
|
|
8
8
|
isCanceled = false;
|
|
9
9
|
promise;
|
|
10
10
|
static options;
|
|
11
|
+
static isTask(obj) {
|
|
12
|
+
return typeof obj === "function" && obj.prototype instanceof _Task;
|
|
13
|
+
}
|
|
11
14
|
// eslint-disable-next-line @typescript-eslint/no-useless-constructor
|
|
12
15
|
constructor(..._) {
|
|
13
16
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { BaseCommand } from '@adonisjs/core/ace';
|
|
2
2
|
import { CommandOptions } from '@adonisjs/core/types/ace';
|
|
3
|
-
import { a as Scheduler } from '../scheduler-
|
|
3
|
+
import { a as Scheduler } from '../scheduler-vBDqaysB.js';
|
|
4
4
|
import '@adonisjs/core/container';
|
|
5
5
|
import '@adonisjs/core/types';
|
|
6
6
|
import '@adonisjs/lock/types';
|
package/build/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { S as SchedulerConfig } from './scheduler-
|
|
2
|
-
export { a as Scheduler, T as Task, b as TaskOptions } from './scheduler-
|
|
1
|
+
import { S as SchedulerConfig } from './scheduler-vBDqaysB.js';
|
|
2
|
+
export { a as Scheduler, T as Task, b as TaskOptions } from './scheduler-vBDqaysB.js';
|
|
3
3
|
import Configure from '@adonisjs/core/commands/configure';
|
|
4
4
|
import '@adonisjs/core/container';
|
|
5
5
|
import '@adonisjs/core/types';
|
package/build/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ApplicationService } from '@adonisjs/core/types';
|
|
2
|
-
import { a as Scheduler, c as SchedulerEvents } from '../scheduler-
|
|
2
|
+
import { a as Scheduler, c as SchedulerEvents } from '../scheduler-vBDqaysB.js';
|
|
3
3
|
import '@adonisjs/core/container';
|
|
4
4
|
import '@adonisjs/lock/types';
|
|
5
5
|
import '@adonisjs/core/logger';
|
|
@@ -17,6 +17,8 @@ interface SchedulerConfig {
|
|
|
17
17
|
* The default ttl for the lock.
|
|
18
18
|
*/
|
|
19
19
|
lockDuration: number | string;
|
|
20
|
+
/** Locations for task auto-discovery */
|
|
21
|
+
locations?: string[];
|
|
20
22
|
}
|
|
21
23
|
interface TaskOptions {
|
|
22
24
|
/**
|
|
@@ -61,6 +63,7 @@ declare abstract class Task {
|
|
|
61
63
|
isCanceled: boolean;
|
|
62
64
|
promise?: Promise<any>;
|
|
63
65
|
static options: TaskOptions;
|
|
66
|
+
static isTask(obj: unknown): obj is typeof Task;
|
|
64
67
|
constructor(..._: any[]);
|
|
65
68
|
get name(): string;
|
|
66
69
|
abstract run(...args: any[]): Promise<void>;
|
|
@@ -81,10 +84,11 @@ declare class Scheduler {
|
|
|
81
84
|
private state;
|
|
82
85
|
private errorHandler?;
|
|
83
86
|
constructor(config: SchedulerConfig, resolver: ContainerResolver<ContainerBindings>, logger: Logger, emitter: Emitter<any>, locks?: LockService | undefined);
|
|
84
|
-
register(options: TaskRegisterOptions | Factory<typeof Task>): this;
|
|
87
|
+
register(options: TaskRegisterOptions | typeof Task | Factory<typeof Task>): this;
|
|
85
88
|
start(wait?: boolean): Promise<void>;
|
|
86
89
|
stop(): Promise<void>;
|
|
87
90
|
onError(callback: ErrorHandler): this;
|
|
91
|
+
private registerFromGlob;
|
|
88
92
|
private load;
|
|
89
93
|
private make;
|
|
90
94
|
private run;
|
package/build/services/main.d.ts
CHANGED
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@outloud/adonis-scheduler",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.7",
|
|
5
5
|
"description": "Schedule cron jobs in AdonisJS.",
|
|
6
6
|
"author": "Outloud <hello@outloud.co>",
|
|
7
7
|
"contributors": [
|
|
@@ -66,7 +66,6 @@
|
|
|
66
66
|
"postcompile": "pnpm run copy:templates && pnpm run index:commands",
|
|
67
67
|
"build": "rm -rf build && pnpm run compile",
|
|
68
68
|
"release": "release-it",
|
|
69
|
-
"index:commands": "adonis-kit index build/commands"
|
|
70
|
-
"version": "pnpm run build"
|
|
69
|
+
"index:commands": "adonis-kit index build/commands"
|
|
71
70
|
}
|
|
72
71
|
}
|