kwhitley / itty-durable

Cloudflare Durable Objects + Itty Router = shorter code
MIT License
236 stars 18 forks source link

the alarm doesn't work properly, The number of times a request call is made, the number of times an alarm is call , it should be calls one time every 2 seconds #43

Open harshil-bodara opened 8 months ago

harshil-bodara commented 8 months ago

I am facing an issue with the alarm

every time, it should call at one time every 2 seconds

export class Schedule { id: string | DurableObjectId; storage: DurableObjectStorage; doEverySeconds: number; env: Env;

constructor(state: DurableObjectState, env: Env) {
    this.storage = state.storage;
    this.id = state.id;
    this.doEverySeconds = 2;
    this.env = env;
}

async fetch() {
    this.scheduleAlarm();
    return new Response("schedule Successfully!");
}

async alarm() {
    console.log("Schedule Alaram Doing");
    this.scheduleAlarm();
}

async scheduleAlarm() {
    let scheduledTime: number = Date.now();
    scheduledTime += this.doEverySeconds * 1000;
    this.storage.setAlarm(scheduledTime);
}

}