Fetch inside a scheduled Cron Job
Hello,
I have a worker that I want to use to ping another worker endpoint every day.
When I run the
dev
server it works great but when I deploy I get 404.
Here's an example code block:
Am I not using this right? I looked into ctx.waitUntil but that didn't help either.
Thank you!3 Replies
I've read about calling one worker from another using service binding but the service that I'm trying to call is using honojs as the api and I'm trying to call a specific endpoint in there. I'm thoroughly confused.
Right now your code has the scheduled job and the API in the same worker. Is that what you want? I never done it, but you should be able to ping the API handler from the scheduled job using the Worker URL & fetch (maybe there's another way, IDK).
You'd want your Worker to look like this:
Source: https://hono.dev/docs/getting-started/cloudflare-workers#using-hono-with-other-event-handlers
You can also do this with two Workers. The concept would be the same (scheduled job sends a fetch request to the API Worker URL). I'm about to log off, but I think this will put you in the right direction.
Cloudflare Workers - Hono
Web framework built on Web Standards for Cloudflare Workers, Fastly Compute, Deno, Bun, Vercel, Node.js, and others. Fast, but not only fast.
THanks @Shane I tried something similar to that but the fetch function inside the scheduled handler kept erroring so I thought I move the cron job outside the worker