diff --git a/webapp/src/app/modules/jobs/jobs.component.ts b/webapp/src/app/modules/jobs/jobs.component.ts index a76a8236..f7bdb907 100644 --- a/webapp/src/app/modules/jobs/jobs.component.ts +++ b/webapp/src/app/modules/jobs/jobs.component.ts @@ -17,7 +17,15 @@ import { MatIconModule } from '@angular/material/icon'; import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; import { MatSnackBar, MatSnackBarModule } from '@angular/material/snack-bar'; import { MatTooltipModule } from '@angular/material/tooltip'; -import { EMPTY, Subject, catchError, merge, switchMap, timer } from 'rxjs'; +import { + EMPTY, + Subject, + Subscription, + catchError, + merge, + switchMap, + timer, +} from 'rxjs'; import { BackgroundJobsService, BackgroundJobTask, @@ -33,6 +41,7 @@ import { formatScheduleCron, formatUtcDateAsLocal, statusMeta, + toUtcMs, } from './jobs.utils'; import { TaskScheduleDialogComponent, @@ -49,6 +58,8 @@ import { TaskEditDialogData, } from './task-edit-dialog/task-edit-dialog.component'; +const MAX_IDLE_POLL_WAIT_MS = 60000; + @Component({ selector: 'app-jobs', imports: [ @@ -104,13 +115,16 @@ export class JobsComponent implements OnInit { ); private readonly taskRefreshTrigger = new Subject(); + private readonly pollNow = new Subject(); private readonly pendingStopTaskIds = new Set(); + private pollDelaySub?: Subscription; + private tickSub?: Subscription; ngOnInit(): void { this.tasks.set(this.tasksInput()); this.dockerHealth.set(this.dockerHealthInput()); - merge(timer(5000, 5000), this.taskRefreshTrigger) + merge(this.pollNow, this.taskRefreshTrigger) .pipe( switchMap(() => this.backgroundJobsService @@ -126,11 +140,12 @@ export class JobsComponent implements OnInit { ), takeUntilDestroyed(this.destroyRef), ) - .subscribe((tasks) => this.setTasksPreservingPendingStops(tasks)); + .subscribe((tasks) => { + this.setTasksPreservingPendingStops(tasks); + this.syncBackgroundWork(); + }); - timer(1000, 1000) - .pipe(takeUntilDestroyed(this.destroyRef)) - .subscribe(() => this.nowMs.set(Date.now())); + this.syncBackgroundWork(); this.taskColumns = [ { @@ -171,6 +186,55 @@ export class JobsComponent implements OnInit { this.taskRefreshTrigger.next(); } + private hasActiveTask(): boolean { + return this.tasks().some( + (task) => task.status === 'running' || task.status === 'stopping', + ); + } + + private syncBackgroundWork(): void { + const active = this.hasActiveTask(); + + if (active && !this.tickSub) { + this.tickSub = timer(1000, 1000) + .pipe(takeUntilDestroyed(this.destroyRef)) + .subscribe(() => this.nowMs.set(Date.now())); + } else if (!active && this.tickSub) { + this.tickSub.unsubscribe(); + this.tickSub = undefined; + } + + this.pollDelaySub?.unsubscribe(); + this.pollDelaySub = undefined; + const delayMs = this.nextPollDelayMs(active); + if (delayMs != null) { + this.pollDelaySub = timer(delayMs) + .pipe(takeUntilDestroyed(this.destroyRef)) + .subscribe(() => this.pollNow.next()); + } + } + + private nextPollDelayMs(active: boolean): number | null { + if (active) return 5000; + + const nextRun = this.earliestUpcomingRun(); + if (nextRun == null) return null; + + const untilRun = nextRun - Date.now(); + if (untilRun <= 0) return 3000; + return Math.min(untilRun + 3000, MAX_IDLE_POLL_WAIT_MS); + } + + private earliestUpcomingRun(): number | null { + let earliest: number | null = null; + for (const task of this.tasks()) { + if (!task.enabled || !task.schedule || !task.next_run_at) continue; + const ts = toUtcMs(task.next_run_at); + if (earliest == null || ts < earliest) earliest = ts; + } + return earliest; + } + protected refreshDockerHealth(): void { this.dockerHealthLoading.set(true); this.backgroundJobsService.getBackgroundJobsDockerHealth().subscribe({ @@ -389,6 +453,7 @@ export class JobsComponent implements OnInit { this.tasks.update((current) => current.map((task) => (task.id === id ? updater(task) : task)), ); + this.syncBackgroundWork(); } private deleteTask(task: BackgroundJobTask): void { diff --git a/webapp/src/app/modules/jobs/jobs.utils.ts b/webapp/src/app/modules/jobs/jobs.utils.ts index b7151c90..23bcfc58 100644 --- a/webapp/src/app/modules/jobs/jobs.utils.ts +++ b/webapp/src/app/modules/jobs/jobs.utils.ts @@ -43,7 +43,7 @@ function appendUtcZ(value: string): string { return /(?:Z|[+-]\d{2}:\d{2})$/.test(value) ? value : `${value}Z`; } -function toUtcMs(value: string): number { +export function toUtcMs(value: string): number { return new Date(appendUtcZ(value)).getTime(); }