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:
import { Env, Hono } from "hono";

const app = new Hono();

app.get("/", (c) => {
return c.text("Hello Hono!");
});

export default {
async scheduled(
event: ScheduledEvent,
env: Env,
ctx: ExecutionContext
): Promise<void> {
console.log("Scheduled Event Triggered at:", new Date().toISOString());

const response = await fetch("example.worker.dev/my-endpoint");
},

async fetch(request: Request, env: Env) {
return await app.fetch(request, env);
},
};
import { Env, Hono } from "hono";

const app = new Hono();

app.get("/", (c) => {
return c.text("Hello Hono!");
});

export default {
async scheduled(
event: ScheduledEvent,
env: Env,
ctx: ExecutionContext
): Promise<void> {
console.log("Scheduled Event Triggered at:", new Date().toISOString());

const response = await fetch("example.worker.dev/my-endpoint");
},

async fetch(request: Request, env: Env) {
return await app.fetch(request, env);
},
};
Am I not using this right? I looked into ctx.waitUntil but that didn't help either. Thank you!
3 Replies
Web Bae
Web BaeOP2mo ago
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.
Cloudflare Docs
Service bindings | Cloudflare Workers docs
Facilitate Worker-to-Worker communication.
Shane
Shane2mo ago
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:
import { Hono } from 'hono'

type Bindings = {
//
}

const app = new Hono<{Bindings: Bindings}>()

// Routes

export default {
fetch: app.fetch,
scheduled: async (event: ScheduledEvent, env: Bindings) => {
//
}
}
import { Hono } from 'hono'

type Bindings = {
//
}

const app = new Hono<{Bindings: Bindings}>()

// Routes

export default {
fetch: app.fetch,
scheduled: async (event: ScheduledEvent, env: Bindings) => {
//
}
}
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.
Web Bae
Web BaeOP2mo ago
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
Want results from more Discord servers?
Add your server