S
SolidJSβ€’4w ago
sh03

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
TaQuanMinhLong
TaQuanMinhLongβ€’4w ago
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
sh03
sh03OPβ€’4w ago
@TaQuanMinhLong Do you mean the OS cron system or what do you mean by using the worker thread?
TaQuanMinhLong
TaQuanMinhLongβ€’4w ago
I mean js is single threaded, so it's better to have another thread running the cron job instead if possible
sh03
sh03OPβ€’4w ago
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.
TaQuanMinhLong
TaQuanMinhLongβ€’4w ago
:Worry_Think: I think every framework should have some sort of entry point
sh03
sh03OPβ€’4w ago
word
TaQuanMinhLong
TaQuanMinhLongβ€’4w ago
@Atila :Worry_DontKnow:
TaQuanMinhLong
TaQuanMinhLongβ€’4w ago
Tasks - Nitro
Nitro tasks allow on-off operations in runtime.
TaQuanMinhLong
TaQuanMinhLongβ€’4w ago
I think this should be helpful
sh03
sh03OPβ€’4w ago
Thank you very much kind stranger πŸ™
TaQuanMinhLong
TaQuanMinhLongβ€’4w ago
:Worry_OK:
Atila
Atilaβ€’4w ago
@Brendonovich and I just had a look and though we haven't tried it, everything suggests this would work:
import { defineConfig } from "@solidjs/start/config";

export default defineConfig({
server: {
experimental: {
tasks: true,
},
scheduledTasks: {
// being `db:backup' the npm script you'd run for the task
"* * * * *": ["db:backup"],
},
},
});
import { defineConfig } from "@solidjs/start/config";

export default defineConfig({
server: {
experimental: {
tasks: true,
},
scheduledTasks: {
// being `db:backup' the npm script you'd run for the task
"* * * * *": ["db:backup"],
},
},
});
sh03
sh03OPβ€’4w ago
Ah so the server object there is nitro's config?
Atila
Atilaβ€’4w ago
pretty much
sh03
sh03OPβ€’4w ago
Nice thanks πŸ‘
Atila
Atilaβ€’4w ago
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 πŸ™‚
sh03
sh03OPβ€’4w ago
Apparently the task needs to be defined as a Nitro task with its own syntax, e.g.:
export default defineTask({
meta: {
name: "db:migrate",
description: "Run database migrations",
},
run({ payload, context }) {
console.log("Running DB migration task...");
return { result: "Success" };
},
});
export default defineTask({
meta: {
name: "db:migrate",
description: "Run database migrations",
},
run({ payload, context }) {
console.log("Running DB migration task...");
return { result: "Success" };
},
});
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.
Atila
Atilaβ€’4w ago
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?
sh03
sh03OPβ€’4w ago
Alright I can confirm I was able to make it work by adding this in app.config.ts
experimental: {
tasks: true,
},
scheduledTasks: {
"* * * * *": ["cron"],
},
experimental: {
tasks: true,
},
scheduledTasks: {
"* * * * *": ["cron"],
},
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:
pnpm add nitropack
pnpm add nitropack
and finally we can define the task in cron.ts as:
import { defineTask } from "nitropack/runtime";

export default defineTask({
meta: {
name: "db:migrate",
description: "Run database migrations",
},
run() {
console.log("Running DB migration task...");
return { result: "Success" };
},
});
import { defineTask } from "nitropack/runtime";

export default defineTask({
meta: {
name: "db:migrate",
description: "Run database migrations",
},
run() {
console.log("Running DB migration task...");
return { result: "Success" };
},
});
(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.
Brendonovich
Brendonovichβ€’4w ago
damn that's pretty cool that it works
sh03
sh03OPβ€’4w ago
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?
nksaraf
nksarafβ€’4w ago
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
Atila
Atilaβ€’4w ago
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
sh03
sh03OPβ€’4w ago
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...
apatrida
apatridaβ€’4w ago
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?
Brendonovich
Brendonovichβ€’4w ago
There isn’t protection for this yet, it’d require something like skew protection
apatrida
apatridaβ€’4w ago
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.
Brendonovich
Brendonovichβ€’4w ago
You might be able to read a git hash from env? Not sure what’s available by default but you could add it yourself
apatrida
apatridaβ€’4w ago
too granular, ok, I'll maybe do a hash at buildtime from the output of the server functions or something. will think on it.
Brendonovich
Brendonovichβ€’4w ago
You might be able to get a hash from getServerFunctionMeta, not sure if that exists yet
apatrida
apatridaβ€’4w ago
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!
Want results from more Discord servers?
Add your server