@@ -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