Add ability to run background tasks on a timer
This commit is contained in:
parent
8d1befcf25
commit
9e05d884cf
5 changed files with 109 additions and 2 deletions
|
@ -30,6 +30,7 @@
|
||||||
"@discordjs/rest": "^2.0.0",
|
"@discordjs/rest": "^2.0.0",
|
||||||
"@types/jest": "^29.0.0",
|
"@types/jest": "^29.0.0",
|
||||||
"@types/uuid": "^9.0.0",
|
"@types/uuid": "^9.0.0",
|
||||||
|
"cron": "^3.3.1",
|
||||||
"discord.js": "^14.3.0",
|
"discord.js": "^14.3.0",
|
||||||
"dotenv": "^16.0.0",
|
"dotenv": "^16.0.0",
|
||||||
"emoji-regex": "^10.0.0",
|
"emoji-regex": "^10.0.0",
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
import { Client, Partials } from "discord.js";
|
import { Client, Partials } from "discord.js";
|
||||||
import * as dotenv from "dotenv";
|
import * as dotenv from "dotenv";
|
||||||
import { createConnection } from "typeorm";
|
|
||||||
import { EventType } from "../constants/EventType";
|
import { EventType } from "../constants/EventType";
|
||||||
import ICommandItem from "../contracts/ICommandItem";
|
import ICommandItem from "../contracts/ICommandItem";
|
||||||
import IEventItem from "../contracts/IEventItem";
|
import IEventItem from "../contracts/IEventItem";
|
||||||
|
@ -12,6 +11,7 @@ import AppDataSource from "../database/dataSources/appDataSource";
|
||||||
import ButtonEventItem from "../contracts/ButtonEventItem";
|
import ButtonEventItem from "../contracts/ButtonEventItem";
|
||||||
import { ButtonEvent } from "../type/buttonEvent";
|
import { ButtonEvent } from "../type/buttonEvent";
|
||||||
import CacheHelper from "../helpers/CacheHelper";
|
import CacheHelper from "../helpers/CacheHelper";
|
||||||
|
import TimerHelper from "../helpers/TimerHelper";
|
||||||
|
|
||||||
export class CoreClient extends Client {
|
export class CoreClient extends Client {
|
||||||
private static _commandItems: ICommandItem[];
|
private static _commandItems: ICommandItem[];
|
||||||
|
@ -20,6 +20,7 @@ export class CoreClient extends Client {
|
||||||
|
|
||||||
private _events: Events;
|
private _events: Events;
|
||||||
private _util: Util;
|
private _util: Util;
|
||||||
|
private _timerHelper: TimerHelper;
|
||||||
|
|
||||||
public static get commandItems(): ICommandItem[] {
|
public static get commandItems(): ICommandItem[] {
|
||||||
return this._commandItems;
|
return this._commandItems;
|
||||||
|
@ -43,6 +44,7 @@ export class CoreClient extends Client {
|
||||||
|
|
||||||
this._events = new Events();
|
this._events = new Events();
|
||||||
this._util = new Util();
|
this._util = new Util();
|
||||||
|
this._timerHelper = new TimerHelper();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async start() {
|
public async start() {
|
||||||
|
@ -52,7 +54,11 @@ export class CoreClient extends Client {
|
||||||
}
|
}
|
||||||
|
|
||||||
await AppDataSource.initialize()
|
await AppDataSource.initialize()
|
||||||
.then(() => console.log("Data Source Initialized"))
|
.then(() => {
|
||||||
|
console.log("Data Source Initialized");
|
||||||
|
|
||||||
|
// this.timerHelper.AddTimer
|
||||||
|
})
|
||||||
.catch((err) => console.error("Error Initialising Data Source", err));
|
.catch((err) => console.error("Error Initialising Data Source", err));
|
||||||
|
|
||||||
super.on("interactionCreate", this._events.onInteractionCreate);
|
super.on("interactionCreate", this._events.onInteractionCreate);
|
||||||
|
|
81
src/helpers/TimerHelper.ts
Normal file
81
src/helpers/TimerHelper.ts
Normal file
|
@ -0,0 +1,81 @@
|
||||||
|
import {CronJob} from "cron";
|
||||||
|
import {primitive} from "../type/primitive";
|
||||||
|
import {v4} from "uuid";
|
||||||
|
|
||||||
|
interface Timer {
|
||||||
|
id: string;
|
||||||
|
job: CronJob;
|
||||||
|
context: Map<string, primitive>;
|
||||||
|
onTick: ((context: Map<string, primitive>) => void) | ((context: Map<string, primitive>) => Promise<void>);
|
||||||
|
runOnStart: boolean;
|
||||||
|
}
|
||||||
|
|
||||||
|
export default class TimerHelper {
|
||||||
|
private _timers: Timer[];
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
this._timers = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
public AddTimer(
|
||||||
|
cronTime: string,
|
||||||
|
timeZone: string,
|
||||||
|
onTick: ((context: Map<string, primitive>) => void) | ((context: Map<string, primitive>) => Promise<void>),
|
||||||
|
runOnStart: boolean = false): string {
|
||||||
|
const context = new Map<string, primitive>();
|
||||||
|
|
||||||
|
const job = new CronJob(
|
||||||
|
cronTime,
|
||||||
|
() => {
|
||||||
|
onTick(context);
|
||||||
|
},
|
||||||
|
null,
|
||||||
|
false,
|
||||||
|
timeZone,
|
||||||
|
);
|
||||||
|
|
||||||
|
const id = v4();
|
||||||
|
|
||||||
|
this._timers.push({
|
||||||
|
id,
|
||||||
|
job,
|
||||||
|
context,
|
||||||
|
onTick,
|
||||||
|
runOnStart,
|
||||||
|
});
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public StartAllTimers() {
|
||||||
|
this._timers.forEach(timer => this.StartJob(timer));
|
||||||
|
}
|
||||||
|
|
||||||
|
public StopAllTimers() {
|
||||||
|
this._timers.forEach(timer => timer.job.stop());
|
||||||
|
}
|
||||||
|
|
||||||
|
public StartTimer(id: string) {
|
||||||
|
const timer = this._timers.find(x => x.id == id);
|
||||||
|
|
||||||
|
if (!timer) return;
|
||||||
|
|
||||||
|
this.StartJob(timer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public StopTimer(id: string) {
|
||||||
|
const timer = this._timers.find(x => x.id == id);
|
||||||
|
|
||||||
|
if (!timer) return;
|
||||||
|
|
||||||
|
timer.job.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
private StartJob(timer: Timer) {
|
||||||
|
timer.job.start();
|
||||||
|
|
||||||
|
if (timer.runOnStart) {
|
||||||
|
timer.onTick(timer.context);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
1
src/type/primitive.ts
Normal file
1
src/type/primitive.ts
Normal file
|
@ -0,0 +1 @@
|
||||||
|
export type primitive = string | number | boolean;
|
18
yarn.lock
18
yarn.lock
|
@ -788,6 +788,11 @@
|
||||||
expect "^29.0.0"
|
expect "^29.0.0"
|
||||||
pretty-format "^29.0.0"
|
pretty-format "^29.0.0"
|
||||||
|
|
||||||
|
"@types/luxon@~3.4.0":
|
||||||
|
version "3.4.2"
|
||||||
|
resolved "https://registry.yarnpkg.com/@types/luxon/-/luxon-3.4.2.tgz#e4fc7214a420173cea47739c33cdf10874694db7"
|
||||||
|
integrity sha512-TifLZlFudklWlMBfhubvgqTXRzLDI5pCbGa4P8a3wPyUQSW+1xQ5eDsreP9DWHX3tjq1ke96uYG/nwundroWcA==
|
||||||
|
|
||||||
"@types/node@*":
|
"@types/node@*":
|
||||||
version "22.7.5"
|
version "22.7.5"
|
||||||
resolved "https://registry.npmjs.org/@types/node/-/node-22.7.5.tgz"
|
resolved "https://registry.npmjs.org/@types/node/-/node-22.7.5.tgz"
|
||||||
|
@ -1458,6 +1463,14 @@ create-jest@^29.7.0:
|
||||||
jest-util "^29.7.0"
|
jest-util "^29.7.0"
|
||||||
prompts "^2.0.1"
|
prompts "^2.0.1"
|
||||||
|
|
||||||
|
cron@^3.3.1:
|
||||||
|
version "3.3.1"
|
||||||
|
resolved "https://registry.yarnpkg.com/cron/-/cron-3.3.1.tgz#03c56b4a3ad52606160adfba1fab932c53838807"
|
||||||
|
integrity sha512-KpvuzJEbeTMTfLsXhUuDfsFYr8s5roUlLKb4fa68GszWrA4783C7q6m9yj4vyc6neyD/V9e0YiADSX2c+yRDXg==
|
||||||
|
dependencies:
|
||||||
|
"@types/luxon" "~3.4.0"
|
||||||
|
luxon "~3.5.0"
|
||||||
|
|
||||||
cross-spawn@^7.0.0, cross-spawn@^7.0.3:
|
cross-spawn@^7.0.0, cross-spawn@^7.0.3:
|
||||||
version "7.0.3"
|
version "7.0.3"
|
||||||
resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz"
|
resolved "https://registry.npmjs.org/cross-spawn/-/cross-spawn-7.0.3.tgz"
|
||||||
|
@ -3076,6 +3089,11 @@ lru-cache@^5.1.1:
|
||||||
dependencies:
|
dependencies:
|
||||||
yallist "^3.0.2"
|
yallist "^3.0.2"
|
||||||
|
|
||||||
|
luxon@~3.5.0:
|
||||||
|
version "3.5.0"
|
||||||
|
resolved "https://registry.yarnpkg.com/luxon/-/luxon-3.5.0.tgz#6b6f65c5cd1d61d1fd19dbf07ee87a50bf4b8e20"
|
||||||
|
integrity sha512-rh+Zjr6DNfUYR3bPwJEnuwDdqMbxZW7LOQfUN4B54+Cl+0o5zaU9RJ6bcidfDtC1cWCZXQ+nvX8bf6bAji37QQ==
|
||||||
|
|
||||||
magic-bytes.js@^1.10.0:
|
magic-bytes.js@^1.10.0:
|
||||||
version "1.10.0"
|
version "1.10.0"
|
||||||
resolved "https://registry.npmjs.org/magic-bytes.js/-/magic-bytes.js-1.10.0.tgz"
|
resolved "https://registry.npmjs.org/magic-bytes.js/-/magic-bytes.js-1.10.0.tgz"
|
||||||
|
|
Loading…
Reference in a new issue