1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

improving progress fetching

This commit is contained in:
Patrik J. Braun 2019-12-10 15:26:10 +01:00
parent b46cdc2e11
commit b6b8a9e203

View File

@ -7,81 +7,81 @@ import {NetworkService} from '../../model/network/network.service';
export class ScheduledTasksService { export class ScheduledTasksService {
public progress: BehaviorSubject<{ [key: string]: TaskProgressDTO }>; public progress: BehaviorSubject<{ [key: string]: TaskProgressDTO }>;
public onTaskFinish: EventEmitter<string> = new EventEmitter<string>(); public onTaskFinish: EventEmitter<string> = new EventEmitter<string>();
timer: number = null; timer: number = null;
private subscribers = 0; private subscribers = 0;
constructor(private _networkService: NetworkService) { constructor(private _networkService: NetworkService) {
this.progress = new BehaviorSubject({}); this.progress = new BehaviorSubject({});
} }
public calcTimeElapsed(progress: TaskProgressDTO) { public calcTimeElapsed(progress: TaskProgressDTO) {
if (progress) { if (progress) {
return (progress.time.current - progress.time.start); return (progress.time.current - progress.time.start);
}
} }
}
public calcTimeLeft(progress: TaskProgressDTO) { public calcTimeLeft(progress: TaskProgressDTO) {
if (progress) { if (progress) {
return (progress.time.current - progress.time.start) / progress.progress * progress.left; return (progress.time.current - progress.time.start) / progress.progress * progress.left;
}
} }
}
subscribeToProgress(): void { subscribeToProgress(): void {
this.incSubscribers(); this.incSubscribers();
} }
unsubscribeFromProgress(): void { unsubscribeFromProgress(): void {
this.decSubscribers(); this.decSubscribers();
} }
public forceUpdate() { public forceUpdate() {
return this.getProgress(); return this.getProgress();
} }
public async start(id: string, config?: any): Promise<void> { public async start(id: string, config?: any): Promise<void> {
await this._networkService.postJson('/admin/tasks/scheduled/' + id + '/start', {config: config}); await this._networkService.postJson('/admin/tasks/scheduled/' + id + '/start', {config: config});
this.forceUpdate(); this.forceUpdate();
} }
public async stop(id: string): Promise<void> { public async stop(id: string): Promise<void> {
await this._networkService.postJson('/admin/tasks/scheduled/' + id + '/stop'); await this._networkService.postJson('/admin/tasks/scheduled/' + id + '/stop');
this.forceUpdate(); this.forceUpdate();
} }
protected async getProgress() { protected async getProgress() {
const prevPrg = this.progress.value; const prevPrg = this.progress.value;
this.progress.next(await this._networkService.getJson<{ [key: string]: TaskProgressDTO }>('/admin/tasks/scheduled/progress')); this.progress.next(await this._networkService.getJson<{ [key: string]: TaskProgressDTO }>('/admin/tasks/scheduled/progress'));
for (const prg in prevPrg) { for (const prg in prevPrg) {
if (!this.progress.value.hasOwnProperty(prg)) { if (!this.progress.value.hasOwnProperty(prg)) {
this.onTaskFinish.emit(prg); this.onTaskFinish.emit(prg);
} }
}
} }
}
protected getProgressPeriodically() { protected getProgressPeriodically() {
if (this.timer != null || this.subscribers === 0) { if (this.timer != null || this.subscribers === 0) {
return; return;
}
let repeatTime = 5000;
if (Object.values(this.progress.value).length === 0) {
repeatTime = 10000;
}
this.timer = window.setTimeout(async () => {
await this.getProgress();
this.timer = null;
this.getProgressPeriodically();
}, repeatTime);
} }
let repeatTime = 5000;
if (Object.values(this.progress.value).length === 0) {
repeatTime = 10000;
}
this.timer = window.setTimeout(async () => {
this.timer = null;
this.getProgressPeriodically();
}, repeatTime);
this.getProgress().catch(console.error);
}
private incSubscribers() { private incSubscribers() {
this.subscribers++; this.subscribers++;
this.getProgressPeriodically(); this.getProgressPeriodically();
} }
private decSubscribers() { private decSubscribers() {
this.subscribers--; this.subscribers--;
} }
} }