Skip to content

Commit 7553257

Browse files
committed
fixes
1 parent 0fa64c1 commit 7553257

File tree

2 files changed

+40
-32
lines changed

2 files changed

+40
-32
lines changed

js/botasaurus-server-js/src/routes-db-logic.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ async function save(x: [number, string]) {
633633
}
634634
async function executeGetTasks(queryParams: Record<string, any>): Promise<any> {
635635

636-
const withResults = isAffirmative(queryParams.with_results || 'false');;
636+
const withResults = isAffirmative(queryParams.with_results || 'false');
637637

638638
let page = queryParams.page;
639639
let per_page = queryParams.per_page;
@@ -1032,6 +1032,7 @@ function convertUnicodeDictToAsciiDictInPlace(inputList: any[]): any[] {
10321032

10331033
if (is_all_task) {
10341034
await TaskHelper.abortChildTasks(taskId);
1035+
await TaskHelper.collectAndSaveAllTaskForAbortedTask(taskId, removeDuplicatesBy);
10351036
} else {
10361037
if (parentId) {
10371038
const allChildrenCount = await TaskHelper.getAllChildrenCount(parentId, taskId);

js/botasaurus-server-js/src/task-helper.ts

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -172,26 +172,31 @@ class TaskHelper {
172172
const query: any = {
173173
parent_task_id: parentId,
174174
status: TaskStatus.COMPLETED,
175+
result_count: { $gte: 1 },
175176
};
176177

177178
if (exceptTaskId) {
178179
query.id = { $ne: exceptTaskId };
179180
}
180181

181-
const results = await new Promise<any[]>((resolve, reject) => {
182-
db.find(query).sort({ sort_id: -1}).exec((err: any, docs: any[]) => {
183-
if (err) {
184-
reject(err);
185-
} else {
186-
resolve(docs.map((doc) => doc.id));
187-
}
188-
})
189-
});
190-
191-
return results
192-
182+
const docs = await db.findAsync(query, { id: 1 }).sort({ sort_id: -1 }) as any[];
183+
return docs.map((doc) => doc.id);
193184
}
194185

186+
static async getChildrenIdsWithResults(parentId: number): Promise<any[]> {
187+
const query = {
188+
parent_task_id: parentId,
189+
result_count: { $gte: 1 },
190+
};
191+
192+
const docs = await db.findAsync(query, { id: 1 }).sort({ sort_id: -1 }) as any[];
193+
const results = docs.map((doc) => doc.id);
194+
195+
return results;
196+
}
197+
198+
199+
195200
static async areAllChildTaskDone(parentId: number): Promise<boolean> {
196201
const doneChildrenCount = await TaskHelper.getDoneChildrenCount(
197202
parentId
@@ -472,41 +477,43 @@ class TaskHelper {
472477
parentId,
473478
exceptTaskId,
474479
)
480+
481+
return await this.finishParentTask(ids, parentId, removeDuplicatesBy, status, shouldFinish)
482+
}
483+
484+
static async finishParentTask(ids: any[], parentId: number, removeDuplicatesBy: string | null, status: string, shouldFinish: boolean) {
475485
let [itemsCount, path] = await normalizeAndDeduplicateChildrenTasks(ids, parentId, removeDuplicatesBy)
476486
const isLarge = isLargeFile(path as any)
477487

478-
const taskUpdateDetails:any = {
488+
const taskUpdateDetails: any = {
479489
result_count: itemsCount,
480490
status: status,
481491
is_large: isLarge,
482492
}
483493
if (shouldFinish) {
484494
// this flow ran by task deletion/abortuin
485495
const now_date = new Date()
486-
taskUpdateDetails['finished_at'] = now_date
487-
488-
return this.updateTask(parentId, taskUpdateDetails)
489-
// const query = {
490-
// $and: [
491-
// { id: parentId },
492-
// { started_at: null },
493-
// ]
494-
// }
495-
// const update = {
496-
// "started_at": now_date
497-
// }
498-
499-
// const updateResult = await this.updateTaskByQuery(query, update)
500-
// console.log({query, update, updateResult, })
501-
// return updateResult
496+
taskUpdateDetails['finished_at'] = now_date
502497

503498
} else {
504499
// this flow ran by cache completeion
505-
return this.updateTask(parentId, taskUpdateDetails);
506500
}
507-
501+
502+
return this.updateTask(parentId, taskUpdateDetails)
503+
}
504+
505+
static async collectAndSaveAllTaskForAbortedTask(
506+
parentId: number,
507+
removeDuplicatesBy: string | null,
508+
) {
509+
const ids = await this.getChildrenIdsWithResults(
510+
parentId,
511+
)
512+
513+
return await this.finishParentTask(ids, parentId, removeDuplicatesBy, TaskStatus.ABORTED, true)
508514

509515
}
516+
510517
static async readCleanSaveTask(
511518
parentId: number,
512519
removeDuplicatesBy: string | null,

0 commit comments

Comments
 (0)