Cron Triggers with modules

Is there any way to run cron triggers while also using modules with durable objects?

There doesn’t seem to be any documentation or existing threads about this.

Does anyone have any information about this? or would I be better off looking for a different solution?

You certainly can. It’s similar to module-based fetch, just a different method to export. You can do both fetch and scheduled in the same worker too.

export default {

    async scheduled(event: ModuleWorkerScheduledEvent, env: YourWorkerEnv, ctx: ModuleWorkerContext): Promise<void> {
        await doYourThing();
    }

};

export interface ModuleWorkerScheduledEvent {
    /** The time the ScheduledEvent was scheduled to be executed in milliseconds since January 1, 1970, UTC. 
     * 
     * It can be parsed as new Date(event.scheduledTime) */
     readonly scheduledTime: number;

     /** The original cron string that the event was scheduled for. */
     readonly cron: string;
}


export interface ModuleWorkerContext {
    /** Prevents requests from failing due to an unhandled exception thrown by the Worker, causing it instead to “fail open”. 
     * 
     * Instead of returning an error response, the runtime will proxy the request to the origin server as though the Worker was never invoked. */
     passThroughOnException(): void;

     /** Extend the lifetime of the event without blocking the response from being sent. 
      * 
      * Use this method to notify the runtime to wait for tasks (e.g. logging, analytics to third-party services, streaming and caching) 
      * that need to run longer than the usual time it takes to send a response. */
     waitUntil(promise: Promise<unknown>): void;
}

Wow, perfect!

Can you link the documentation for this?

Honestly, I don’t think it’s available online anywhere currently, which is why I included those context types for reference in my comment above.

The best way to stay up-to-date on the current production runtime environment using module workers and DO is to join the Discord.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.