Skip to content

Commit ed60c4f

Browse files
jalmonterNuroDev
andauthored
fix(wrangler): show correct port in scheduled worker warning (#11494)
* fix(wrangler): show correct port in scheduled worker warning * test: warn about scheduled workers with the correct port * chore: add changeset * test: warn about scheduled workers with the correct port * test: warn about scheduled workers with the correct port * Fixed e2e tests worker hostnames * Fixed minor code formatting issue --------- Co-authored-by: Ben <4991309+NuroDev@users.noreply.github.com> Co-authored-by: Ben Dixon <ben@nuro.dev>
1 parent 5e89bd1 commit ed60c4f

File tree

5 files changed

+201
-17
lines changed

5 files changed

+201
-17
lines changed

.changeset/chubby-spoons-learn.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
"miniflare": patch
3+
"wrangler": patch
4+
---
5+
6+
Fix scheduled trigger warning showing `undefined` port
7+
8+
When running `wrangler dev` with a worker that has cron triggers, the warning message displayed an invalid URL like `curl "http://localhost:undefined/cdn-cgi/handler/scheduled"` because the port wasn't yet determined when the warning was logged.
9+
10+
Moved the warning to after the proxy server is fully ready, where the actual public URL and port are known.

packages/wrangler/e2e/dev.test.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,89 @@ describe.each([
234234
});
235235
});
236236

237+
describe(`scheduled worker warning with ${cmd}`, () => {
238+
it("shows warning with correct port when cron trigger is configured", async () => {
239+
const helper = new WranglerE2ETestHelper();
240+
await helper.seed({
241+
"wrangler.toml": dedent`
242+
name = "${workerName}"
243+
main = "src/index.ts"
244+
compatibility_date = "2023-01-01"
245+
246+
[triggers]
247+
crons = ["* * * * *"]
248+
`,
249+
"src/index.ts": dedent`
250+
export default {
251+
fetch(request) {
252+
return new Response("Hello World!")
253+
},
254+
scheduled(event) {
255+
console.log("Scheduled event triggered");
256+
}
257+
}`,
258+
"package.json": dedent`
259+
{
260+
"name": "worker",
261+
"version": "0.0.0",
262+
"private": true
263+
}
264+
`,
265+
});
266+
const worker = helper.runLongLived(cmd);
267+
268+
const { url } = await worker.waitForReady();
269+
const { hostname, port } = new URL(url);
270+
271+
// The warning should contain the actual port, not "undefined"
272+
expect(worker.currentOutput).toContain(
273+
"Scheduled Workers are not automatically triggered"
274+
);
275+
expect(worker.currentOutput).toContain(
276+
`curl "http://${hostname}:${port}/cdn-cgi/handler/scheduled"`
277+
);
278+
expect(worker.currentOutput).not.toContain("undefined");
279+
});
280+
281+
it("does not show warning when --test-scheduled is enabled", async () => {
282+
const helper = new WranglerE2ETestHelper();
283+
await helper.seed({
284+
"wrangler.toml": dedent`
285+
name = "${workerName}"
286+
main = "src/index.ts"
287+
compatibility_date = "2023-01-01"
288+
289+
[triggers]
290+
crons = ["* * * * *"]
291+
`,
292+
"src/index.ts": dedent`
293+
export default {
294+
fetch(request) {
295+
return new Response("Hello World!")
296+
},
297+
scheduled(event) {
298+
console.log("Scheduled event triggered");
299+
}
300+
}`,
301+
"package.json": dedent`
302+
{
303+
"name": "worker",
304+
"version": "0.0.0",
305+
"private": true
306+
}
307+
`,
308+
});
309+
const worker = helper.runLongLived(`${cmd} --test-scheduled`);
310+
311+
await worker.waitForReady();
312+
313+
// The warning should NOT appear when testScheduled is enabled
314+
expect(worker.currentOutput).not.toContain(
315+
"Scheduled Workers are not automatically triggered"
316+
);
317+
});
318+
});
319+
237320
describe("Workers + Assets", () => {
238321
it(`can modify User Worker during ${cmd}`, async () => {
239322
const helper = new WranglerE2ETestHelper();

packages/wrangler/e2e/multiworker-dev.test.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -606,4 +606,73 @@ describe("multiworker", () => {
606606
);
607607
});
608608
});
609+
610+
describe("scheduled worker warnings", () => {
611+
beforeEach(async () => {
612+
await baseSeed(a, {
613+
"wrangler.toml": dedent`
614+
name = "${workerName}"
615+
main = "src/index.ts"
616+
compatibility_date = "2024-11-01"
617+
618+
[triggers]
619+
crons = ["* * * * *"]
620+
621+
[[services]]
622+
binding = "BEE"
623+
service = '${workerName2}'
624+
`,
625+
"src/index.ts": dedent/* javascript */ `
626+
export default {
627+
async fetch(req, env) {
628+
return env.BEE.fetch(req);
629+
},
630+
scheduled(event) {
631+
console.log("Worker A scheduled event");
632+
}
633+
};
634+
`,
635+
});
636+
637+
await baseSeed(b, {
638+
"wrangler.toml": dedent`
639+
name = "${workerName2}"
640+
main = "src/index.ts"
641+
compatibility_date = "2024-11-01"
642+
643+
[triggers]
644+
crons = ["0 * * * *"]
645+
`,
646+
"src/index.ts": dedent/* javascript */ `
647+
export default {
648+
async fetch(req, env) {
649+
return new Response("hello world");
650+
},
651+
scheduled(event) {
652+
console.log("Worker B scheduled event");
653+
}
654+
};
655+
`,
656+
});
657+
});
658+
659+
it("shows warning with correct port when multiple workers have cron triggers", async () => {
660+
const worker = helper.runLongLived(
661+
`wrangler dev -c wrangler.toml -c ${b}/wrangler.toml`,
662+
{ cwd: a }
663+
);
664+
665+
const { url } = await worker.waitForReady(5_000);
666+
const { hostname, port } = new URL(url);
667+
668+
// The warning should contain the actual port, not "undefined"
669+
expect(worker.currentOutput).toContain(
670+
"Scheduled Workers are not automatically triggered"
671+
);
672+
expect(worker.currentOutput).toContain(
673+
`curl "http://${hostname}:${port}/cdn-cgi/handler/scheduled"`
674+
);
675+
expect(worker.currentOutput).not.toContain("undefined");
676+
});
677+
});
609678
});

packages/wrangler/src/dev/miniflare/index.ts

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -837,23 +837,6 @@ export async function buildMiniflareOptions(
837837
remoteProxyConnectionString: RemoteProxyConnectionString | undefined,
838838
onDevRegistryUpdate?: (registry: WorkerRegistry) => void
839839
): Promise<Options> {
840-
if (config.crons?.length && !config.testScheduled) {
841-
const host =
842-
config.initialIp === "0.0.0.0" || config.initialIp === "::"
843-
? "localhost"
844-
: config.initialIp.includes(":")
845-
? `[${config.initialIp}]`
846-
: config.initialIp;
847-
const port = config.initialPort;
848-
849-
logger.once.warn(
850-
`Scheduled Workers are not automatically triggered during local development.\n` +
851-
`To manually trigger a scheduled event, run:\n` +
852-
` curl "http://${host}:${port}/cdn-cgi/handler/scheduled"\n` +
853-
`For more details, see https://developers.cloudflare.com/workers/configuration/cron-triggers/#test-cron-triggers-locally`
854-
);
855-
}
856-
857840
const upstream =
858841
typeof config.localUpstream === "string"
859842
? `${config.upstreamProtocol}://${config.localUpstream}`

packages/wrangler/src/dev/start-dev.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,15 @@ export async function startDev(args: StartDevOptions) {
144144
})
145145
);
146146
}
147+
148+
// Print scheduled worker warning with the actual public URL
149+
const allDevEnvs = [primaryDevEnv, ...secondary];
150+
const hasCrons = allDevEnvs.some(
151+
(env) =>
152+
env.config.latestConfig?.triggers?.some((t) => t.type === "cron") ??
153+
false
154+
);
155+
maybePrintScheduledWorkerWarning(hasCrons, !!args.testScheduled, url);
147156
});
148157

149158
return {
@@ -347,3 +356,33 @@ function getResolvedSiteAssetPaths(
347356
args.siteExclude
348357
);
349358
}
359+
360+
/**
361+
* Logs a warning about scheduled workers not being automatically triggered
362+
* during local development. This should be called after the server is ready
363+
* and the actual URL is known.
364+
*/
365+
function maybePrintScheduledWorkerWarning(
366+
hasCrons: boolean,
367+
testScheduled: boolean,
368+
url: URL
369+
): void {
370+
if (!hasCrons || testScheduled) {
371+
return;
372+
}
373+
374+
const host =
375+
url.hostname === "0.0.0.0" || url.hostname === "::"
376+
? "localhost"
377+
: url.hostname.includes(":")
378+
? `[${url.hostname}]`
379+
: url.hostname;
380+
const port = url.port;
381+
382+
logger.once.warn(
383+
`Scheduled Workers are not automatically triggered during local development.\n` +
384+
`To manually trigger a scheduled event, run:\n` +
385+
` curl "http://${host}:${port}/cdn-cgi/handler/scheduled"\n` +
386+
`For more details, see https://developers.cloudflare.com/workers/configuration/cron-triggers/#test-cron-triggers-locally`
387+
);
388+
}

0 commit comments

Comments
 (0)