
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
- deleteDocument shows The current user is...
my application is an ecomm app that list the products and i just want to add the delete functionality to delete the product the user has permission in appwrite...
- File preview issue- "message": "Image tr...
Hi everyone 👋 I’m using Appwrite on the Free Plan. When I try to display images using getFilePreview() from the Storage API, I get this error: "message": "I...
- Getting “Internal Error 500” for all ser...
SDK error is “Access to this resource is block” - but the credentials and same script was working fine before (there’s been no changes my side). The Console sys...
