1
0
mirror of https://github.com/bitwarden/directory-connector synced 2025-12-14 23:33:19 +00:00

stop and start syncing

This commit is contained in:
Kyle Spearrin
2018-05-03 12:27:30 -04:00
parent fef65c5209
commit fd3793d3d7
6 changed files with 97 additions and 12 deletions

View File

@@ -33,6 +33,9 @@ import { UserService } from 'jslib/abstractions/user.service';
import { ConstantsService } from 'jslib/services/constants.service';
import { ConfigurationService } from '../services/configuration.service';
import { SyncService } from '../services/sync.service';
const BroadcasterSubscriptionId = 'AppComponent';
@Component({
@@ -62,7 +65,8 @@ export class AppComponent implements OnInit {
private authService: AuthService, private router: Router, private analytics: Angulartics2,
private toasterService: ToasterService, private i18nService: I18nService,
private platformUtilsService: PlatformUtilsService, private ngZone: NgZone,
private componentFactoryResolver: ComponentFactoryResolver, private messagingService: MessagingService) { }
private componentFactoryResolver: ComponentFactoryResolver, private messagingService: MessagingService,
private configurationService: ConfigurationService, private syncService: SyncService) { }
ngOnInit() {
this.broadcasterService.subscribe(BroadcasterSubscriptionId, async (message: any) => {
@@ -71,6 +75,41 @@ export class AppComponent implements OnInit {
case 'logout':
this.logOut(!!message.expired);
break;
case 'checkDirSync':
try {
const syncConfig = await this.configurationService.getSync();
if (syncConfig.interval == null || syncConfig.interval < 5) {
return;
}
const syncInterval = syncConfig.interval * 60000;
const lastGroupSync = await this.configurationService.getLastGroupSyncDate();
const lastUserSync = await this.configurationService.getLastUserSyncDate();
let lastSync: Date = null;
if (lastGroupSync != null && lastUserSync == null) {
lastSync = lastGroupSync;
} else if (lastGroupSync == null && lastUserSync != null) {
lastSync = lastUserSync;
} else if (lastGroupSync != null && lastUserSync != null) {
if (lastGroupSync.getTime() < lastUserSync.getTime()) {
lastSync = lastGroupSync;
} else {
lastSync = lastUserSync;
}
}
let lastSyncAgo = syncInterval + 1;
if (lastSync != null) {
lastSyncAgo = new Date().getTime() - lastSync.getTime();
}
if (lastSyncAgo >= syncInterval) {
await this.syncService.sync(false, false);
}
} catch { }
this.messagingService.send('scheduleNextDirSync');
break;
default:
}
});

View File

@@ -71,7 +71,8 @@ const containerService = new ContainerService(cryptoService, platformUtilsServic
const authService = new AuthService(cryptoService, apiService, userService, tokenService, appIdService,
i18nService, platformUtilsService, messagingService, false);
const configurationService = new ConfigurationService(storageService, secureStorageService);
const syncSevrice = new SyncService(configurationService, logService, cryptoFunctionService, apiService);
const syncSevrice = new SyncService(configurationService, logService, cryptoFunctionService, apiService,
messagingService);
const analytics = new Analytics(window, () => true, platformUtilsService, storageService, appIdService);
containerService.attachToWindow(window);

View File

@@ -14,8 +14,9 @@
<strong *ngIf="syncRunning" class="text-success">Running</strong>
<strong *ngIf="!syncRunning" class="text-danger">Stopped</strong>
</p>
<button (click)="start()" class="btn btn-primary">
<i class="fa fa-play fa-fw"></i>
<button #startBtn (click)="start()" [appApiAction]="startPromise" class="btn btn-primary" [disabled]="startBtn.loading">
<i class="fa fa-play fa-fw" [hidden]="startBtn.loading"></i>
<i class="fa fa-spinner fa-fw fa-spin" [hidden]="!startBtn.loading"></i>
Start Sync
</button>
<button (click)="stop()" class="btn btn-primary">

View File

@@ -1,9 +1,13 @@
import {
Component,
NgZone,
OnInit,
} from '@angular/core';
import { ToasterService } from 'angular2-toaster';
import { I18nService } from 'jslib/abstractions/i18n.service';
import { MessagingService } from 'jslib/abstractions/messaging.service';
import { AzureDirectoryService } from '../../services/azure-directory.service';
import { GSuiteDirectoryService } from '../../services/gsuite-directory.service';
@@ -15,6 +19,10 @@ import { GroupEntry } from '../../models/groupEntry';
import { UserEntry } from '../../models/userEntry';
import { ConfigurationService } from '../../services/configuration.service';
import { BroadcasterService } from 'jslib/angular/services/broadcaster.service';
const BroadcasterSubscriptionId = 'DashboardComponent';
@Component({
selector: 'app-dashboard',
templateUrl: 'dashboard.component.html',
@@ -28,24 +36,43 @@ export class DashboardComponent implements OnInit {
simPromise: Promise<any>;
simSinceLast: boolean = false;
syncPromise: Promise<any>;
startPromise: Promise<any>;
lastGroupSync: Date;
lastUserSync: Date;
syncRunning: boolean;
constructor(private i18nService: I18nService, private syncService: SyncService,
private configurationService: ConfigurationService) { }
private configurationService: ConfigurationService, private broadcasterService: BroadcasterService,
private ngZone: NgZone, private messagingService: MessagingService,
private toasterService: ToasterService) { }
async ngOnInit() {
this.lastGroupSync = await this.configurationService.getLastGroupSyncDate();
this.lastUserSync = await this.configurationService.getLastUserSyncDate();
this.broadcasterService.subscribe(BroadcasterSubscriptionId, async (message: any) => {
this.ngZone.run(async () => {
switch (message.command) {
case 'dirSyncCompleted':
this.updateLastSync();
break;
default:
}
});
});
this.updateLastSync();
}
async start() {
this.startPromise = this.syncService.sync(false, false);
await this.startPromise;
this.messagingService.send('scheduleNextDirSync');
this.syncRunning = true;
this.toasterService.popAsync('success', null, 'Syncing started.');
}
async stop() {
this.messagingService.send('cancelDirSync');
this.syncRunning = false;
this.toasterService.popAsync('success', null, 'Syncing stopped.');
}
async sync() {
@@ -115,4 +142,9 @@ export class DashboardComponent implements OnInit {
a.displayName.localeCompare(b.displayName);
});
}
private async updateLastSync() {
this.lastGroupSync = await this.configurationService.getLastGroupSyncDate();
this.lastUserSync = await this.configurationService.getLastUserSyncDate();
}
}

View File

@@ -7,7 +7,7 @@ import { WindowMain } from 'jslib/electron/window.main';
import { MenuMain } from './menu.main';
const SyncInterval = 5 * 60 * 1000; // 5 minutes
const SyncCheckInterval = 60 * 1000; // 1 minute
export class MessagingMain {
private syncTimeout: NodeJS.Timer;
@@ -21,9 +21,14 @@ export class MessagingMain {
onMessage(message: any) {
switch (message.command) {
case 'scheduleNextSync':
case 'scheduleNextDirSync':
this.scheduleNextSync();
break;
case 'cancelDirSync':
if (this.syncTimeout) {
global.clearTimeout(this.syncTimeout);
}
break;
default:
break;
}
@@ -40,8 +45,8 @@ export class MessagingMain {
}
this.windowMain.win.webContents.send('messagingService', {
command: 'checkSyncVault',
command: 'checkDirSync',
});
}, SyncInterval);
}, SyncCheckInterval);
}
}

View File

@@ -10,6 +10,7 @@ import { ImportDirectoryRequestUser } from 'jslib/models/request/importDirectory
import { ApiService } from 'jslib/abstractions/api.service';
import { CryptoFunctionService } from 'jslib/abstractions/cryptoFunction.service';
import { LogService } from 'jslib/abstractions/log.service';
import { MessagingService } from 'jslib/abstractions/messaging.service';
import { StorageService } from 'jslib/abstractions/storage.service';
import { Utils } from 'jslib/misc/utils';
@@ -28,7 +29,8 @@ export class SyncService {
private dirType: DirectoryType;
constructor(private configurationService: ConfigurationService, private logService: LogService,
private cryptoFunctionService: CryptoFunctionService, private apiService: ApiService) { }
private cryptoFunctionService: CryptoFunctionService, private apiService: ApiService,
private messagingService: MessagingService) { }
async sync(force: boolean, test: boolean): Promise<[GroupEntry[], UserEntry[]]> {
this.dirType = await this.configurationService.getDirectoryType();
@@ -46,6 +48,7 @@ export class SyncService {
const startingUserDelta = await this.configurationService.getUserDeltaToken();
const now = new Date();
this.messagingService.send('dirSyncStarted');
try {
const entries = await directoryService.getEntries(force, test);
const groups = entries[0];
@@ -56,6 +59,7 @@ export class SyncService {
}
if (test || groups == null || groups.length === 0 || users == null || users.length === 0) {
this.messagingService.send('dirSyncCompleted', { successfully: true });
return [groups, users];
}
@@ -85,12 +89,15 @@ export class SyncService {
}
}
this.messagingService.send('dirSyncCompleted', { successfully: true });
return [groups, users];
} catch (e) {
if (!test) {
await this.configurationService.saveGroupDeltaToken(startingGroupDelta);
await this.configurationService.saveUserDeltaToken(startingUserDelta);
}
this.messagingService.send('dirSyncCompleted', { successfully: false });
throw e;
}
}