Implement healthcheck service
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good
All checks were successful
FarmMaps.Develop/FarmMapsLib/pipeline/head This commit looks good
This commit is contained in:
parent
e525227865
commit
d494a7c7d6
@ -10,6 +10,7 @@ import { IEventMessage } from '../../models/event.message';
|
|||||||
import { IListItem} from '../../models/list.item';
|
import { IListItem} from '../../models/list.item';
|
||||||
import { EventService } from '../../services/event.service';
|
import { EventService } from '../../services/event.service';
|
||||||
import * as commonActions from '../../actions/app-common.actions';
|
import * as commonActions from '../../actions/app-common.actions';
|
||||||
|
import { HealthCheckService } from '../../services/healthcheck.service';
|
||||||
|
|
||||||
import * as appReducers from '../../reducers/app-common.reducer';
|
import * as appReducers from '../../reducers/app-common.reducer';
|
||||||
|
|
||||||
@ -46,26 +47,18 @@ export class AppComponent implements OnInit, OnDestroy {
|
|||||||
private meta: Meta,
|
private meta: Meta,
|
||||||
private store: Store<appReducers.State>,
|
private store: Store<appReducers.State>,
|
||||||
private eventService: EventService,
|
private eventService: EventService,
|
||||||
|
private healthCheckService: HealthCheckService
|
||||||
) {
|
) {
|
||||||
|
|
||||||
var connection = navigator['connection'] || navigator['mozConnection'] || navigator['webkitConnection'];
|
//check health every 30 seconds
|
||||||
if(connection) {
|
this.healthCheckService.check(30000).subscribe((online) => {
|
||||||
this.setOnOffline(connection);
|
if(online) {
|
||||||
connection.addEventListener('change', () => {
|
|
||||||
this.setOnOffline(connection);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
setOnOffline(connection:any) {
|
|
||||||
console.debug(connection);
|
|
||||||
if(connection.downlink == 0) {
|
|
||||||
this.store.dispatch(new commonActions.Offline());
|
|
||||||
} else {
|
|
||||||
this.store.dispatch(new commonActions.Online());
|
this.store.dispatch(new commonActions.Online());
|
||||||
}
|
} else {
|
||||||
|
this.store.dispatch(new commonActions.Offline());
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
getActionFromEvent(event: IEventMessage): Action {
|
getActionFromEvent(event: IEventMessage): Action {
|
||||||
|
@ -165,18 +165,21 @@ export class AppCommonEffects {
|
|||||||
@Effect({ dispatch: false })
|
@Effect({ dispatch: false })
|
||||||
online$: Observable<Action> = this.actions$.pipe(
|
online$: Observable<Action> = this.actions$.pipe(
|
||||||
ofType(appCommonActions.ONLINE),
|
ofType(appCommonActions.ONLINE),
|
||||||
map((action) => {
|
withLatestFrom(this.store$.select(appCommonReducers.SelectGetIsOnline)),
|
||||||
console.debug("Online: Check token");
|
switchMap(([action,isOnline]) => {
|
||||||
if(!this.oauthService$.hasValidAccessToken()) {
|
if(!isOnline) {
|
||||||
console.debug("No valid token, try to refresh");
|
console.debug("Online: Check token");
|
||||||
if(this.oauthService$.getRefreshToken() != null ) {
|
if(!this.oauthService$.hasValidAccessToken()) {
|
||||||
console.debug("We have a refresh token");
|
console.debug("No valid token, try to refresh");
|
||||||
this.oauthService$.refreshToken().then(() => {
|
if(this.oauthService$.getRefreshToken() != null ) {
|
||||||
this.store$.dispatch(new appCommonActions.InitUser());
|
console.debug("We have a refresh token");
|
||||||
});
|
this.oauthService$.refreshToken().then(() => {
|
||||||
}
|
this.store$.dispatch(new appCommonActions.InitUser());
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return null;
|
return of(undefined);
|
||||||
}));
|
}));
|
||||||
|
|
||||||
|
|
||||||
|
30
projects/common/src/fm/services/healthcheck.service.ts
Normal file
30
projects/common/src/fm/services/healthcheck.service.ts
Normal file
@ -0,0 +1,30 @@
|
|||||||
|
import { Injectable } from '@angular/core';
|
||||||
|
import { Observable,BehaviorSubject,of } from 'rxjs';
|
||||||
|
import { map, catchError } from 'rxjs/operators';
|
||||||
|
import { HttpClient, HttpXhrBackend } from "@angular/common/http";
|
||||||
|
import { AppConfig } from "../shared/app.config";
|
||||||
|
|
||||||
|
@Injectable({
|
||||||
|
providedIn: 'root',
|
||||||
|
})
|
||||||
|
export class HealthCheckService {
|
||||||
|
private httpClient: HttpClient;
|
||||||
|
|
||||||
|
constructor(xhrBackend: HttpXhrBackend, public appConfig: AppConfig) {
|
||||||
|
this.httpClient = new HttpClient(xhrBackend);
|
||||||
|
}
|
||||||
|
|
||||||
|
ApiEndpoint() {
|
||||||
|
return this.appConfig.getConfig("apiEndPoint");
|
||||||
|
}
|
||||||
|
|
||||||
|
check(interval:number): Observable<boolean> {
|
||||||
|
let retval = new BehaviorSubject<boolean>(false);
|
||||||
|
setInterval(() => {
|
||||||
|
this.httpClient.get(`${this.ApiEndpoint()}/api/v1/healthcheck`).pipe(map(() => true),catchError((error) => of(false))).toPromise().then((status) => {
|
||||||
|
retval.next(status);
|
||||||
|
});
|
||||||
|
},interval);
|
||||||
|
return retval;
|
||||||
|
}
|
||||||
|
}
|
@ -1,47 +1,45 @@
|
|||||||
import { Injectable, Injector, Inject } from '@angular/core';
|
import { Injectable, Injector, Inject } from '@angular/core';
|
||||||
import { DOCUMENT } from '@angular/common'
|
import { DOCUMENT } from '@angular/common'
|
||||||
import { AppConfig } from "./app.config";
|
import { AppConfig } from "./app.config";
|
||||||
import {
|
import {
|
||||||
HttpRequest,
|
HttpRequest,
|
||||||
HttpHandler,
|
HttpHandler,
|
||||||
HttpEvent,
|
HttpEvent,
|
||||||
HttpInterceptor
|
HttpInterceptor
|
||||||
} from '@angular/common/http';
|
} from '@angular/common/http';
|
||||||
import { OAuthService } from 'angular-oauth2-oidc';
|
import { OAuthService } from 'angular-oauth2-oidc';
|
||||||
import { Observable } from 'rxjs';
|
import { Observable } from 'rxjs';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AccessTokenInterceptor implements HttpInterceptor {
|
export class AccessTokenInterceptor implements HttpInterceptor {
|
||||||
private oauthService: OAuthService = null;
|
private oauthService: OAuthService = null;
|
||||||
private audience: string[] = [];
|
private audience: string[] = [];
|
||||||
private base: string;
|
private base: string;
|
||||||
|
|
||||||
constructor(private injector: Injector, private appConfig: AppConfig, @Inject(DOCUMENT) private document: any) {
|
constructor(private injector: Injector, private appConfig: AppConfig, @Inject(DOCUMENT) private document: any) {
|
||||||
this.base = document.location.href;
|
this.base = document.location.href;
|
||||||
}
|
}
|
||||||
|
|
||||||
hasAudience(url: string): boolean {
|
hasAudience(url: string): boolean {
|
||||||
let u = new URL(url,this.base);
|
let u = new URL(url,this.base);
|
||||||
for (let audience of this.audience) {
|
for (let audience of this.audience) {
|
||||||
if (u.href.startsWith(audience)) return true;
|
if (u.href.startsWith(audience)) return true;
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
|
||||||
if (this.oauthService && this.hasAudience(request.url)) {
|
if (this.oauthService && this.hasAudience(request.url)) {
|
||||||
request = request.clone({
|
request = request.clone({
|
||||||
setHeaders: {
|
setHeaders: {
|
||||||
Authorization: `Bearer ${this.oauthService.getAccessToken()}`
|
Authorization: `Bearer ${this.oauthService.getAccessToken()}`
|
||||||
}
|
}
|
||||||
// Please uncomment the next line if you need to connect to the backend running in Docker.
|
});
|
||||||
//, url: `http://localhost:8082${request.url}`
|
} else {
|
||||||
});
|
this.oauthService = this.injector.get(OAuthService, null);
|
||||||
} else {
|
if(this.oauthService && this.oauthService.issuer) this.audience = (this.appConfig.getConfig("audience") as string).split(",");
|
||||||
this.oauthService = this.injector.get(OAuthService, null);
|
}
|
||||||
if(this.oauthService && this.oauthService.issuer) this.audience = (this.appConfig.getConfig("audience") as string).split(",");
|
return next.handle(request);
|
||||||
}
|
}
|
||||||
return next.handle(request);
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@ -6,4 +6,9 @@ import { Component } from '@angular/core';
|
|||||||
})
|
})
|
||||||
export class AppRootComponent {
|
export class AppRootComponent {
|
||||||
title = 'FarmMaps';
|
title = 'FarmMaps';
|
||||||
|
constructor() {
|
||||||
|
|
||||||
|
window.addEventListener('online', () => console.log('came online'));
|
||||||
|
window.addEventListener('offline', () => console.log('came offline'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user