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
- Problem with Google Workspace at DNS Rec...
Hello, I bought a domain at Namecheap, and Google Workspace used to work there, but now that I switched from Custom DNS to Appwrite's nameservers, it doesn't w...
- change role of a team member in Appwrite
It's not possible to add/change roles of a team meber in Appwrite Frontend. When you click on a member of a team you get forwarded to the configuration page of ...
- Session not found. Please run appwrite l...
I have encounter an issue with appwrite CLI They asking for a login session but in the doc, it’s mention that only setup client with endpoint / api key is enou...