Issue with scheduled code that runs ok locally, not production

For Workes & Pages, what is the name of the domain?

raymondcamden.workers.dev

What is the issue or error you’re encountering

I have a scheduled task worker that does a fetch call to an API and then does some work with the results. This is all running fine locally. However, in production, when I make a fetch call to the remote API, I get: “error code: 521”. To be clear, the same fetch() call is run in dev versus production. On a whim, I then built a whole new Worker, HTTP based, copied and pasted the function that makes the fetch() call, and it works fine.

What steps have you taken to resolve the issue?

Lots of debugging. :wink:

This is the ‘scratch’ worker I built to just test via HTTP, and it works:

Source: cloudflareworkers-demos/scratchworker/src/index.js at main · cfjedimaster/cloudflareworkers-demos · GitHub
Live URL: https://scratchworker.raymondcamden.workers.dev/

This is the source of the scheduled task version: cloudflareworkers-demos/getincidents/src/index.js at main · cfjedimaster/cloudflareworkers-demos · GitHub

Still looking for any help here, please. As a test, I built another worker that just runs via HTTP. As I mentioned already, that works fine. I then built a new scheduled worker that used a service binding to call it. This too ends up with a 521 error.

Hi! We looked into this.

It appears the web site you’re trying to reach refuses traffic from outside the US, and your cron trigger was running in ZRH (Switzerland).

We run cron triggers in off-peak colos to save energy; unfortunately you cannot control this. However, you could create a Durable Object in the US, and have your request proxy through it. (Or use alarms in the DO in place of cron triggers.)

I know you said you determined that the IPs were the same. Most likely this was an illusion because the API you hit to get your IP was also on Cloudflare, so no external IP was used at all. It’s actually rare to see multiple requests from Cloudflare use the same IP, as your worker doesn’t run in any single place in our network and even individual requests from the same worker invocation are often routed through different machines on the way out of our network.

2 Likes

Thank you SO much! That makes perfect sense. I’ll take a look at Durable Objects.