export default async function run( executor: IExecutor, queue: AsyncIterable<ITask>, maxThreads = 0 ) { maxThreads = Math.max(0, maxThreads);
const AllTasks = new Map<number, TaskLog[]>(); const activeTargets = new Set<number>(); const pending = new Map<number, ITask[]>(); const running = new Set<Promise<void>>();
async function startTask(task: ITask) { const startTime = Date.now();
if (!AllTasks.has(task.targetId)) {
AllTasks.set(task.targetId, []);
}
// сохраняем инфу о задаче
AllTasks.get(task.targetId)!.push({
task,
start: startTime,
end: null,
});
activeTargets.add(task.targetId);
const p = executor
.executeTask(task)
.then(() => {
const end = Date.now();
const taskLog = AllTasks.get(task.targetId)!.find(
(t) => t.start === startTime
);
if (taskLog) taskLog.end = end;
activeTargets.delete(task.targetId);
const q = pending.get(task.targetId);
if (q && q.length > 0) {
const next = q.shift()!;
startTask(next);
}
})
.finally(() => {
running.delete(p);
});
running.add(p);
}
for await (const task of queue) { if (activeTargets.has(task.targetId)) { if (!pending.has(task.targetId)) pending.set(task.targetId, []); pending.get(task.targetId)!.push(task); continue; }
startTask(task);
while (maxThreads > 0 && running.size >= maxThreads) {
await Promise.race(running);
}
}
while (running.size > 0) { await Promise.race(running); } }Hello! I encountered such a change in my code: when running the run() function with the maxThreads = 2 parameter, the tasks do not pass valid checks (tests fail), although with maxThreads = 5 with the same sequence of modifications everything is successfully executed. If anyone has any ideas, let me know! I will be very grateful!
Recommended threads
- How can I disable client-side account re...
Hi everyone! I’m currently building a game backend using Appwrite, and I’d like to prevent users from creating new accounts directly through the client. My go...
- relationships bug or error idk
hey every one, am using am using appwrite on a project and ran into a problem where by created a relationship and two way to be specific and it is stuck on proc...
- Domain is owned by a different organizat...
Hi Appwrite team, I'm trying to add my custom domain **futlive9.com** to my project, but I'm getting the error: "Domain is owned by a different organization." ...