How to monitor Google Cloud Scheduler jobs
A scheduler HTTP response may arrive before asynchronous work finishes. Ping only after the actual work.
Report success and failure
Create a check and set PING_URL to its private URL. Replace the example command with your actual task. These examples preserve command failure and do not let a failed reporting request turn a successful job into a failure.
// Modern Node.js / Workers fetch. Supply runJob with your actual work.
async function monitored(runJob, pingUrl = process.env.PING_URL) {
if (!pingUrl) throw new Error('Set PING_URL');
const rid = crypto.randomUUID();
const ping = async status => {
for (let n = 0; n < 4; n++) {
try {
const res = await fetch(`${pingUrl}/${status}?rid=${rid}`, {
signal: AbortSignal.timeout(10000)
});
if (res.ok) return;
} catch {}
}
console.error('Heartbeat delivery failed');
};
await ping('start');
await new Promise(resolve => setTimeout(resolve, 1100));
try { await runJob(); }
catch (error) { await ping('1'); throw error; }
await ping('0');
}
Connect it to Google Cloud Scheduler
Use this body in your authenticated HTTP function.
await monitored(runJob);
res.status(200).send("done");These are integration fragments for an existing project, not a full application. Set the URL in the job’s environment, keep credentials private and ensure the runtime has the dependencies shown.
Things to watch
The shell examples wait one second between start and work to respect the per-check rate guard. For very short tasks, completion-only pings avoid that delay. A killed process may never send a failure ping; the completion deadline catches the missing run instead.
Test the setup
- Run a successful task and confirm the check turns up.
- Force a nonzero exit or exception and verify the check turns down.
- Stop the scheduler after one success and wait for the expected run plus grace.
Set the schedule to your task’s completion cadence, allowing for runtime and queue delay. Use the cron helper to review your calendar schedule.
Questions about Google Cloud Scheduler
Where should the heartbeat run?
Which grace period should I use?
Does this test the result of my work?
Platform reference: official Google Cloud Scheduler documentation. Adapt fragments to your installed version.