From: Tiago Melo Date: Fri, 11 Jan 2019 12:08:01 +0000 (-0100) Subject: mgr/dashboard: Fix summary refresh call stack X-Git-Tag: v14.1.0~311^2 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=3eab11752072f5c6cccba4740ed6f6b52ffd3ff1;p=ceph.git mgr/dashboard: Fix summary refresh call stack Currently each time we created a task it would call the summary refresh method. Since that method was a recursive method, overtime we would get N calls to the refresh each 5 seconds. Signed-off-by: Tiago Melo --- diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts index 86ea4e108976..d4439416d987 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.spec.ts @@ -47,6 +47,7 @@ describe('SummaryService', () => { }); it('should call refresh', fakeAsync(() => { + summaryService.enablePolling(); authStorageService.set('foobar', undefined, undefined); const calledWith = []; summaryService.subscribe((data) => { @@ -57,10 +58,9 @@ describe('SummaryService', () => { expect(calledWith).toEqual([summary, summary]); tick(10000); expect(calledWith.length).toEqual(4); - // In order to not trigger setTimeout again, + // In order to not trigger setInterval again, // which would raise 'Error: 1 timer(s) still in the queue.' - spyOn(summaryService, 'refresh').and.callFake(() => true); - tick(5000); + window.clearInterval(summaryService.polling); })); describe('Should test methods after first refresh', () => { diff --git a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.ts b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.ts index a4397f85cba1..0f81659c7c7b 100644 --- a/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.ts +++ b/src/pybind/mgr/dashboard/frontend/src/app/shared/services/summary.service.ts @@ -18,19 +18,17 @@ export class SummaryService { // Observable streams summaryData$ = this.summaryDataSource.asObservable(); + polling: number; + constructor(private http: HttpClient, private router: Router, private ngZone: NgZone) { - this.refresh(); + this.enablePolling(); } - refresh() { - if (this.router.url !== '/login') { - this.http.get('api/summary').subscribe((data) => { - this.summaryDataSource.next(data); - }); - } + enablePolling() { + this.refresh(); this.ngZone.runOutsideAngular(() => { - setTimeout(() => { + this.polling = window.setInterval(() => { this.ngZone.run(() => { this.refresh(); }); @@ -38,6 +36,14 @@ export class SummaryService { }); } + refresh() { + if (this.router.url !== '/login') { + this.http.get('api/summary').subscribe((data) => { + this.summaryDataSource.next(data); + }); + } + } + /** * Returns the current value of summaryData */