Skip to content

Commit

Permalink
feat: task scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
zaida04 committed Jun 28, 2024
1 parent 424486e commit 1eca3a7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
15 changes: 12 additions & 3 deletions packages/gil/lib/structures/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ import { Collection } from "@discordjs/collection";
import glob from "fast-glob";
import { GilClient } from "../GilClient";
import { Manager } from "./Manager";
import Cron from "node-cron";

interface TaskOptions {
// The internal-safe name of the task
name: string;
// The interval to run the task. You can put anything that https://github.com/breejs/bree supports.
// For example, you can use crons like "0 0 * * *" to run the task every day at midnight.
// A cron representing the interval at which the task should run.
interval: string;
// Whether the task should run immediately upon startup.
runImmediately?: boolean;
}
export abstract class Task {
public constructor(
public readonly gil: GilClient,
public readonly options: TaskOptions,
) {}
) { }

public abstract execute(): unknown | Promise<unknown>;
}
Expand Down Expand Up @@ -48,6 +50,13 @@ export class TaskManager extends Manager {
const createdTask: Task = new imported.default(this.gil);
this.gil.logger.info(`Task ${createdTask.options.name} loaded.`);
this.tasks.set(createdTask.options.name, createdTask);

if (createdTask.options.runImmediately) {
this.gil.logger.info(`Running task ${createdTask.options.name} immediately.`);
createdTask.execute();
}

Cron.schedule(createdTask.options.interval, createdTask.execute.bind(createdTask));
}
}
}
4 changes: 3 additions & 1 deletion packages/gil/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@guildedjs/gil",
"version": "0.6.3",
"version": "0.6.4",
"description": "Framework for guilded.js that allows you to build bots with ease.",
"author": "Zaid \"Nico\" <contact@nico.engineer>",
"license": "MIT",
Expand All @@ -16,6 +16,7 @@
"devDependencies": {
"@types/better-sqlite3": "^7.6.9",
"@types/mongoose": "^5.11.97",
"@types/node-cron": "^3.0.11",
"better-sqlite3": "^9.4.3",
"dotenv": "^16.0.3",
"mongoose": "^8.2.3",
Expand All @@ -30,6 +31,7 @@
"fast-glob": "^3.3.2",
"guilded.js": "workspace:*",
"lexure": "^0.17.0",
"node-cron": "^3.0.3",
"typed-emitter": "^2.1.0"
},
"contributors": [
Expand Down

0 comments on commit 1eca3a7

Please sign in to comment.