ataractic
ataractic16mo ago

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
/|\  /|\
/|\ /|\16mo ago
I would create an array with queued tasks, when system ends first task, it removes it from array and then start the next one
ataractic
ataracticOP16mo ago
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
/|\  /|\
/|\ /|\16mo ago
I would create tasks like: [function, callback] It executes the function and then calls the callback with the result of the function
ataractic
ataracticOP16mo ago
Since my task is always the same but for different conditions i can use the same function
/|\  /|\
/|\ /|\16mo ago
Then maybe you could simply use: [args, callback]
ataractic
ataracticOP16mo ago
is the callback really needed?
/|\  /|\
/|\ /|\16mo ago
Depends on what you want to do with the result
ataractic
ataracticOP16mo ago
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
/|\  /|\
/|\ /|\16mo ago
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:
let running = false;
function startTask() {
if (running) return;
running = true; task(window["example"].shift());
running = false;
if (window["example"].length) startTask();
}
let running = false;
function startTask() {
if (running) return;
running = true; task(window["example"].shift());
running = false;
if (window["example"].length) startTask();
}
ataractic
ataracticOP16mo ago
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 point
ataractic
ataracticOP16mo ago
i think i found my answer with this deno workers https://deno.land/manual@v1.36.3/runtime/workers
/|\  /|\
/|\ /|\16mo ago
Yeah you should probably better use a while statement Looks perfect for your needs
Esente
Esente16mo ago
Worker may not be available in Deno Deploy.
mxdvl
mxdvl16mo ago
p-queue is a nice library that does exactly what you describe: https://deno.land/x/p_queue
ataractic
ataracticOP16mo ago
thank you everyone but i'm going with workers as a final choice