@@ -11,6 +11,7 @@ import { JobsService } from '../jobs/jobs.service';
1111import { TasksService } from '../tasks/tasks.service' ;
1212import { ActionType } from './_enum/action-type.enum' ;
1313import { ExecuteJobOptions } from './_interfaces/execute-job-options.interface' ;
14+ import { Tasks } from '../tasks/_schemas/tasks.schema' ;
1415
1516const DEFAULT_SYNC_TIMEOUT = 30_000 ;
1617
@@ -28,7 +29,6 @@ export class BackendsService extends AbstractQueueProcessor {
2829 public async onModuleInit ( ) {
2930 await super . onModuleInit ( ) ;
3031
31- //TODO: resync failed + completed
3232 const jobsCompleted = await this . _queue . getCompleted ( ) ;
3333 for ( const job of jobsCompleted ) {
3434 const isSyncedJob = await this . jobsService . model . findOneAndUpdate < Jobs > (
@@ -43,6 +43,11 @@ export class BackendsService extends AbstractQueueProcessor {
4343 { new : true } ,
4444 ) ;
4545 if ( isSyncedJob ) {
46+ await this . identitiesService . model . findByIdAndUpdate ( isSyncedJob . concernedTo , {
47+ $set : {
48+ state : IdentityState . SYNCED ,
49+ } ,
50+ } ) ;
4651 this . logger . warn ( `Job already completed, syncing... [${ job . id } ::COMPLETED]` ) ;
4752 }
4853 }
@@ -64,7 +69,7 @@ export class BackendsService extends AbstractQueueProcessor {
6469
6570 this . queueEvents . on ( 'failed' , async ( payload ) => {
6671 this . logger . debug ( `Job failed ! [${ payload . jobId } ]` ) ;
67- await this . jobsService . model . findOneAndUpdate < Jobs > (
72+ const failedJob = await this . jobsService . model . findOneAndUpdate < Jobs > (
6873 { jobId : payload . jobId , state : { $ne : JobState . COMPLETED } } ,
6974 {
7075 $set : {
@@ -75,10 +80,15 @@ export class BackendsService extends AbstractQueueProcessor {
7580 } ,
7681 { new : true } ,
7782 ) ;
83+ await this . identitiesService . model . findByIdAndUpdate ( failedJob . concernedTo , {
84+ $set : {
85+ state : IdentityState . ON_ERROR ,
86+ } ,
87+ } ) ;
7888 } ) ;
7989
8090 this . queueEvents . on ( 'completed' , async ( payload ) => {
81- await this . jobsService . model . findOneAndUpdate < Jobs > (
91+ const completedJob = await this . jobsService . model . findOneAndUpdate < Jobs > (
8292 { jobId : payload . jobId , state : { $ne : JobState . COMPLETED } } ,
8393 {
8494 $set : {
@@ -87,8 +97,13 @@ export class BackendsService extends AbstractQueueProcessor {
8797 result : payload . returnvalue ,
8898 } ,
8999 } ,
90- { upsert : true } ,
100+ { upsert : true , new : true } ,
91101 ) ;
102+ await this . identitiesService . model . findByIdAndUpdate ( completedJob . concernedTo , {
103+ $set : {
104+ state : IdentityState . SYNCED ,
105+ } ,
106+ } ) ;
92107 this . logger . log ( `Job completed... Syncing [${ payload . jobId } ]` ) ;
93108 } ) ;
94109 }
@@ -114,9 +129,21 @@ export class BackendsService extends AbstractQueueProcessor {
114129 } ) ;
115130 }
116131
132+ const task : Tasks = await this . tasksService . create < Tasks > ( {
133+ jobs : identities . map ( ( identity ) => identity . identity . _id ) ,
134+ } ) ;
135+
117136 const result = { } ;
118137 for ( const identity of identities ) {
119- const [ executedJob ] = await this . executeJob ( identity . action , identity . identity . _id , { identity } , options ) ;
138+ const [ executedJob ] = await this . executeJob (
139+ identity . action ,
140+ identity . identity . _id ,
141+ { identity } ,
142+ {
143+ ...options ,
144+ task : task . _id ,
145+ } ,
146+ ) ;
120147 result [ identity . identity . _id ] = executedJob ;
121148 }
122149 return result ;
@@ -148,6 +175,7 @@ export class BackendsService extends AbstractQueueProcessor {
148175 params : payload ,
149176 concernedTo : concernedTo ,
150177 comment : options ?. comment ,
178+ task : options ?. task ,
151179 state : JobState . CREATED ,
152180 ...optionals ,
153181 } ) ;
@@ -163,6 +191,11 @@ export class BackendsService extends AbstractQueueProcessor {
163191 result : response ,
164192 } ,
165193 } ) ;
194+ await this . identitiesService . model . findByIdAndUpdate ( concernedTo , {
195+ $set : {
196+ state : IdentityState . SYNCED ,
197+ } ,
198+ } ) ;
166199 return [ jobStoreUpdated as unknown as Jobs , response ] ;
167200 } catch ( err ) {
168201 error = err ;
@@ -174,6 +207,11 @@ export class BackendsService extends AbstractQueueProcessor {
174207 result : error ,
175208 } ,
176209 } ) ;
210+ await this . identitiesService . model . findByIdAndUpdate ( concernedTo , {
211+ $set : {
212+ state : IdentityState . ON_ERROR ,
213+ } ,
214+ } ) ;
177215 if ( options ?. timeoutDiscard !== false ) {
178216 job . discard ( ) ;
179217 throw new BadRequestException ( {
0 commit comments