@miniflare/scheduler
Advanced tools
Comparing version 2.0.0-next.3 to 2.0.0-rc.1
@@ -1,10 +0,15 @@ | ||
import { Compatibility } from '@miniflare/shared'; | ||
import { CorePluginSignatures } from '@miniflare/core'; | ||
import { Log } from '@miniflare/shared'; | ||
import type { Cron } from 'cron-schedule'; | ||
import type { ITimerHandle } from 'cron-schedule'; | ||
import { MiniflareCore } from '@miniflare/core'; | ||
import { MiniflareError } from '@miniflare/shared'; | ||
import { nodeCron } from 'node-cron'; | ||
import { Plugin } from '@miniflare/shared'; | ||
import { PluginContext } from '@miniflare/shared'; | ||
import { ReloadEvent } from '@miniflare/core'; | ||
export declare interface CronScheduler { | ||
setInterval(cron: Cron, task: () => any): ITimerHandle; | ||
clearTimeoutOrInterval(handle: ITimerHandle): void; | ||
} | ||
declare const kReload: unique symbol; | ||
@@ -14,10 +19,8 @@ | ||
private readonly mf; | ||
private readonly cron; | ||
private readonly cronScheduler; | ||
private previousValidatedCrons?; | ||
private scheduledTasks?; | ||
constructor(mf: MiniflareCore<Plugins>, cron?: Promise<{ | ||
default: nodeCron; | ||
}>); | ||
[kReload](event: ReloadEvent<Plugins>): Promise<void>; | ||
dispose(): void; | ||
private scheduledHandles?; | ||
constructor(mf: MiniflareCore<Plugins>, cronScheduler?: Promise<CronScheduler>); | ||
[kReload]: (event: ReloadEvent<Plugins>) => Promise<void>; | ||
dispose(): Promise<void>; | ||
} | ||
@@ -37,4 +40,4 @@ | ||
crons?: string[]; | ||
constructor(log: Log, compat: Compatibility, options?: SchedulerOptions); | ||
get validatedCrons(): string[]; | ||
constructor(ctx: PluginContext, options?: SchedulerOptions); | ||
get validatedCrons(): Cron[]; | ||
setup(): Promise<void>; | ||
@@ -47,6 +50,4 @@ } | ||
export declare function startScheduler<Plugins extends SchedulerPluginSignatures>(mf: MiniflareCore<Plugins>, cron?: Promise<{ | ||
default: nodeCron; | ||
}>): Promise<Scheduler<Plugins>>; | ||
export declare function startScheduler<Plugins extends SchedulerPluginSignatures>(mf: MiniflareCore<Plugins>, cronScheduler?: Promise<CronScheduler>): Promise<Scheduler<Plugins>>; | ||
export { } |
@@ -26,4 +26,2 @@ var __defProp = Object.defineProperty; | ||
} from "@miniflare/shared"; | ||
var noop = () => { | ||
}; | ||
var SchedulerError = class extends MiniflareError { | ||
@@ -34,4 +32,4 @@ }; | ||
#validatedCrons = []; | ||
constructor(log, compat, options) { | ||
super(log, compat); | ||
constructor(ctx, options) { | ||
super(ctx); | ||
this.assignOptions(options); | ||
@@ -43,15 +41,16 @@ } | ||
async setup() { | ||
const validatedCrons = []; | ||
if (!this.crons?.length) { | ||
this.#validatedCrons = validatedCrons; | ||
this.#validatedCrons = []; | ||
return; | ||
} | ||
const cron = await import("node-cron"); | ||
for (const spec of this.crons) { | ||
const { parseCronExpression } = await import("cron-schedule"); | ||
const validatedCrons = Array(this.crons.length); | ||
for (let i = 0; i < this.crons.length; i++) { | ||
const spec = this.crons[i]; | ||
try { | ||
const task = cron.default.schedule(spec, noop, { scheduled: false }); | ||
task.stop(); | ||
validatedCrons.push(spec); | ||
const cron = parseCronExpression(spec); | ||
cron.toString = () => spec; | ||
validatedCrons[i] = cron; | ||
} catch (e) { | ||
throw new SchedulerError("ERR_INVALID_CRON", `Unable to parse CRON "${spec}": ${e}`); | ||
throw new SchedulerError("ERR_INVALID_CRON", `Unable to parse CRON "${spec}": ${e.message}`); | ||
} | ||
@@ -75,11 +74,10 @@ } | ||
var Scheduler = class { | ||
constructor(mf, cron = import("node-cron")) { | ||
constructor(mf, cronScheduler = import("cron-schedule").then((module) => module.TimerBasedCronScheduler)) { | ||
this.mf = mf; | ||
this.cron = cron; | ||
this[kReload] = this[kReload].bind(this); | ||
this.cronScheduler = cronScheduler; | ||
mf.addEventListener("reload", this[kReload]); | ||
} | ||
previousValidatedCrons; | ||
scheduledTasks; | ||
async [kReload](event) { | ||
scheduledHandles; | ||
[kReload] = async (event) => { | ||
const validatedCrons = event.plugins.SchedulerPlugin.validatedCrons; | ||
@@ -89,25 +87,29 @@ if (this.previousValidatedCrons === validatedCrons) | ||
this.previousValidatedCrons = validatedCrons; | ||
this.scheduledTasks?.forEach((task) => task.destroy()); | ||
const cronScheduler = await this.cronScheduler; | ||
this.scheduledHandles?.forEach((handle) => cronScheduler.clearTimeoutOrInterval(handle)); | ||
if (!validatedCrons.length) | ||
return; | ||
const cron = await this.cron; | ||
this.scheduledTasks = validatedCrons?.map((expression) => cron.default.schedule(expression, async () => { | ||
const start = process.hrtime(); | ||
const waitUntil = this.mf.dispatchScheduled(void 0, expression); | ||
await logResponse(this.mf.log, { | ||
start, | ||
method: "SCHD", | ||
url: expression, | ||
waitUntil | ||
this.scheduledHandles = validatedCrons?.map((cron) => { | ||
const spec = cron.toString(); | ||
return cronScheduler.setInterval(cron, async () => { | ||
const start = process.hrtime(); | ||
const waitUntil = this.mf.dispatchScheduled(void 0, spec); | ||
await logResponse(this.mf.log, { | ||
start, | ||
method: "SCHD", | ||
url: spec, | ||
waitUntil | ||
}); | ||
}); | ||
})); | ||
} | ||
dispose() { | ||
}); | ||
}; | ||
async dispose() { | ||
this.mf.removeEventListener("reload", this[kReload]); | ||
this.scheduledTasks?.forEach((task) => task.destroy()); | ||
const cronScheduler = await this.cronScheduler; | ||
this.scheduledHandles?.forEach((handle) => cronScheduler.clearTimeoutOrInterval(handle)); | ||
} | ||
}; | ||
async function startScheduler(mf, cron) { | ||
const scheduler = new Scheduler(mf, cron); | ||
await scheduler[kReload](new ReloadEvent(await mf.getPlugins())); | ||
async function startScheduler(mf, cronScheduler) { | ||
const scheduler = new Scheduler(mf, cronScheduler); | ||
await scheduler[kReload](new ReloadEvent(await mf.getPlugins(), false)); | ||
return scheduler; | ||
@@ -114,0 +116,0 @@ } |
{ | ||
"name": "@miniflare/scheduler", | ||
"version": "2.0.0-next.3", | ||
"version": "2.0.0-rc.1", | ||
"description": "Scheduler module for Miniflare: a fun, full-featured, fully-local simulator for Cloudflare Workers", | ||
@@ -39,10 +39,9 @@ "keywords": [ | ||
"dependencies": { | ||
"@miniflare/core": "2.0.0-next.3", | ||
"@miniflare/shared": "2.0.0-next.3", | ||
"node-cron": "^3.0.0" | ||
"@miniflare/core": "2.0.0-rc.1", | ||
"@miniflare/shared": "2.0.0-rc.1", | ||
"cron-schedule": "^3.0.4" | ||
}, | ||
"devDependencies": { | ||
"@miniflare/shared-test": "2.0.0-next.3", | ||
"@types/node-cron": "^2.0.4" | ||
"@miniflare/shared-test": "2.0.0-rc.1" | ||
} | ||
} |
Sorry, the diff of this file is not supported yet
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
8490
1
155
+ Addedcron-schedule@^3.0.4
+ Added@miniflare/core@2.0.0-rc.1(transitive)
+ Added@miniflare/shared@2.0.0-rc.1(transitive)
+ Addedcron-schedule@3.0.6(transitive)
- Removednode-cron@^3.0.0
- Removed@miniflare/core@2.0.0-next.3(transitive)
- Removed@miniflare/shared@2.0.0-next.3(transitive)
- Removednode-cron@3.0.3(transitive)
- Removeduuid@8.3.2(transitive)
Updated@miniflare/core@2.0.0-rc.1
Updated@miniflare/shared@2.0.0-rc.1