How can I add cron job to `wrangler.json` file?

I used svelitekit framework template and hosted to workers. I see this in my wrangler.json file:
"main": ".svelte-kit/cloudflare/_worker.js"
"main": ".svelte-kit/cloudflare/_worker.js"
How can I add cron job to that file? How can I extand that file to cron job?
17 Replies
Sithu Khant
Sithu KhantOP3w ago
where can I place this code?
interface Env {}
export default {
async scheduled(
controller: ScheduledController,
env: Env,
ctx: ExecutionContext,
) {
console.log("cron processed");
},
};
interface Env {}
export default {
async scheduled(
controller: ScheduledController,
env: Env,
ctx: ExecutionContext,
) {
console.log("cron processed");
},
};
Walshy
Walshy3w ago
Probably a question for SvelteKit people, I don't know if they allow setting things like cron triggers and from the original post, it seems they control the whole worker
Hello, I’m Allie!
I shim it in manually, but note that this only works with deployed Workers/wrangler dev It won’t run when using Svelte’s own Dev Process
Hello, I’m Allie!
GitHub
authrora/src/worker.ts at main · helloimalastair/authrora
Authentication and Authorization at the Edge. Contribute to helloimalastair/authrora development by creating an account on GitHub.
Sithu Khant
Sithu KhantOP3w ago
How about like this? I got this from gpt:
import worker from "./../../../../.svelte-kit/cloudflare/_worker.js";

// Merge SvelteKit's worker with our cron handler
export default {
...worker,
async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) {
ctx.waitUntil(handleCronJob(event));
}
};

async function handleCronJob(event: ScheduledEvent) {
console.log(`Cron ${event.cron} triggered at ${new Date().toISOString()}`);
// Add your cron logic here
}
import worker from "./../../../../.svelte-kit/cloudflare/_worker.js";

// Merge SvelteKit's worker with our cron handler
export default {
...worker,
async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) {
ctx.waitUntil(handleCronJob(event));
}
};

async function handleCronJob(event: ScheduledEvent) {
console.log(`Cron ${event.cron} triggered at ${new Date().toISOString()}`);
// Add your cron logic here
}
Hello, I’m Allie!
That looks like it might work? Then you just need to point your main towards that, instead of the sveltekit output
Sithu Khant
Sithu KhantOP3w ago
Is this working?
Hello, I’m Allie!
Yes, I have this deployed right now
Sithu Khant
Sithu KhantOP3w ago
Then I might use that method @HardlyWorkin', hey just a suggestion, this method works too:
import worker from "./../../../../.svelte-kit/cloudflare/_worker.js";

interface Env {
DB: D1Database;
}

export default {
...worker,
async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) {
ctx.waitUntil(handleCronJob(event)); // Pass env if needed
}
};

async function handleCronJob(event: ScheduledEvent) {
const cron = event.cron;
const time = new Date(event.scheduledTime).toISOString();

console.log(`Cron ${cron} triggered at ${time}`);

switch (cron) {
case "* * * * *":
await minuteJob();
break;
case "0 * * * *":
await hourlyJob();
break;
case "0 0 * * *":
await dailyJob();
break;
default:
console.warn("Unknown cron pattern:", cron);
}
}

// Example jobs
async function minuteJob() {
// Every minute logic
console.log("Minute job...");
}

async function hourlyJob() {
// Hourly logic (at :00)
}

async function dailyJob() {
// Daily midnight logic
}
import worker from "./../../../../.svelte-kit/cloudflare/_worker.js";

interface Env {
DB: D1Database;
}

export default {
...worker,
async scheduled(event: ScheduledEvent, env: Env, ctx: ExecutionContext) {
ctx.waitUntil(handleCronJob(event)); // Pass env if needed
}
};

async function handleCronJob(event: ScheduledEvent) {
const cron = event.cron;
const time = new Date(event.scheduledTime).toISOString();

console.log(`Cron ${cron} triggered at ${time}`);

switch (cron) {
case "* * * * *":
await minuteJob();
break;
case "0 * * * *":
await hourlyJob();
break;
case "0 0 * * *":
await dailyJob();
break;
default:
console.warn("Unknown cron pattern:", cron);
}
}

// Example jobs
async function minuteJob() {
// Every minute logic
console.log("Minute job...");
}

async function hourlyJob() {
// Hourly logic (at :00)
}

async function dailyJob() {
// Daily midnight logic
}
Sithu Khant
Sithu KhantOP3w ago
after multiple trial failed, it is working now 🎉
No description
Sithu Khant
Sithu KhantOP3w ago
@Hello, I’m Allie! you are the svelte guy, right? Do you know how to setup trpc?
Hello, I’m Allie!
I mean, I do use Svelte in my own projects, but I wouldn’t say I’m an expert at it. As for tRPC, I’m afraid I haven’t used it before, so I wouldn’t know any more about setting it up than you
Sithu Khant
Sithu KhantOP3w ago
it is oaky, I mean I looked up the project you provided, you seems have experiences in Svelte a lot
Hello, I’m Allie!
Not really, other than a few sandbox projects with 1-2 files, this was actually my first larger project with Svelte(Kit). But thank you for the vote of confidence! Actually, nvm, WDOL is also written in SvelteKit… point still stands though, I’m definitely no expert!
Sithu Khant
Sithu KhantOP3w ago
GitHub
GitHub - ottomated/create-o7-app: An opinionated CLI for creating t...
An opinionated CLI for creating type-safe Svelte apps. - ottomated/create-o7-app

Did you find this page helpful?