adonisjs-scheduler
Advanced tools
Comparing version 0.0.18 to 0.0.19
@@ -8,2 +8,14 @@ "use strict"; | ||
const node_cron_1 = __importDefault(require("node-cron")); | ||
const async_lock_1 = __importDefault(require("async-lock")); | ||
const lock = new async_lock_1.default(); | ||
const run = async (cb, options) => { | ||
if (!options.enabled) | ||
await cb(); | ||
if (lock.isBusy(options.key)) { | ||
console.log(`${options.key} is busy`); | ||
return; | ||
} | ||
const unlock = await lock.acquire(options.key, cb, { maxPending: 1, timeout: options.timeout }); | ||
unlock(); | ||
}; | ||
class SchedulerCommand extends standalone_1.BaseCommand { | ||
@@ -13,10 +25,19 @@ async run() { | ||
const Ace = this.application.container.use('Adonis/Core/Ace'); | ||
for (const command of Scheduler.items) { | ||
for (let index = 0; index < Scheduler.items.length; index++) { | ||
const command = Scheduler.items[index]; | ||
node_cron_1.default.schedule(command.expression, async () => { | ||
switch (command.type) { | ||
case "command": | ||
await Ace.exec(command.commandName, command.commandArgs); | ||
await run(() => Ace.exec(command.commandName, command.commandArgs), { | ||
enabled: command.config.withoutOverlapping, | ||
timeout: command.config.expiresAt, | ||
key: `${index}-${command.commandName}-${command.commandArgs}` | ||
}); | ||
break; | ||
case "callback": | ||
await command.callback(); | ||
await run(() => command.callback(), { | ||
enabled: command.config.withoutOverlapping, | ||
timeout: command.config.expiresAt, | ||
key: `${index}-callback` | ||
}); | ||
default: | ||
@@ -26,3 +47,3 @@ break; | ||
}, { | ||
runOnInit: command.runOnInit | ||
runOnInit: command.config.immediate | ||
}); | ||
@@ -29,0 +50,0 @@ } |
@@ -5,4 +5,9 @@ import { BaseCommand } from '@adonisjs/core/build/standalone'; | ||
expression: string; | ||
runOnInit: boolean; | ||
config: { | ||
immediate: boolean; | ||
withoutOverlapping: boolean; | ||
expiresAt: number; | ||
}; | ||
immediate(state?: boolean): void; | ||
withoutOverlapping(expiresAt?: number): void; | ||
everyMinutes(minutes: number): this; | ||
@@ -38,3 +43,3 @@ everyMinute(): this; | ||
declare class ScheduleCommand extends BaseSchedule { | ||
type: string; | ||
type: "command"; | ||
commandName: string; | ||
@@ -45,3 +50,3 @@ commandArgs: string[]; | ||
declare class ScheduleCallback extends BaseSchedule { | ||
type: string; | ||
type: "callback"; | ||
callback: Function; | ||
@@ -51,6 +56,9 @@ constructor(callback: Function); | ||
export declare class Scheduler { | ||
items: BaseSchedule[]; | ||
items: (ScheduleCallback | ScheduleCommand)[]; | ||
command(name: string | typeof BaseCommand, args?: string[]): ScheduleCommand; | ||
call(callback: Function): ScheduleCallback; | ||
withoutOverlapping(callback: () => void, config?: { | ||
expiresAt: number; | ||
}): void; | ||
} | ||
export {}; |
@@ -13,12 +13,20 @@ "use strict"; | ||
}); | ||
Object.defineProperty(this, "runOnInit", { | ||
Object.defineProperty(this, "config", { | ||
enumerable: true, | ||
configurable: true, | ||
writable: true, | ||
value: false | ||
value: { | ||
immediate: false, | ||
withoutOverlapping: false, | ||
expiresAt: 3600000 | ||
} | ||
}); | ||
} | ||
immediate(state = true) { | ||
this.runOnInit = state; | ||
this.config.immediate = state; | ||
} | ||
withoutOverlapping(expiresAt = 3600000) { | ||
this.config.withoutOverlapping = true; | ||
this.config.expiresAt = expiresAt; | ||
} | ||
everyMinutes(minutes) { | ||
@@ -183,3 +191,12 @@ this.expression = (0, node_cron_expression_1.every)(minutes).minutes().toString(); | ||
} | ||
withoutOverlapping(callback, config = { expiresAt: 3600000 }) { | ||
const lastLength = this.items.length; | ||
callback(); | ||
const currentLength = this.items.length; | ||
const newItems = this.items.slice(lastLength, currentLength); | ||
for (const item of newItems) { | ||
item.withoutOverlapping(config.expiresAt); | ||
} | ||
} | ||
} | ||
exports.Scheduler = Scheduler; |
{ | ||
"name": "adonisjs-scheduler", | ||
"version": "0.0.18", | ||
"version": "0.0.19", | ||
"description": "Task scheduler for AdonisJS", | ||
@@ -34,2 +34,3 @@ "homepage": "https://github.com/KABBOUCHI/adonisjs-scheduler#readme", | ||
"@adonisjs/sink": "^5.3.0", | ||
"@types/async-lock": "^1.4.0", | ||
"@types/node-cron": "^3.0.7", | ||
@@ -82,2 +83,3 @@ "copyfiles": "^2.4.1", | ||
"dependencies": { | ||
"async-lock": "^1.4.0", | ||
"node-cron": "^3.0.2", | ||
@@ -84,0 +86,0 @@ "node-cron-expression": "^1.3.1" |
@@ -52,4 +52,9 @@ <div align="center"> | ||
Scheduler.command("inspire").everyFiveSeconds(); | ||
Scheduler.command(PurgeUsers, ["30 days"]).everyFiveSeconds(); | ||
Scheduler.command(PurgeUsers, ["30 days"]).everyFiveSeconds().withoutOverlapping(); | ||
Scheduler.withoutOverlapping(() => { | ||
Scheduler.command("inspire").everySecond(); | ||
Scheduler.command(PurgeUsers, ["30 days"]).everyFiveSeconds(); | ||
}, { expiresAt: 30_000 }); | ||
Scheduler.call(() => { | ||
@@ -85,2 +90,3 @@ console.log("Pruge DB!"); | ||
`.immediate();` | Run the task on startup | ||
`.withoutOverlapping();` | Run the task without overlapping | ||
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23356
493
91
3
10
+ Addedasync-lock@^1.4.0
+ Addedasync-lock@1.4.1(transitive)