Closed evrys closed 2 years ago
Is there a reason sending emails needs to be the responsibility of your SvelteKit app, rather than a separate dedicated worker?
It uses a bunch of code and environment from the svelte-kit app to send the emails, since it needs to iterate over the users in the database and determine if they're ready to receive a reminder email.
But you're right-- the Cloudflare Discord helped me work out a solution using a second worker for the Cron Trigger part that calls a /heartbeat
endpoint on the svelte-kit app through Service Bindings. It's a little janky because Service Bindings were just released the other day, but it works!
Excellent — will close this as it's not expected that addEventListener('scheduled', ...)
will work, and if we wanted it to then we'd need to explicitly design support for it
This is a bit hacky, but here's what I did to get a cron working within the same Svelte Kit + Cloudflare Workers adapter app. (mostly because it's really convenient to keep everything as a single worker 🙂)
[...]
main = "./.cloudflare/worker.mjs"
[...]
[...]
"scripts": {
[...]
"postbuild": "cat src/lib/cron.js >> .cloudflare/worker.mjs"
}
[...]
entry_default.scheduled = async (event, env, ctx) => {
ctx.waitUntil(cron(env));
};
// Cron logic
async function cron(env) {
console.log("Starting cron");
// ...
}
(of course this only works if cron.js
doesn't need to import anything, otherwise it would need its own build step)
Here's my revised solution based on @robertaboukhalil's
// $lib/cron.js
/**
* @param {import("@cloudflare/workers-types").ScheduledEvent} event
* @param {Env} env
* @param {import('@cloudflare/workers-types').EventContext<Env, "", {}>} ctx
*/
entry_default.scheduled = async (event, env, ctx) => {
// ... do stuff here
};
// append.js
import { appendFile, readFile } from 'fs/promises';
const file = await readFile('src/lib/cron.js', 'utf8');
await appendFile('.cloudflare/worker.js', file, 'utf8')
"build": "vite build && node append.js",
Note that if you're planning on fetch-ing existing endpoints in your app, you don't have SvelteKit's fetch function, so you'll get a 1042 error. You can get around this by adding a service binding that points to itself:
# wrangler.toml
[[services]]
binding = "SELF"
service = "worker_name_here"
entry_default.scheduled = async (event, env, ctx) => {
await env.SELF.fetch('...');
};
Describe the problem
I notice the CF Workers adapter now uses the module worker format for output. That's great! But now I'm wondering, how can I have sveltekit output the default export needed to handle scheduled events on CF? I need it for sending scheduled emails. https://developers.cloudflare.com/workers/runtime-apis/scheduled-event/#syntax-module-worker
Describe the proposed solution
Documentation on how to use the Cloudflare Workers scheduling feature with a SvelteKit project
Alternatives considered
In the version before the adapter used module workers, it was possible to just put this in
hooks.ts
:However this is no longer the syntax for the module format.
Importance
nice to have
Additional Information
No response