Skip to content

Commit 60a3df4

Browse files
authored
Merge pull request microsoft#161705 from microsoft/sandy081/arrogant-starfish
fix turn on sync
2 parents b8a7485 + d0c5e38 commit 60a3df4

File tree

2 files changed

+54
-30
lines changed

2 files changed

+54
-30
lines changed

src/vs/platform/userDataSync/common/userDataAutoSyncService.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,6 @@ class AutoSync extends Disposable {
411411
this.syncTask?.stop();
412412
this.logService.info('Auto Sync: Stopped');
413413
}));
414-
this.logService.info('Auto Sync: Started');
415414
this.sync(AutoSync.INTERVAL_SYNCING, false);
416415
}
417416

src/vs/platform/userDataSync/common/userDataSyncService.ts

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
104104
async createSyncTask(manifest: IUserDataManifest | null, disableCache?: boolean): Promise<IUserDataSyncTask> {
105105
this.checkEnablement();
106106

107+
this.logService.info('Sync started.');
108+
const startTime = new Date().getTime();
107109
const executionId = generateUuid();
108110
try {
109111
const syncHeaders = createSyncHeaders(executionId);
@@ -127,7 +129,9 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
127129
throw new Error('Can run a task only once');
128130
}
129131
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();
131135
},
132136
stop(): Promise<void> {
133137
cancellablePromise?.cancel();
@@ -143,9 +147,10 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
143147
throw new UserDataSyncError('Cannot start manual sync when sync is enabled', UserDataSyncErrorCode.LocalError);
144148
}
145149

150+
this.logService.info('Sync started.');
151+
const startTime = new Date().getTime();
146152
const executionId = generateUuid();
147153
const syncHeaders = createSyncHeaders(executionId);
148-
149154
let manifest: IUserDataManifest | null;
150155
try {
151156
manifest = await this.userDataSyncStoreService.manifest(null, syncHeaders);
@@ -166,12 +171,9 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
166171
return that.sync(manifest, true, executionId, cancellableToken.token);
167172
},
168173
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();
175177
},
176178
stop(): Promise<void> {
177179
cancellableToken.cancel();
@@ -185,14 +187,8 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
185187
}
186188

187189
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();
193190
this._syncErrors = [];
194191
try {
195-
this.logService.info('Sync started.');
196192
if (this.status !== SyncStatus.HasConflicts) {
197193
this.setStatus(SyncStatus.Syncing);
198194
}
@@ -208,27 +204,56 @@ export class UserDataSyncService extends Disposable implements IUserDataSyncServ
208204
if (token.isCancellationRequested) {
209205
return;
210206
}
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);
224208
}
225-
this.logService.info(`Sync done. Took ${new Date().getTime() - startTime}ms`);
226-
this.updateLastSyncTime();
227209
} finally {
228210
this._onSyncErrors.fire(this._syncErrors);
229211
}
230212
}
231213

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+
232257
private async syncProfile(profileSynchronizer: ProfileSynchronizer, manifest: IUserDataManifest | null, merge: boolean, executionId: string, token: CancellationToken): Promise<IUserDataSyncResourceError[]> {
233258
const errors = await profileSynchronizer.sync(manifest, merge, executionId, token);
234259
return errors.map(([syncResource, error]) => ({ profile: profileSynchronizer.profile, syncResource, error }));

0 commit comments

Comments
 (0)