-
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
59 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,29 @@ | ||
import { TaskScheduler } from "../core/schedule" | ||
import * as Files from '../core/module-loading' | ||
import { UnpackedDependencies } from "../types/utility"; | ||
import { ScheduledTask } from "../types/core-modules"; | ||
import { CronJob } from "cron"; | ||
import { relative } from "path"; | ||
import { fileURLToPath } from "url"; | ||
|
||
interface ScheduledTaskModule { | ||
name?: string; | ||
description?: string; | ||
pattern: string; | ||
execute(deps: UnpackedDependencies, tasks: string[]): any | ||
} | ||
|
||
export const registerTasks = async (path: string, deps: UnpackedDependencies) => { | ||
export const registerTasks = async (tasksPath: string, deps: UnpackedDependencies) => { | ||
const taskManager = new TaskScheduler() | ||
|
||
for await (const f of Files.readRecursive(path)) { | ||
let { module } = await Files.importModule<ScheduledTaskModule>(f); | ||
for await (const f of Files.readRecursive(tasksPath)) { | ||
let { module } = await Files.importModule<ScheduledTask & { meta: { absPath: string } }>(f); | ||
|
||
//module.name is assigned by Files.importModule<> | ||
taskManager.scheduleTask(module.name!, module.pattern, () => { | ||
module.execute(deps, taskManager.tasks()) | ||
}) | ||
// the id created for the task is unique | ||
const uuid = module.name!+":"+relative(tasksPath,fileURLToPath(f)) | ||
taskManager.scheduleTask(uuid, module.pattern, function(this: CronJob) { | ||
module.execute({ | ||
deps, | ||
runningTasks: taskManager.tasks(), | ||
lastTimeExecution: this.lastExecution, | ||
nextTimeExecution: this.nextDate().toJSDate() | ||
}) | ||
}, | ||
module.timezone) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters