Prism/developer documentation/Clocks

Clocks

Run a task on a schedule.

A clock runs run() on a cron schedule. Put clocks in src/clocks/.

modules/example/src/clocks/cleanup.js
import { Clocks } from "#prism/modules";

export class CleanupClock extends Clocks {
    constructor(prism) {
        super(prism, { name: "cleanup", schedule: "0 0 * * *" });
    }

    async run() {
        const removed = await this.prism.modules.handlers.get(this.module, "items").purge();
        if (removed > 0) this.prism.console.log(this.prism.console.levels.info, `Removed ${removed} old items.`);
    }
}
KeyWhat it does
nameThe clock name.
scheduleA 5-field cron expression: minute, hour, day, month, weekday.
ScheduleRuns
* * * * *Every minute.
*/10 * * * *Every 10 minutes.
0 * * * *Every hour.
0 0 * * *Every day at midnight.
0 0 * * 0Every Sunday at midnight.

A run never overlaps the one before it. Errors are logged and the clock keeps its schedule. Clocks start once the module loads and stop when Prism stops.

For an interval the admin picks, run every minute and check the config. The Kora module syncs products this way:

async run() {
    if (Date.now() - this.last < this.prism.modules.config.value("example", "settings", "sync_every") * 60000) return;
    this.last = Date.now();
    await this.prism.modules.handlers.get("example", "items").sync();
}