From 72800db7e71bbd696eebc75bd128c46546f53c33 Mon Sep 17 00:00:00 2001 From: Yuqian Yang Date: Thu, 10 Apr 2025 15:12:46 +0800 Subject: feat(mail-server): done (except aws message-id map). --- services/docker/mail-server/relay/cron.ts | 43 +++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 services/docker/mail-server/relay/cron.ts (limited to 'services/docker/mail-server/relay/cron.ts') diff --git a/services/docker/mail-server/relay/cron.ts b/services/docker/mail-server/relay/cron.ts new file mode 100644 index 0000000..bf0a0be --- /dev/null +++ b/services/docker/mail-server/relay/cron.ts @@ -0,0 +1,43 @@ +export type CronCallback = (task: CronTask) => Promise; + +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; + } + } +} -- cgit v1.2.3