@@ -104,6 +104,8 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
104
104
async createSyncTask ( manifest : IUserDataManifest | null , disableCache ?: boolean ) : Promise < IUserDataSyncTask > {
105
105
this . checkEnablement ( ) ;
106
106
107
+ this . logService . info ( 'Sync started.' ) ;
108
+ const startTime = new Date ( ) . getTime ( ) ;
107
109
const executionId = generateUuid ( ) ;
108
110
try {
109
111
const syncHeaders = createSyncHeaders ( executionId ) ;
@@ -127,7 +129,9 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
127
129
throw new Error ( 'Can run a task only once' ) ;
128
130
}
129
131
cancellablePromise = createCancelablePromise ( token => that . sync ( manifest , false , executionId , token ) ) ;
130
- return cancellablePromise . finally ( ( ) => cancellablePromise = undefined ) ;
132
+ await cancellablePromise . finally ( ( ) => cancellablePromise = undefined ) ;
133
+ that . logService . info ( `Sync done. Took ${ new Date ( ) . getTime ( ) - startTime } ms` ) ;
134
+ that . updateLastSyncTime ( ) ;
131
135
} ,
132
136
stop ( ) : Promise < void > {
133
137
cancellablePromise ?. cancel ( ) ;
@@ -143,9 +147,10 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
143
147
throw new UserDataSyncError ( 'Cannot start manual sync when sync is enabled' , UserDataSyncErrorCode . LocalError ) ;
144
148
}
145
149
150
+ this . logService . info ( 'Sync started.' ) ;
151
+ const startTime = new Date ( ) . getTime ( ) ;
146
152
const executionId = generateUuid ( ) ;
147
153
const syncHeaders = createSyncHeaders ( executionId ) ;
148
-
149
154
let manifest : IUserDataManifest | null ;
150
155
try {
151
156
manifest = await this . userDataSyncStoreService . manifest ( null , syncHeaders ) ;
@@ -166,12 +171,9 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
166
171
return that . sync ( manifest , true , executionId , cancellableToken . token ) ;
167
172
} ,
168
173
async apply ( ) : Promise < void > {
169
- for ( const profileSynchronizer of that . getActiveProfileSynchronizers ( ) ) {
170
- if ( cancellableToken . token . isCancellationRequested ) {
171
- return ;
172
- }
173
- await profileSynchronizer . apply ( executionId , cancellableToken . token ) ;
174
- }
174
+ await that . applyManualSync ( manifest , executionId , cancellableToken . token ) ;
175
+ that . logService . info ( `Sync done. Took ${ new Date ( ) . getTime ( ) - startTime } ms` ) ;
176
+ that . updateLastSyncTime ( ) ;
175
177
} ,
176
178
stop ( ) : Promise < void > {
177
179
cancellableToken . cancel ( ) ;
@@ -185,14 +187,8 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
185
187
}
186
188
187
189
private async sync ( manifest : IUserDataManifest | null , merge : boolean , executionId : string , token : CancellationToken ) : Promise < void > {
188
- // Return if cancellation is requested
189
- if ( token . isCancellationRequested ) {
190
- return ;
191
- }
192
- const startTime = new Date ( ) . getTime ( ) ;
193
190
this . _syncErrors = [ ] ;
194
191
try {
195
- this . logService . info ( 'Sync started.' ) ;
196
192
if ( this . status !== SyncStatus . HasConflicts ) {
197
193
this . setStatus ( SyncStatus . Syncing ) ;
198
194
}
@@ -208,27 +204,56 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
208
204
if ( token . isCancellationRequested ) {
209
205
return ;
210
206
}
211
- for ( const syncProfile of syncProfiles ) {
212
- if ( token . isCancellationRequested ) {
213
- return ;
214
- }
215
- const profile = this . userDataProfilesService . profiles . find ( p => p . id === syncProfile . id ) ;
216
- if ( ! profile ) {
217
- this . logService . error ( `Settings Profile with id:${ syncProfile . id } and name: ${ syncProfile . name } does not exist locally to sync.` ) ;
218
- continue ;
219
- }
220
- this . logService . info ( 'Syncing profile.' , syncProfile . name ) ;
221
- const profileSynchronizer = this . getOrCreateActiveProfileSynchronizer ( profile , syncProfile ) ;
222
- this . _syncErrors . push ( ...await this . syncProfile ( profileSynchronizer , manifest , merge , executionId , token ) ) ;
223
- }
207
+ await this . syncRemoteProfiles ( syncProfiles , manifest , merge , executionId , token ) ;
224
208
}
225
- this . logService . info ( `Sync done. Took ${ new Date ( ) . getTime ( ) - startTime } ms` ) ;
226
- this . updateLastSyncTime ( ) ;
227
209
} finally {
228
210
this . _onSyncErrors . fire ( this . _syncErrors ) ;
229
211
}
230
212
}
231
213
214
+ private async syncRemoteProfiles ( remoteProfiles : ISyncUserDataProfile [ ] , manifest : IUserDataManifest | null , merge : boolean , executionId : string , token : CancellationToken ) : Promise < void > {
215
+ for ( const syncProfile of remoteProfiles ) {
216
+ if ( token . isCancellationRequested ) {
217
+ return ;
218
+ }
219
+ const profile = this . userDataProfilesService . profiles . find ( p => p . id === syncProfile . id ) ;
220
+ if ( ! profile ) {
221
+ this . logService . error ( `Settings Profile with id:${ syncProfile . id } and name: ${ syncProfile . name } does not exist locally to sync.` ) ;
222
+ continue ;
223
+ }
224
+ this . logService . info ( 'Syncing profile.' , syncProfile . name ) ;
225
+ const profileSynchronizer = this . getOrCreateActiveProfileSynchronizer ( profile , syncProfile ) ;
226
+ this . _syncErrors . push ( ...await this . syncProfile ( profileSynchronizer , manifest , merge , executionId , token ) ) ;
227
+ }
228
+ }
229
+
230
+ private async applyManualSync ( manifest : IUserDataManifest | null , executionId : string , token : CancellationToken ) : Promise < void > {
231
+ const profileSynchronizers = this . getActiveProfileSynchronizers ( ) ;
232
+ for ( const profileSynchronizer of profileSynchronizers ) {
233
+ if ( token . isCancellationRequested ) {
234
+ return ;
235
+ }
236
+ await profileSynchronizer . apply ( executionId , token ) ;
237
+ }
238
+
239
+ const defaultProfileSynchronizer = profileSynchronizers . find ( s => s . profile . isDefault ) ;
240
+ if ( ! defaultProfileSynchronizer ) {
241
+ return ;
242
+ }
243
+
244
+ const userDataProfileManifestSynchronizer = defaultProfileSynchronizer . enabled . find ( s => s . resource === SyncResource . Profiles ) ;
245
+ if ( ! userDataProfileManifestSynchronizer ) {
246
+ return ;
247
+ }
248
+
249
+ // Sync remote profiles which are not synced locally
250
+ const remoteProfiles = ( await ( userDataProfileManifestSynchronizer as UserDataProfilesManifestSynchroniser ) . getRemoteSyncedProfiles ( manifest ?. latest ?? null ) ) || [ ] ;
251
+ const remoteProfilesToSync = remoteProfiles . filter ( remoteProfile => profileSynchronizers . every ( s => s . profile . id !== remoteProfile . id ) ) ;
252
+ if ( remoteProfilesToSync . length ) {
253
+ await this . syncRemoteProfiles ( remoteProfilesToSync , manifest , false , executionId , token ) ;
254
+ }
255
+ }
256
+
232
257
private async syncProfile ( profileSynchronizer : ProfileSynchronizer , manifest : IUserDataManifest | null , merge : boolean , executionId : string , token : CancellationToken ) : Promise < IUserDataSyncResourceError [ ] > {
233
258
const errors = await profileSynchronizer . sync ( manifest , merge , executionId , token ) ;
234
259
return errors . map ( ( [ syncResource , error ] ) => ( { profile : profileSynchronizer . profile , syncResource, error } ) ) ;
0 commit comments