T
TanStackโ€ข9mo ago
generous-apricot

Cron / Background jobs / Tasks

In SolidJS which also uses Nitropack under the hood it was possible to setup cron jobs using Nitropack. See docs here: https://nitro.build/guide/tasks And specific implementation in SolidJS: https://github.com/solidjs/solid-docs/issues/964 AFAIK SolidJS did nothing to enable this and it just came out of the box by using Vinxi and Nitropack. I just tried the same with Tanstack Start and while I can add those configurations in app.config.ts the tasks seem to be ignored. Do you guys know by any chance how to enable that feature somehow? Thanks ๐Ÿ™ Disclamer - I know this out of scope for this so no worries if nobody wants to spend time on it; in fact thanks for this amazing library already ๐Ÿ™ - I know ideally one should run tasks in a separate process/thread since node is single threaded; my specific use case is for an app that works in a specific timezone and has traffic downtime during the night and I can run some simple and fast cleanup tasks.
Tasks - Nitro
Nitro tasks allow on-off operations in runtime.
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...
20 Replies
exotic-emerald
exotic-emeraldโ€ข9mo ago
how does your app.config.ts look like? it might be we just don't pass on those things to vinxi
generous-apricot
generous-apricotOPโ€ข9mo ago
import { defineConfig } from "@tanstack/start/config";
import tsConfigPaths from "vite-tsconfig-paths";

export default defineConfig({
vite: {
plugins: [
tsConfigPaths({
projects: ["./tsconfig.json"],
}),
],
},
server: {
experimental: {
tasks: true,
},
scheduledTasks: {
"* * * * *": ["cron"],
},
},
});
import { defineConfig } from "@tanstack/start/config";
import tsConfigPaths from "vite-tsconfig-paths";

export default defineConfig({
vite: {
plugins: [
tsConfigPaths({
projects: ["./tsconfig.json"],
}),
],
},
server: {
experimental: {
tasks: true,
},
scheduledTasks: {
"* * * * *": ["cron"],
},
},
});
Here you go
exotic-emerald
exotic-emeraldโ€ข9mo ago
i checked, we forward this to vinxi (everything in server) so this should work in terms of vinxi configuration at least which vinxi version are you using?
generous-apricot
generous-apricotOPโ€ข9mo ago
0.5.1
exotic-emerald
exotic-emeraldโ€ข9mo ago
try downgrading to 0.4.3 maybe it broke in vinxi? and nitro
generous-apricot
generous-apricotOPโ€ข9mo ago
tried downgrading and it's still not working Something that I remember used to happen in SolidJS was that there was a message saying "registered task XYZ" or something along those lines. That's also nowhere to be found. Hold on I can just push this into a repository so that you have a reproducible env
exotic-emerald
exotic-emeraldโ€ข9mo ago
so maybe the cron.ts file is not where it should be?
generous-apricot
generous-apricotOPโ€ข9mo ago
If that was the cause it would say something like "task not registered because it was not found" Hold let me give you the exact messages Nevermind. I can't make them work in SolidJS so yeah something broke with nitro probably. Thanks anyway ๐Ÿ™
exotic-emerald
exotic-emeraldโ€ข9mo ago
ah
fair-rose
fair-roseโ€ข9mo ago
any news on this ?
fair-rose
fair-roseโ€ข9mo ago
GitHub
Nitro Tasks API ยท Issue #1974 ยท nitrojs/nitro
Nitro tasks allow on-off operations in runtime. Docs: https://nitro.unjs.io/guide/tasks ### Tasks - [x] Initial Implementation (#1929) - [x] Write documentation section (PR welcome!) - [x] Stabiliz...
fair-rose
fair-roseโ€ข9mo ago
i guess we'll just have to wait then
generous-apricot
generous-apricotOPโ€ข9mo ago
Nope. I've posted here: https://github.com/nitrojs/nitro/issues/1974#issuecomment-2542516974 but apparently it's not something that was ever available I guess I've hallucinated it
fair-rose
fair-roseโ€ข9mo ago
:/
generous-apricot
generous-apricotOPโ€ข9mo ago
Boring hallucination if you ask me
fair-rose
fair-roseโ€ข9mo ago
how do you go about scheduling tasks then ? any recommendation ?
generous-apricot
generous-apricotOPโ€ข9mo ago
Luckily for me I can delay the implementation for a few months but if I had to implement it today I'd probably have a separate worker / server running the background task. I'd put the code for it inside the current codebase (or maybe create a monorepo with multiple packages) and just run tsx cron-script.ts in some worker. If you want to keep everything in the same app/deployment then I'd probably use concurrently to run both the server and the cron tasks script (probably using node-cron or similar) but it's not clear how production ready concurrently is If concurrently is not good for prod I'd go with Docker + linux crontab If you are doing serverless I have no idea as I tend to avoid that world
fair-rose
fair-roseโ€ข9mo ago
Thanks for the insight. I'm running both services (along with the db) in a docker compose for now the issue with something like concurrently (unless im wrong) is that there is no proper way to restart the job scheduler if it crashes, is there ?
generous-apricot
generous-apricotOPโ€ข9mo ago
That's right. If you are already using docker-compose maybe a separate container is the best option for you. You should be able to handle restarts as well.
extended-salmon
extended-salmonโ€ข8mo ago
You can also try PM2 to run a cron on the same or another process.

Did you find this page helpful?