idkwhoiam322
idkwhoiam322
CDCloudflare Developers
Created by idkwhoiam322 on 6/6/2024 in #workers-help
Worker doesn't show any logs/Cron job fails when code is good
As strange as the title is, that's exactly what my issue is. My code is a simple discord tool and it's been running fine but suddenly decides to not do anything. I noticed it with when i was trying to implement a new feature and the code was broken and I saw the Exception logs - as expected, but when I fixed it, there would no longer be any logs, Real Time Logs page would just keep spinning on "Websocket connection established. Listening for events..." and that's it. Cron events page would show show the cron job erroring out but no logging is available.
wrangler tail
wrangler tail
doesn't return anything either. If I intentionally break the code again, I get the logs and everything, what's up with this exactly? At the moment, I've minimised the code to simply:
addEventListener("scheduled", (event) => {
event.waitUntil(handleScheduled(event));
});
async function handleScheduled(event) {
const originalCurrentTime = new Date(event.scheduledTime);
await sendToDiscord(DEBUG_CHANNEL, `Last triggered at: ${currentTime}`);
}

async function sendToDiscord(webhookUrl, message) {
// Send the message and get the response
// If the response is not 200, log the error
const response = await fetch(webhookUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ content: message }),
});

// Check if the response status is in the 200-204 range
if (response.status >= 200 && response.status <= 204) {
console.log("Message sent successfully to Discord.");
} else {
// If the response status is not in the success range, log the error
console.error(
`Error sending message to Discord. Status: ${response.status}, Message: ${message}`
);
// Also log the response body for further investigation
const responseBody = await response.text();
console.error(`Response Body: ${responseBody}`);
}
}
addEventListener("scheduled", (event) => {
event.waitUntil(handleScheduled(event));
});
async function handleScheduled(event) {
const originalCurrentTime = new Date(event.scheduledTime);
await sendToDiscord(DEBUG_CHANNEL, `Last triggered at: ${currentTime}`);
}

async function sendToDiscord(webhookUrl, message) {
// Send the message and get the response
// If the response is not 200, log the error
const response = await fetch(webhookUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ content: message }),
});

// Check if the response status is in the 200-204 range
if (response.status >= 200 && response.status <= 204) {
console.log("Message sent successfully to Discord.");
} else {
// If the response status is not in the success range, log the error
console.error(
`Error sending message to Discord. Status: ${response.status}, Message: ${message}`
);
// Also log the response body for further investigation
const responseBody = await response.text();
console.error(`Response Body: ${responseBody}`);
}
}
DEBUG_CHANNEL is just a webhook URL for test purposes. Am I missing something?
4 replies