aboutsummaryrefslogtreecommitdiff
path: root/deno/base/cron.ts
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-06-05 22:30:51 +0800
committerYuqian Yang <crupest@crupest.life>2025-06-09 21:48:00 +0800
commit0b702d027973ea26d7e9618d4edc181d4cd1fc31 (patch)
tree5f568ed7f2ff756e39e78ff928ab2f26ddaf08da /deno/base/cron.ts
parent2004236ad040b7db3f7fa7e3c3edae52fd9bca13 (diff)
downloadcrupest-0b702d027973ea26d7e9618d4edc181d4cd1fc31.tar.gz
crupest-0b702d027973ea26d7e9618d4edc181d4cd1fc31.tar.bz2
crupest-0b702d027973ea26d7e9618d4edc181d4cd1fc31.zip
feat(deno): move deno (mail-server) to top level.
Diffstat (limited to 'deno/base/cron.ts')
-rw-r--r--deno/base/cron.ts43
1 files changed, 43 insertions, 0 deletions
diff --git a/deno/base/cron.ts b/deno/base/cron.ts
new file mode 100644
index 0000000..bf0a0be
--- /dev/null
+++ b/deno/base/cron.ts
@@ -0,0 +1,43 @@
+export type CronCallback = (task: CronTask) => Promise<void>;
+
+export interface CronTaskConfig {
+ readonly name: string;
+ readonly interval: number;
+ readonly callback: CronCallback;
+ readonly startNow?: boolean;
+}
+
+export class CronTask {
+ #timerTag: number | null = null;
+
+ constructor(public readonly config: CronTaskConfig) {
+ if (config.interval <= 0) {
+ throw new Error("Cron task interval must be positive.");
+ }
+
+ if (config.startNow === true) {
+ this.start();
+ }
+ }
+
+ get running(): boolean {
+ return this.#timerTag != null;
+ }
+
+ start() {
+ if (this.#timerTag == null) {
+ this.#timerTag = setInterval(
+ this.config.callback,
+ this.config.interval,
+ this,
+ );
+ }
+ }
+
+ stop() {
+ if (this.#timerTag != null) {
+ clearInterval(this.#timerTag);
+ this.#timerTag = null;
+ }
+ }
+}