X-Git-Url: https://gerrit.automotivelinux.org/gerrit/gitweb?a=blobdiff_plain;f=webapp%2Fsrc%2Fapp%2Fcommon%2Fsyncthing.service.ts;h=28b19a9d09f38c96e28d4aea1955c1d694da89d2;hb=40a7183f3b4aa32379aa8b4949f5f9c5e32f79f6;hp=c8b019329b919e76a0602a09b0c143a5aa1d4a71;hpb=ec7051e1da665206f594c7616ad381bfeaea333a;p=src%2Fxds%2Fxds-server.git diff --git a/webapp/src/app/common/syncthing.service.ts b/webapp/src/app/common/syncthing.service.ts index c8b0193..28b19a9 100644 --- a/webapp/src/app/common/syncthing.service.ts +++ b/webapp/src/app/common/syncthing.service.ts @@ -23,12 +23,13 @@ export interface ISyncThingStatus { ID: string; baseURL: string; connected: boolean; + connectionRetry: number; tilde: string; rawStatus: any; } // Private interfaces of Syncthing -const ISTCONFIG_VERSION = 19; +const ISTCONFIG_VERSION = 20; interface ISTFolderDeviceConfiguration { deviceID: string; @@ -115,10 +116,12 @@ export class SyncthingService { private apikey: string; private localSTID: string; private stCurVersion: number; + private connectionMaxRetry: number; private _status: ISyncThingStatus = { ID: null, baseURL: "", connected: false, + connectionRetry: 0, tilde: "", rawStatus: null, }; @@ -129,6 +132,7 @@ export class SyncthingService { this.baseRestUrl = this._status.baseURL + '/rest'; this.apikey = DEFAULT_GUI_API_KEY; this.stCurVersion = -1; + this.connectionMaxRetry = 10; // 10 seconds this.Status$ = this.statusSubject.asObservable(); } @@ -140,41 +144,28 @@ export class SyncthingService { } this._status.connected = false; this._status.ID = null; - return this.getStatus(retry); + this._status.connectionRetry = 0; + this.connectionMaxRetry = retry || 3600; // 1 hour + return this.getStatus(); } - getID(retry?: number): Observable { + getID(): Observable { if (this._status.ID != null) { return Observable.of(this._status.ID); } - return this.getStatus(retry).map(sts => sts.ID); + return this.getStatus().map(sts => sts.ID); } - getStatus(retry?: number): Observable { - - if (retry == null) { - retry = 3600; // 1 hour - } + getStatus(): Observable { return this._get('/system/status') .map((status) => { this._status.ID = status["myID"]; this._status.tilde = status["tilde"]; - this._status.connected = true; console.debug('ST local ID', this._status.ID); this._status.rawStatus = status; return this._status; - }) - .retryWhen((attempts) => { - let count = 0; - return attempts.flatMap(error => { - if (++count >= retry) { - return this._handleError(error); - } else { - return Observable.timer(count * 1000); - } - }); }); } @@ -270,12 +261,22 @@ export class SyncthingService { } private _checkAlive(): Observable { + if (this._status.connected) { + return Observable.of(true); + } + return this.http.get(this.baseRestUrl + '/system/version', this._attachAuthHeaders()) .map((r) => this._status.connected = true) - .repeatWhen - .catch((err) => { - this._status.connected = false; - throw new Error("Syncthing local daemon not responding (url=" + this._status.baseURL + ")"); + .retryWhen((attempts) => { + this._status.connectionRetry = 0; + return attempts.flatMap(error => { + this._status.connected = false; + if (++this._status.connectionRetry >= this.connectionMaxRetry) { + return Observable.throw("Syncthing local daemon not responding (url=" + this._status.baseURL + ")"); + } else { + return Observable.timer(1000); + } + }); }); } @@ -284,8 +285,7 @@ export class SyncthingService { return Observable.of(this.stCurVersion); } - return this._checkAlive() - .flatMap(() => this.http.get(this.baseRestUrl + '/system/config', this._attachAuthHeaders())) + return this.http.get(this.baseRestUrl + '/system/config', this._attachAuthHeaders()) .map((res: Response) => { let conf: ISTConfiguration = res.json(); this.stCurVersion = (conf && conf.version) || -1; @@ -305,14 +305,16 @@ export class SyncthingService { } private _get(url: string): Observable { - return this._checkAPIVersion() + return this._checkAlive() + .flatMap(() => this._checkAPIVersion()) .flatMap(() => this.http.get(this.baseRestUrl + url, this._attachAuthHeaders())) .map((res: Response) => res.json()) .catch(this._handleError); } private _post(url: string, body: any): Observable { - return this._checkAPIVersion() + return this._checkAlive() + .flatMap(() => this._checkAPIVersion()) .flatMap(() => this.http.post(this.baseRestUrl + url, JSON.stringify(body), this._attachAuthHeaders())) .map((res: Response) => { if (res && res.status && res.status === 200) {