@@ -28,11 +28,19 @@ export async function findAndProcessAQueuedTask() {
2828 { status : 'Processing' } ,
2929 { new : true }
3030 )
31+
3132 if ( task != null ) {
3233 activeTasks ++
3334 await processNextTaskRound ( task )
3435 activeTasks --
3536 }
37+
38+ const asyncTasks = await TaskModel . find ( { status : 'Pending Async' } ) ;
39+
40+ asyncTasks . forEach ( async task => {
41+ await checkAsyncTaskStatus ( task ) ;
42+ } )
43+
3644 } catch ( err ) {
3745 if ( task == null ) {
3846 logger . error ( `An error occurred while looking for rerun tasks: ${ err } ` )
@@ -45,6 +53,34 @@ export async function findAndProcessAQueuedTask() {
4553 }
4654}
4755
56+ async function checkAsyncTaskStatus ( task ) {
57+ const pendingAsyncTransactions = task . transactions . filter ( transaction => transaction . rerunStatus === 'Pending Async' ) ;
58+
59+ let remainingAsyncTransactions = pendingAsyncTransactions . length ;
60+
61+ pendingAsyncTransactions . forEach ( async transaction => {
62+ const currentTransactionStatus = await TransactionModel . findById ( transaction . rerunID ) ;
63+
64+ if ( [ "Successful" , "Completed with error(s)" , "Failed" ] . includes ( currentTransactionStatus . status ) ) {
65+ transaction . tstatus = 'Completed' ;
66+ transaction . rerunStatus = currentTransactionStatus . status ;
67+ await task . save ( ) ;
68+ remainingAsyncTransactions -- ;
69+ }
70+ } ) ;
71+
72+
73+ if ( remainingAsyncTransactions === 0 ) {
74+ task . status = 'Completed' ;
75+ task . completedDate = new Date ( ) ;
76+ await task . save ( )
77+ logger . info ( `Async task ${ task . _id } completed` ) ;
78+ }
79+
80+ return ;
81+
82+ }
83+
4884function rerunTaskProcessor ( ) {
4985 if ( live ) {
5086 findAndProcessAQueuedTask ( )
@@ -139,6 +175,8 @@ async function processNextTaskRound(task) {
139175 return
140176 }
141177
178+ let taskHasAsyncTransactions = false
179+
142180 const promises = transactions . map ( transaction => {
143181 task . remainingTransactions --
144182
@@ -158,7 +196,11 @@ async function processNextTaskRound(task) {
158196 logger . error (
159197 `An error occurred while rerunning transaction ${ transaction . tid } for task ${ task . _id } : ${ err } `
160198 )
161- } else {
199+ } else if ( response . status === 202 ) {
200+ transaction . tstatus = 'Processing'
201+ taskHasAsyncTransactions = true
202+ }
203+ else {
162204 transaction . tstatus = 'Completed'
163205 }
164206 return resolve ( )
@@ -177,8 +219,8 @@ async function processNextTaskRound(task) {
177219 if ( task . remainingTransactions ) {
178220 await processNextTaskRound ( task )
179221 } else {
180- task . status = 'Completed'
181- task . completedDate = new Date ( )
222+ task . status = taskHasAsyncTransactions ? 'Pending Async' : 'Completed'
223+ task . completedDate = taskHasAsyncTransactions ? null : new Date ( )
182224 logger . info ( `Round completed for rerun task #${ task . _id } - Task completed` )
183225
184226 await task . save ( ) . catch ( err => {
0 commit comments