ataractic
ataractic17mo 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
/|\  /|\
/|\ /|\17mo 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
ataracticOP17mo 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
/|\  /|\
/|\ /|\17mo ago
I would create tasks like: [function, callback] It executes the function and then calls the callback with the result of the function
ataractic
ataracticOP17mo ago
Since my task is always the same but for different conditions i can use the same function
/|\  /|\
/|\ /|\17mo ago
Then maybe you could simply use: [args, callback]
ataractic
ataracticOP17mo ago
is the callback really needed?
/|\  /|\
/|\ /|\17mo ago
Depends on what you want to do with the result
ataractic
ataracticOP17mo 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
/|\  /|\
/|\ /|\17mo 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
ataracticOP17mo 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
ataracticOP17mo ago
i think i found my answer with this deno workers https://deno.land/manual@v1.36.3/runtime/workers
/|\  /|\
/|\ /|\17mo ago
Yeah you should probably better use a while statement Looks perfect for your needs
Esente
Esente17mo ago
Worker may not be available in Deno Deploy.
mxdvl
mxdvl17mo ago
p-queue is a nice library that does exactly what you describe: https://deno.land/x/p_queue
ataractic
ataracticOP17mo ago
thank you everyone but i'm going with workers as a final choice

Did you find this page helpful?