aboutsummaryrefslogtreecommitdiff
path: root/deno/mail-relay/cron.ts
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-04-10 15:12:46 +0800
committerYuqian Yang <crupest@crupest.life>2025-06-05 21:07:37 +0800
commit750aaddc978156d370ab10737d6b9e4449b7eb7e (patch)
treed14d4ac7ebe02aa6cada75d72b4ec6eddc8b6d7b /deno/mail-relay/cron.ts
parent04c86d8cec22d1c0883eb240453c160ea12da162 (diff)
downloadcrupest-750aaddc978156d370ab10737d6b9e4449b7eb7e.tar.gz
crupest-750aaddc978156d370ab10737d6b9e4449b7eb7e.tar.bz2
crupest-750aaddc978156d370ab10737d6b9e4449b7eb7e.zip
feat(mail-server): done aws message id mapping.
Diffstat (limited to 'deno/mail-relay/cron.ts')
-rw-r--r--deno/mail-relay/cron.ts43
1 files changed, 43 insertions, 0 deletions
diff --git a/deno/mail-relay/cron.ts b/deno/mail-relay/cron.ts
new file mode 100644
index 0000000..bf0a0be
--- /dev/null
+++ b/deno/mail-relay/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;
+ }
+ }
+}