kit icon indicating copy to clipboard operation
kit copied to clipboard

Worker Threads

Open Beiri22 opened this issue 1 year ago • 4 comments

Describe the problem

I'd like to let Sveltekit run a regular background task that is CPU-intensive and don't want to block all the apps function. The only way visible to me is using node's worker threads, either directly or using a library like Bree. Problem is, that each worker has to be a separate file that is only referenced by its name. It kinda works in dev mode; but when you run the build step, my worker gets treeshaken out; and even if it did not, the bundler would change the name and may not adapt the reference. I'm greatful about every hint...

Describe the proposed solution

Do support worker threads in some way; or help me how to prevent my worker from being tree-shaken.

Alternatives considered

No response

Importance

i cannot use SvelteKit without it

Additional Information

No response

Beiri22 avatar Jun 07 '23 15:06 Beiri22

Importing workers is really a Vite thing.

As far as I know it's limited to web workers

Do support worker threads in some way; or help me how to prevent my worker from being tree-shaken.

What sort of work do you want to put on the worker? Is it something that needs sveltekit specific state or can it just be an additional script you run along side your sveltekit project?

ghostdevv avatar Jun 07 '23 23:06 ghostdevv

It should be possible to be put into a separate script, as long as it can access the database. I'm using prisma for the database, so the prisma schema and generated client library are in some way part of the svelte project. Is there a way to define a deparate entrypoint in the project for such a script?

Beiri22 avatar Jun 08 '23 05:06 Beiri22

You should just be able to import your prisma client into a js/ts file and run it. When you deploy your kit application (I am assuming you are using node adapter) you can copy your script as well and run it as a separate process. Don't forget to make sure the prisma node module is installed wherever you deploy the script, or you could have a build script that will bundle it into a single file - up to you!

Also I found an already open issue (#3653) for worker threads

ghostdevv avatar Jun 08 '23 12:06 ghostdevv

I could try that; but real support that works in dev and prod would be a lot nicer. Is there any easy way to tell vite to just copy a job-script; or better to bundle it separately?

Beiri22 avatar Jun 08 '23 15:06 Beiri22