Is there an idiomatic/common/modern way of creating a queue of jobs to be executed one after anot...
Is there an idiomatic/common/modern way of creating a queue of jobs to be executed one after another using Deno and typescript?
Example: Multiple users wants to execute a resource heavy job by clicking a button on their dashboard, however to not lag the host, only one job should be executed at a time.
15 Replies
I would create an array with queued tasks, when system ends first task, it removes it from array and then start the next one
so my idea is to create an array of objects that contain necessary information about a task but i would need to create a sub process that runs along the web server and checks for jobs? how can i do such a process?
also how to create a global array
I would create tasks like:
[function, callback]
It executes the function and then calls the callback with the result of the functionSince my task is always the same but for different conditions i can use the same function
Then maybe you could simply use:
[args, callback]
is the callback really needed?
Depends on what you want to do with the result
i think i don't need a callback
the real issue here is how to create a global variable and continuous running process that checks for the array
I think you have a globalThis object in deno
No sorry it's just window
So just use
window[my array]
Then create a function to execute the last queued task, and check it's running only once at a time. Each time you append something to the array, check if function is running
Ex:
i understand, will try this method
your function has one problem we should check if there is more tasks in the queue out of the
startTask()
function, because executing the same function inside it can potentially never get out if there is always jobs to be done and i'm sure it would cause memory congestion or leaks at some point.
but i see your pointi think i found my answer with this deno workers https://deno.land/manual@v1.36.3/runtime/workers
Yeah you should probably better use a while statement
Looks perfect for your needs
Worker may not be available in Deno Deploy.
p-queue is a nice library that does exactly what you describe: https://deno.land/x/p_queue
thank you everyone but i'm going with workers as a final choice