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 to Display File in Web?
I'm trying to use Appwrite's Storage to store images and display them in my app, however when I use the `getFileView`, `getFileDownload` or `getFilePreview` met...
- Project Paused Despite Daily Active Usag...
I noticed that my project was automatically **paused**, even though it is actively being used. The project is an **attendance application** that is used daily b...
- Sudden CORS Errors - Domain hasn't Chang...
I have an Appwrite project with two web apps configured, the first one has the hostname `*` and the second one I just added to test if it could fix the issue wi...