@omar#4289 same as, I am getting the

@omar same as, I am getting the same issue, I have explained my issue
No description
HB
Harshil Bodara202d ago
the first time, it is working correctly, when re-call the water Schedule a second time does the calling schedule 2 times every 2 seconds, same as the next
O
omar4289202d ago
What is the code in the worker ? Do you always refer to the same do instance ? @Harshil Bodara
HB
Harshil Bodara202d ago
But whenever I call the schedule, I want to call the alarm only once export class Water { 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() { const alarmTime = await this.storage.get("alarmTime"); console.log("alarmTime",alarmTime) if (!alarmTime) { await this.scheduleAlarm(); } else { return new Response("Water alarm is already scheduled."); } return new Response("Water schedule Successfully!"); }
async alarm() { console.log("Water Alarm Doing"); await this.scheduleAlarm(); }
async scheduleAlarm() { let scheduledTime: number = Date.now() + this.doEverySeconds * 1000; await this.storage.put("alarmTime", scheduledTime.toString()); this.storage.setAlarm(scheduledTime); } }
HB
Harshil Bodara202d ago
No description
O
omar4289202d ago
Ah Ok, I get your point, basically what you want to do is execute your alarm call once in 2 seconds, regardless of how many requests you receive within the 2 seconds range, right ?
HB
Harshil Bodara202d ago
yes, exactly
O
omar4289202d ago
ok, let me send you the code
HB
Harshil Bodara202d ago
for exmaple alaram calling First time call = schudle call every 2 second (working fine) second time call = 2 time schudle call every 2 second third time call = 3 time schudle call every 2 second but i want to do ------------------------------------- First time call = schudle call every 2 second (working fine) second time call = 1 time schudle call every 2 second third time call = 1 time schudle call every 2 second
HB
Harshil Bodara202d ago
No description
O
omar4289202d ago
export class Water { 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() { const alarmTime = await this.storage.get("alarmTime"); console.log("alarmTime",alarmTime) if (!alarmTime) { await this.scheduleAlarm(); } else { return new Response("Water alarm is already scheduled."); } return new Response("Water schedule Successfully!"); }
async alarm() { console.log("Water Alarm Doing"); await this.storage.delete("alarmTime"); }
async scheduleAlarm() { let scheduledTime: number = Date.now() + this.doEverySeconds * 1000; await this.storage.put("alarmTime", scheduledTime.toString()); this.storage.setAlarm(scheduledTime); } } Just replaced the this.scheduleAlarm in the alarm function to await storage.delete
HB
Harshil Bodara202d ago
when i call the API, so alaram calling is only one time
No description
HB
Harshil Bodara202d ago
not every 2 secondse
O
omar4289202d ago
This will be the outcome: Call to DO at t + 1s: Will schedule the alarm Call to DO at t + 1.5s: Will return alarm already scheduled (therefore would do nothing) Call to DO at t + 2s: WIll return alarm already scheduled (therefore would do nothing) At t + 3s: The alarm will be executed At t + 5s: Nothing will happen, until the DO schedules the alarm again through the fetch Is this what you are trying to build ? Based on what I have provided, do you want the alarm to be executed still at t + 5s, t + 7s, .... t + 99s ?
HB
Harshil Bodara202d ago
yes, like this
O
omar4289202d ago
Can you send the code where you call your DO from the worker ? When you get the id and then send the fetch request to the DO
HB
Harshil Bodara202d ago
but I want to when re-calling the schedule so before, schedule is clear(restart) and then the alarm calling
O
omar4289202d ago
Ok, can you express it in a timeline like I did here From t + 1s, to t + 9s
HB
Harshil Bodara202d ago
main.ts -------------------------- import { ThrowableRouter } from 'itty-router-extras'; import { Env } from '../types'; import { withDurables } from 'itty-durable'; export { Water } from './crons/water'; const router = ThrowableRouter(); router.all("", withDurables()); router.get('/water', async (request: Request, env: Env) => { const doId = env.WATER.newUniqueId(); const WaterInstance = env.WATER.get(doId); return WaterInstance.fetch(request, env); }); router.all("", ()=> { return new Response("Route not Found!") }); export default { fetch: router.handle, }; crons/water/index.ts --------------------------------- export class Water { 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() { const alarmTime = await this.storage.get("alarmTime"); console.log("alarmTime",alarmTime) if (!alarmTime) { await this.scheduleAlarm(); } else { return new Response("Water alarm is already scheduled."); } return new Response("Water schedule Successfully!"); } async alarm() { console.log("Water Alarm Doing"); await this.storage.delete("alarmTime"); } async scheduleAlarm() { let scheduledTime: number = Date.now() + this.doEverySeconds * 1000; await this.storage.put("alarmTime", scheduledTime.toString()); this.storage.setAlarm(scheduledTime); } } for example:- first-time call schedule: alarm calling 2s: alarm calling 4s: alarm calling 6s: alarm calling when I recall the API, it is already scheduled already doing 8s: alarm calling 10s: alarm calling continue OR first time call schudle: alarm calling 2s: alarm calling 4s: alarm calling 6s: alarm calling
O
omar4289202d ago
Ok, I get it So, here the new code 1. const doId = env.WATER.newUniqueId(); must be replaced with env.WATER.idFromName('water_instance'). The reason is that you have different DO instance per call if you use newUniqueId(). Different instances means, different storage, they are not synced together .... 2. async alarm() { console.log("Water Alarm Doing"); await this.scheduleAlarm(); }
HB
Harshil Bodara202d ago
he said, water schdule already working so how may i stop and again run?
No description
No description
O
omar4289202d ago
Ok, if you want to manage the state of your waterfall, here is a better solution
HB
Harshil Bodara202d ago
yes, let me know
O
omar4289202d ago
@Harshil Bodara
router.get('/water/start', async (request: Request, env: Env) => {
const doId = env.WATER.idFromName('water_instance');
const WaterInstance = env.WATER.get(doId);
return WaterInstance.fetch(request);
});

router.get('/water/stop', async (request: Request, env: Env) => {
const doId = env.WATER.idFromName('water_instance');
const WaterInstance = env.WATER.get(doId);
return WaterInstance.fetch(request);
});
router.get('/water/start', async (request: Request, env: Env) => {
const doId = env.WATER.idFromName('water_instance');
const WaterInstance = env.WATER.get(doId);
return WaterInstance.fetch(request);
});

router.get('/water/stop', async (request: Request, env: Env) => {
const doId = env.WATER.idFromName('water_instance');
const WaterInstance = env.WATER.get(doId);
return WaterInstance.fetch(request);
});
export class Water {
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(request: Request) {
const url = new URL(request.url);
const action = url.pathname.split('/')[2];

switch (action) {
case 'start':
const alarmTime = await this.storage.get('alarmTime');
if (!alarmTime) {
await this.scheduleAlarm();
return new Response('Water schedule Successfully!');
} else {
return new Response('Water alarm is already scheduled.');
}
case 'stop':
await this.storage.deleteAlarm();
await this.storage.delete('alarmTime');
return new Response("Water alarm stopped");
default:
return new Response('', { status: 404 });
}
}

async alarm() {
console.log('Water Alarm Doing');
await this.scheduleAlarm();
}

async scheduleAlarm() {
let scheduledTime: number = Date.now() + this.doEverySeconds * 1000;
await this.storage.put('alarmTime', scheduledTime.toString());
await this.storage.setAlarm(scheduledTime);
}
}
export class Water {
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(request: Request) {
const url = new URL(request.url);
const action = url.pathname.split('/')[2];

switch (action) {
case 'start':
const alarmTime = await this.storage.get('alarmTime');
if (!alarmTime) {
await this.scheduleAlarm();
return new Response('Water schedule Successfully!');
} else {
return new Response('Water alarm is already scheduled.');
}
case 'stop':
await this.storage.deleteAlarm();
await this.storage.delete('alarmTime');
return new Response("Water alarm stopped");
default:
return new Response('', { status: 404 });
}
}

async alarm() {
console.log('Water Alarm Doing');
await this.scheduleAlarm();
}

async scheduleAlarm() {
let scheduledTime: number = Date.now() + this.doEverySeconds * 1000;
await this.storage.put('alarmTime', scheduledTime.toString());
await this.storage.setAlarm(scheduledTime);
}
}
HB
Harshil Bodara202d ago
Thank you, @omar , It was a very exciting good solution now schedule is working fine
O
omar4289202d ago
You are welcome You should still add more control in case your alarm did stop - For example, on run, if the alarm did not execute since 60minutes, then just rerun it instead of returning already scheduled
HB
Harshil Bodara202d ago
Thank you So much! @omar yes, i got it
Want results from more Discord servers?
Add your server
More Posts
520 ERROR (Important!)Hi. When I use postman, I it works fine, but when I try the exact same configuration with code, I geShow worker page on going to custom domainHey guys, I have a worker at https://test.workers.dev/page. I also have a custom domain https://domLoading page slower and slower after repeatedly disable cache on browserWhy Loading page slower and slower after repeatedly disable cache on browser ( cf-cache = HIT)? And slow speeds on warp+With warp my speeds reduce to 40 mbps and without warp it's 300mbps. Is there anything I can do to sDomain issueSo I just bought a domain and for some reason its filled with DNS I didn't addCan I access a relative path outside build root directory to other repo files?Our repo looks like this: ``` <root>/ site/ src/ <other files and folders> ``` We are using Custom Domains for Pages (saas for custom domains)Need some guidance with Pages custom domains . I would to offer my saas customers the ability to addwoocommerceI want to use cache everything for my site but I don’t want my woocommerce store to mess up. Can I Unable to setup LogPush for WorkersAttempting to setup logpush for a worker and getting a 404 when trying to access .../logs I am on tDNS timing outI have entered the IP of my server to Cloudflare, but it is not working. I am just getting a timed oPages build doesn't seem to be taking environment var changesframework: NuxtJS Build system: v1 I have a pages project that uses a nuxt build module to exlude paHow can I share my cloudflare account with other developers?Hello I have created an account and website/service/etc for my startup. So far so good, but since tHow can I get to do other than systematic failure when Cloudfare checks that you are a human becauseHow can I get to do other than systematic failure when Cloudfare checks that you are a human becauseHow can I get to do other than systematic failure when Cloudfare checks that you are a human becauseHow can I get to do other than systematic failure when Cloudfare checks that you are a human becauseGetting Error While Using Python in Workersi cloned https://github.com/cloudflare/python-worker-hello-world and installed all prerequisite ERROGetting error while using python worker hello world repoHello world, i am trying to use python in workers but after cloning official https://github.com/clouWarp Posture Checks with iOSI'm setting up a Gateway policy which checks compliance in Azure / Intune, and I can't seem to get tStill having issues with custom domain in PagesCame here from Cloudflare Community. Have tried the documentation in troubleshooting. Attached the sCloudflare tunnels config fileCloudflare tunnels config fileDNS not resolving for one of my domains: najemi.czI have around 40 domains with the same registrar and using cloudflare DNS, all seem to be working ex