-
-
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.
revising cron modules and better error messages
- Loading branch information
Showing
15 changed files
with
148 additions
and
105 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -113,3 +113,4 @@ tsconfig-cjs.json | |
tsconfig-esm.json | ||
|
||
renovate.json | ||
fortnite |
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,7 +1,7 @@ | ||
{ | ||
"name": "@sern/handler", | ||
"packageManager": "[email protected]", | ||
"version": "3.3.4", | ||
"version": "4.0.0", | ||
"description": "A complete, customizable, typesafe, & reactive framework for discord bots.", | ||
"main": "./dist/index.js", | ||
"module": "./dist/index.js", | ||
|
@@ -35,10 +35,10 @@ | |
"author": "SernDevs", | ||
"license": "MIT", | ||
"dependencies": { | ||
"@sern/ioc": "^1.0.4", | ||
"@sern/ioc": "^1.1.0", | ||
"callsites": "^3.1.0", | ||
"cron": "^3.1.7", | ||
"deepmerge": "^4.3.1", | ||
"node-cron": "^3.0.3", | ||
"rxjs": "^7.8.0", | ||
"ts-results-es": "^4.1.0" | ||
}, | ||
|
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 |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { CronJob } from 'cron'; | ||
export class TaskScheduler { | ||
private __tasks: Map<string, CronJob> = new Map(); | ||
|
||
scheduleTask(taskName: string, cronExpression: string, task: () => void): boolean { | ||
if (this.__tasks.has(taskName)) { | ||
return false; | ||
} | ||
try { | ||
const job = new CronJob(cronExpression, task); | ||
job.start(); | ||
this.__tasks.set(taskName, job); | ||
return true; | ||
} catch (error) { | ||
return false; | ||
} | ||
} | ||
|
||
private stopTask(taskName: string): boolean { | ||
const job = this.__tasks.get(taskName); | ||
if (job) { | ||
job.stop(); | ||
this.__tasks.delete(taskName); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
private restartTask(taskName: string): boolean { | ||
const job = this.__tasks.get(taskName); | ||
if (job) { | ||
job.start(); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
|
||
tasks(): string[] { | ||
return Array.from(this.__tasks.keys()); | ||
} | ||
|
||
} |
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
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { TaskScheduler } from "../core/schedule" | ||
import * as Files from '../core/module-loading' | ||
import { UnpackedDependencies } from "../types/utility"; | ||
|
||
interface ScheduledTaskModule { | ||
name?: string; | ||
description?: string; | ||
pattern: string; | ||
execute(deps: UnpackedDependencies, tasks: string[]): any | ||
} | ||
|
||
export const registerTasks = async (path: string, deps: UnpackedDependencies) => { | ||
const taskManager = new TaskScheduler() | ||
|
||
for await (const f of Files.readRecursive(path)) { | ||
let { module } = await Files.importModule<ScheduledTaskModule>(f); | ||
//module.name is assigned by Files.importModule<> | ||
taskManager.scheduleTask(module.name!, module.pattern, () => { | ||
module.execute(deps, taskManager.tasks()) | ||
}) | ||
} | ||
|
||
} |
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
Oops, something went wrong.