Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

manager: implement timed replication (fixes #7651) #7656

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/app/manager-dashboard/manager-sync.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<button mat-button (click)="getReplicators()" i18n><mat-icon>refresh</mat-icon>Refresh</button>
</mat-toolbar>
<mat-list>
<mat-list-item *ngFor="let rep of replicators" [ngClass]="{'warn-text-color': rep?.state === 'error' || rep?.state === 'crashing' || rep?.state === 'failed'}">
<mat-list-item *ngFor="let rep of replicators$ | async" [ngClass]="{'warn-text-color': rep?.state === 'error' || rep?.state === 'crashing' || rep?.state === 'failed'}">
<h3 matLine>
<mat-icon *ngIf="rep?.continuous; else singleRep">repeat</mat-icon>
<ng-template #singleRep><mat-icon>repeat_one</mat-icon></ng-template>
Expand Down
63 changes: 54 additions & 9 deletions src/app/manager-dashboard/manager-sync.component.ts
Original file line number Diff line number Diff line change
@@ -1,36 +1,81 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit, OnDestroy } from '@angular/core';
import { CouchService } from '../shared/couchdb.service';
import { DialogsLoadingService } from '../shared/dialogs/dialogs-loading.service';
import { forkJoin } from 'rxjs';
import { forkJoin, interval, Subscription, BehaviorSubject } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { PlanetMessageService } from '../shared/planet-message.service';

@Component({
templateUrl: './manager-sync.component.html'
})

export class ManagerSyncComponent implements OnInit {
export class ManagerSyncComponent implements OnInit, OnDestroy {

replicators = [];
private syncSubscription: Subscription;
private replicatorsSubject = new BehaviorSubject<any[]>([]);
replicators$ = this.replicatorsSubject.asObservable();

private readonly SYNC_INTERVAL = 3600000;
private readonly LAST_SYNC_KEY = 'lastSyncTime';

constructor(
private couchService: CouchService,
private planetMessageService: PlanetMessageService,
private dialogsLoadingService: DialogsLoadingService
) {}
) { }

ngOnInit() {
this.getReplicators();
private timedSync() {
const lastSync = parseInt(localStorage.getItem(this.LAST_SYNC_KEY) || '0', 10);
const now = Date.now();

if (now - lastSync >= this.SYNC_INTERVAL) {
this.syncReplicators();
}

this.syncSubscription = interval(this.SYNC_INTERVAL)
.subscribe(() => {
this.syncReplicators();
});
}

getReplicators() {
private syncReplicators() {
this.dialogsLoadingService.start();
forkJoin([
this.couchService.get('_scheduler/docs'),
this.couchService.findAll('_replicator')
])
.pipe(catchError(
error => {
this.planetMessageService.showMessage($localize`Error during synchronization ${error}`);
this.dialogsLoadingService.stop();
throw error;
})
)
.subscribe(([ reps, data ]) => {
const jobs = reps.docs.filter(replicator => replicator.database === '_replicator');
this.replicators = data.map((rep: any) => ({ ...rep, ...jobs.find(n => n.doc_id === rep._id) }));
const updatedReplicators = data.map((rep: any) => ({
...rep,
...jobs.find(n => n.doc_id === rep._id)
}));
this.replicatorsSubject.next(updatedReplicators);
this.dialogsLoadingService.stop();
localStorage.setItem(this.LAST_SYNC_KEY, Date.now().toString());
});
}

ngOnInit() {
this.timedSync();
this.getReplicators();
}

getReplicators(): BehaviorSubject<any[]> {
return this.replicatorsSubject;
}

ngOnDestroy() {
if (this.syncSubscription) {
this.syncSubscription.unsubscribe();
}
}

}
Loading