aboutsummaryrefslogtreecommitdiff
path: root/services/docker/mail-server/relay/cron.ts
diff options
context:
space:
mode:
authorYuqian Yang <crupest@crupest.life>2025-04-10 15:12:46 +0800
committercrupest <crupest@outlook.com>2025-06-02 18:04:47 +0800
commitdac1e7b63cdffe54aa70c50c4d2d838532e3a5b8 (patch)
tree07ffa4bfc6f6442e747b65e229b2abbb366fbc67 /services/docker/mail-server/relay/cron.ts
parentcbeeaf281f400230e4161f08cfef2fe4715f3773 (diff)
downloadcrupest-dac1e7b63cdffe54aa70c50c4d2d838532e3a5b8.tar.gz
crupest-dac1e7b63cdffe54aa70c50c4d2d838532e3a5b8.tar.bz2
crupest-dac1e7b63cdffe54aa70c50c4d2d838532e3a5b8.zip
feat(mail-server): done (except aws message-id map).
Diffstat (limited to 'services/docker/mail-server/relay/cron.ts')
-rw-r--r--services/docker/mail-server/relay/cron.ts43
1 files changed, 43 insertions, 0 deletions
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<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;
+ }
+ }
+}