Added cron event handler

This commit is contained in:
2023-12-29 01:42:03 +01:00
parent b8d138bb0c
commit 7376c7ddd9
7 changed files with 495 additions and 3 deletions

20
src/utils/Cron.ts Normal file
View File

@@ -0,0 +1,20 @@
import { CronJob } from "cron"
import path from "path"
import fs from "fs"
import { Cron } from "../interfaces"
function loadCronEvents() {
const cronPath = path.join(__dirname, "..", "events", "cron")
const cronFiles = fs.readdirSync(cronPath).filter(file => file.endsWith(".js"))
for (const file of cronFiles) {
const filePath = path.join(cronPath, file)
const cron: Cron = require(filePath)
const time = cron.time.seconds + " " + cron.time.minutes + " " + cron.time.hours + " " + cron.time.dayOfMonth + " " + cron.time.month + " " + cron.time.dayOfWeek
new CronJob(time, cron.execute, cron.onComplete, cron.start, cron.timeZone).start()
}
}
export { loadCronEvents }

View File

@@ -3,6 +3,7 @@ import config from "./Config"
import { redis } from "./Redis"
import { connect } from "mongoose"
import init from "./Init"
import { loadCronEvents } from "./Cron"
const client = new Client()
export default class Illegitimate {
@@ -10,13 +11,11 @@ export default class Illegitimate {
async start() {
init()
client.start()
loadCronEvents()
redis.on("ready", () => {
console.log("Connected to Redis")
})
connect(config.prod.mongoURI!, {}).then(() => {
console.log("Connected to MongoDB")
})