Background jobs in Solid Start
Is there a way to run background jobs in Solid Start?
Any server entry point can suffice since I can just set it up via node-cron or similar libraries.
31 Replies
I think for background tasks in general, if the context is isolated, consider using worker thread to do the thing, otherwise check for something like bullmq
@TaQuanMinhLong Do you mean the OS cron system or what do you mean by using the worker thread?
I mean js is single threaded, so it's better to have another thread running the cron job instead if possible
Yeah yeah I know but in this context it's a low traffic website localized to a specific timezone so I can just run small tasks during the night. Just wondering if there's an entrypoint for this in SolidJS. It's also helpful to run other initialisation code that I usually run with this project.
:Worry_Think: I think every framework should have some sort of entry point
word
@Atila :Worry_DontKnow:
Tasks - Nitro
Nitro tasks allow on-off operations in runtime.
I think this should be helpful
Thank you very much kind stranger π
:Worry_OK:
@Brendonovich and I just had a look and though we haven't tried it, everything suggests this would work:
Ah so the server object there is nitro's config?
pretty much
Nice thanks π
if you end up trying this out before me, please create an issue on the docs so we know we need to add it. I think it's an important feature to point out. I'll try it as soon as I can too π
Apparently the task needs to be defined as a Nitro task with its own syntax, e.g.:
in a
tasks
folder.
I'm trying to figure out where the defineTask
comes from though as TypeScript is complaining that it doesn't exist.you'll probably need to install nitro as a direct dependency if Vinxi doesn't re-export
defineTask
for you.
(lots of these methods are reexported from Nitro or h3 just to make things more ergonomic - but you're now at the cutting-edge)
ping @nksaraf , maybe that's something we'd want to do for Vinxi / :start: too?Alright I can confirm I was able to make it work by adding this in
app.config.ts
then creating a file in tasks/cron.ts
(notice that tasks being in tasks/*
is mandatory for Nitro; also notice this is NOT src/tasks
).
Then we need to install nitro:
and finally we can define the task in cron.ts
as:
(I'm not actually running DB migrations in a task, don't worry, it's just an example π
)
As per documentation this is only supported in dev, node-server, bun and deno-server presets.damn that's pretty cool that it works
But I'm using
dev
and node-server
so that's fine
But yeah I guess the only thing missing is a re-export of defineTask
from Vinxi and solidjs start. That would make this is a breeze π
Is there a repo specifically for documentation or should I just create an issue in the main repo?Yeah I have sent this ApI I was more reluctant to make it part of the core right now since it didn't really work with the popular providers like vercel netlify cloudflare etc
for :start: docs => https://github.com/solidjs/solid-docs/issues
for Vinxi docs => https://github.com/nksaraf/vinxi
gotcha! I'll make sure we have that in the SolidStart docs when we mention it
Opened a ticket here: https://github.com/solidjs/solid-docs/issues/964#issue-2690613253
GitHub
[Other]: Tasks Β· Issue #964 Β· solidjs/solid-docs
π Explain your issue Reference Discord conversation: https://discord.com/channels/722131463138705510/1310190736670457856/1310190736670457856 I was able to make background jobs/tasks work in Solid S...
Question about server functions. Are they safe in the current model? For example, you deploy an app and a function is turned into server function
x-server-id: c_10m2j52#$$function0
and x-server-instance: server-fn:32
... but the hash is from the file name, and the function number is from the ordinal in the file, so what happens if you insert a function in the file, causing the ordinals to shift, but a client is still running and is out of date with the server calling the wrong ordinal? What is the protection across deployments here?There isnβt protection for this yet, itβd require something like skew protection
any suggestions for us being able to read a deployment is different? we aren't versioning the package.json in every deployment and not every deployment needs a user to refresh their browser. but we will need a way to know that the server function hashes have changed overall to cause a client refresh.
You might be able to read a git hash from env? Not sure whatβs available by default but you could add it yourself
too granular, ok, I'll maybe do a hash at buildtime from the output of the server functions or something. will think on it.
You might be able to get a hash from getServerFunctionMeta, not sure if that exists yet
don't think it does yet. symbol search doesn't find it in current. Will get back to this in a week or so and check releases for anything new then. thanks for tips!