D
Deno

help

Is there an idiomatic/common/modern way of creating a queue of jobs to be executed one after anot...

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

Looking for more? Join the community!

Recommended Posts
Launch tests with unstable flag inside VS CodeHi there, I'm creating a module that uses FFI and Deno.dlopen and I wrote tests to validate some of What is the difference between JS Map and JS Object?I know that `Map` is hash table implementation for JS, but how does it differ from an object? With rDeno KV: Should I expect 'value too large' errors inserting a JSON file with kv.set?I was trying out kv and quickly ran into something I found surprising; ```js let kv = await Deno.oDeno language server stops working in VS CodeAfter some random time the language server stops working and I suddenly get code errors like this: `Getting free disk space without using child_process.Hello everyone! I would like to create a small CLI tool using Typescript and Deno. I would like to mtest --filter a stepCan I only run a certain step in the test?fastwebsockets how to end client calls and avoid Error in websocket connection Unexpected EOFWhat is the issue in this client code that causes the error? ```rust pub async fn connect() -> Resuldeno compile --include argumentIn `deno compile`, how can i include a whole directory instead of just a file? Also, is there a way `deno run` as a library`deno run` does a lot of useful things behind the scenes, many of which are not exposed as a libraryPermissions with npm/node importsI'm trying to run a command and it's like I need to add `--allow-read` permissions because of the `nBroadcastChannelWhen using BroadcastChannel api, in the background does deno send the broadcast to every instance orIs it possible to create a TCP server on Deno Deploy?Is it possible to create a TCP server on Deno Deploy?oak server can't accept requests from another server?hi, I'm trying out deno with oak to make a simple server. I set up cors(x/cors), it accepts requestsaddEventListener for keydown in terminal via deno?Is there something like `addEventlistener("keydown")` in deno, so i can capture terminal inputs?Deno KV Type ErrorWhen I run await (await kv.get<typeThing>(["example"])).id (the value is an object), it returns a tyHow to improve seo ?https://deno.land/x/blog My question is when you use tool like this that work with md, how can i do Hashbang with TS file (without .ts extension)Hi y'all, I have a few convenience scripts written in Typescript for Deno that all have a wrapper scAstro + Deno - Remote ImportsFirst of all thanks a lot for the nice template, it looks great !! I followed the guide posted hereDebugging test in VSCode always breaks on line 54 of colors.tsWhen debugging tests in VSCode the debugger always breaks on line 54 of colors.ts. This behavior is Building from `src` offline?Hey guys! I'm new to Deno (and web dev as well to be honest), so could you help me here please. I'