From 36b0a216d6eb36782b405b3f09b7007b373d43db Mon Sep 17 00:00:00 2001 From: "Patrik J. Braun" Date: Thu, 5 Jan 2023 19:50:12 +0100 Subject: [PATCH] Fixing job running in settings #569 --- src/backend/model/jobs/jobs/FileJob.ts | 2 +- src/backend/model/jobs/jobs/Job.ts | 28 ++++++----- .../model/jobs/jobs/ThumbnailGenerationJob.ts | 15 +++--- .../app/ui/settings/scheduled-jobs.service.ts | 32 +++++++++++- .../app/ui/settings/settings.service.ts | 10 +--- .../settings/template/template.component.html | 50 +++++++++++++++---- .../settings/template/template.component.ts | 7 ++- .../button/job-button.settings.component.ts | 44 +++++++++------- .../settings/workflow/workflow.component.html | 6 +-- .../settings/workflow/workflow.component.ts | 18 ++----- 10 files changed, 136 insertions(+), 76 deletions(-) diff --git a/src/backend/model/jobs/jobs/FileJob.ts b/src/backend/model/jobs/jobs/FileJob.ts index f63f67e0..bb2c4c99 100644 --- a/src/backend/model/jobs/jobs/FileJob.ts +++ b/src/backend/model/jobs/jobs/FileJob.ts @@ -21,7 +21,7 @@ const LOG_TAG = '[FileJob]'; /** * Abstract class for thumbnail creation, file deleting etc. */ -export abstract class FileJob extends Job { +export abstract class FileJob extends Job { public readonly ConfigTemplate: ConfigTemplateEntry[] = []; directoryQueue: string[] = []; fileQueue: string[] = []; diff --git a/src/backend/model/jobs/jobs/Job.ts b/src/backend/model/jobs/jobs/Job.ts index 5538c4ec..adf77084 100644 --- a/src/backend/model/jobs/jobs/Job.ts +++ b/src/backend/model/jobs/jobs/Job.ts @@ -1,20 +1,16 @@ -import { Logger } from '../../../Logger'; -import { IJob } from './IJob'; -import { - ConfigTemplateEntry, - JobDTO, - JobDTOUtils, -} from '../../../../common/entities/job/JobDTO'; -import { JobProgress } from './JobProgress'; -import { IJobListener } from './IJobListener'; -import { JobProgressStates } from '../../../../common/entities/job/JobProgressDTO'; +import {Logger} from '../../../Logger'; +import {IJob} from './IJob'; +import {ConfigTemplateEntry, JobDTO, JobDTOUtils,} from '../../../../common/entities/job/JobDTO'; +import {JobProgress} from './JobProgress'; +import {IJobListener} from './IJobListener'; +import {JobProgressStates} from '../../../../common/entities/job/JobProgressDTO'; declare const process: any; declare const global: any; const LOG_TAG = '[JOB]'; -export abstract class Job implements IJob { +export abstract class Job = Record> implements IJob { public allowParallelRun: boolean = null; protected progress: JobProgress = null; protected config: T; @@ -57,7 +53,15 @@ export abstract class Job implements IJob { ); this.soloRun = soloRun; this.allowParallelRun = allowParallelRun; - this.config = config; + this.config = {} as T; + if (this.ConfigTemplate) { + this.ConfigTemplate.forEach(ct => (this.config as any)[ct.id] = ct.defaultValue); + } + if (config) { + for (const key of Object.keys(config)) { + (this.config as any)[key] = config[key]; + } + } this.progress = new JobProgress( this.Name, JobDTOUtils.getHashName(this.Name, this.config) diff --git a/src/backend/model/jobs/jobs/ThumbnailGenerationJob.ts b/src/backend/model/jobs/jobs/ThumbnailGenerationJob.ts index 4a16c117..e1cd3d48 100644 --- a/src/backend/model/jobs/jobs/ThumbnailGenerationJob.ts +++ b/src/backend/model/jobs/jobs/ThumbnailGenerationJob.ts @@ -8,8 +8,8 @@ import {FileDTO} from '../../../../common/entities/FileDTO'; import {backendTexts} from '../../../../common/BackendTexts'; export class ThumbnailGenerationJob extends FileJob<{ - sizes: number[]; - indexedOnly: boolean; + sizes?: number[]; + indexedOnly?: boolean; }> { public readonly Name = DefaultsJobs[DefaultsJobs['Thumbnail Generation']]; @@ -29,16 +29,13 @@ export class ThumbnailGenerationJob extends FileJob<{ } start( - config: { sizes: number[]; indexedOnly: boolean }, + config: { sizes?: number[]; indexedOnly?: boolean }, soloRun = false, allowParallelRun = false ): Promise { - if (!config.sizes || !Array.isArray(config.sizes) || config.sizes.length === 0) { - throw new Error( - 'unknown thumbnails sizes: ' + - config.sizes + - '. It should be an array from:' + Config.Media.Thumbnail.thumbnailSizes - ); + if (!config || !config.sizes || !Array.isArray(config.sizes) || config.sizes.length === 0) { + config = config || {}; + config.sizes = this.ConfigTemplate.find(ct => ct.id == 'sizes').defaultValue; } for (const item of config.sizes) { if (Config.Media.Thumbnail.thumbnailSizes.indexOf(item) === -1) { diff --git a/src/frontend/app/ui/settings/scheduled-jobs.service.ts b/src/frontend/app/ui/settings/scheduled-jobs.service.ts index bb73249b..96fcb826 100644 --- a/src/frontend/app/ui/settings/scheduled-jobs.service.ts +++ b/src/frontend/app/ui/settings/scheduled-jobs.service.ts @@ -3,7 +3,7 @@ import {BehaviorSubject} from 'rxjs'; import {JobProgressDTO, JobProgressStates,} from '../../../../common/entities/job/JobProgressDTO'; import {NetworkService} from '../../model/network/network.service'; import {JobScheduleDTO} from '../../../../common/entities/job/JobScheduleDTO'; -import {JobDTOUtils} from '../../../../common/entities/job/JobDTO'; +import {ConfigTemplateEntry, JobDTO, JobDTOUtils} from '../../../../common/entities/job/JobDTO'; import {BackendtextService} from '../../model/backendtext.service'; import {NotificationService} from '../../model/notification.service'; @@ -12,6 +12,7 @@ export class ScheduledJobsService { public progress: BehaviorSubject>; public onJobFinish: EventEmitter = new EventEmitter(); timer: number = null; + public availableJobs: BehaviorSubject; public jobStartingStopping: { [key: string]: boolean } = {}; private subscribers = 0; @@ -21,6 +22,35 @@ export class ScheduledJobsService { private backendTextService: BackendtextService ) { this.progress = new BehaviorSubject({}); + this.availableJobs = new BehaviorSubject([]); + } + + + public async getAvailableJobs(): Promise { + this.availableJobs.next( + await this.networkService.getJson('/admin/jobs/available') + ); + } + + public getConfigTemplate(JobName: string): ConfigTemplateEntry[] { + const job = this.availableJobs.value.find( + (t) => t.Name === JobName + ); + if (job && job.ConfigTemplate && job.ConfigTemplate.length > 0) { + return job.ConfigTemplate; + } + return null; + } + + public getDefaultConfig(jobName: string): Record { + + const ct = this.getConfigTemplate(jobName); + if (!ct) { + return null; + } + const config = {} as Record; + ct.forEach(c => config[c.id] = c.defaultValue); + return config; } getProgress(schedule: JobScheduleDTO): JobProgressDTO { diff --git a/src/frontend/app/ui/settings/settings.service.ts b/src/frontend/app/ui/settings/settings.service.ts index 1af0feb9..458f2048 100644 --- a/src/frontend/app/ui/settings/settings.service.ts +++ b/src/frontend/app/ui/settings/settings.service.ts @@ -7,7 +7,7 @@ import {WebConfigClassBuilder} from 'typeconfig/src/decorators/builders/WebConfi import {ConfigPriority, TAGS} from '../../../../common/config/public/ClientConfig'; import {CookieNames} from '../../../../common/CookieNames'; import {CookieService} from 'ngx-cookie-service'; -import {DefaultsJobs, JobDTO} from '../../../../common/entities/job/JobDTO'; +import {DefaultsJobs} from '../../../../common/entities/job/JobDTO'; import {StatisticDTO} from '../../../../common/entities/settings/StatisticDTO'; import {ScheduledJobsService} from './scheduled-jobs.service'; import {IWebConfigClassPrivate} from '../../../../../node_modules/typeconfig/src/decorators/class/IWebConfigClass'; @@ -17,14 +17,12 @@ export class SettingsService { public configPriority = ConfigPriority.basic; public settings: BehaviorSubject & WebConfig>; private fetchingSettings = false; - public availableJobs: BehaviorSubject; public statistic: BehaviorSubject; constructor(private networkService: NetworkService, private jobsService: ScheduledJobsService, private cookieService: CookieService) { this.statistic = new BehaviorSubject(null); - this.availableJobs = new BehaviorSubject([]); this.settings = new BehaviorSubject & WebConfig>(WebConfigClassBuilder.attachPrivateInterface(new WebConfig())); this.getSettings().catch(console.error); @@ -48,12 +46,6 @@ export class SettingsService { } - public async getAvailableJobs(): Promise { - this.availableJobs.next( - await this.networkService.getJson('/admin/jobs/available') - ); - } - public async getSettings(): Promise { if (this.fetchingSettings === true) { return; diff --git a/src/frontend/app/ui/settings/template/template.component.html b/src/frontend/app/ui/settings/template/template.component.html index 82e93bda..86022286 100644 --- a/src/frontend/app/ui/settings/template/template.component.html +++ b/src/frontend/app/ui/settings/template/template.component.html @@ -23,10 +23,8 @@
- - @@ -35,6 +33,11 @@ {{Name}} config is not supported with these settings.
+
+ +
+
+ +
+
+
+ + +
- + @@ -78,8 +96,18 @@ -
- +
+ + +
+ + +
+ @@ -93,13 +121,17 @@ [jobName]="job.job"> - +
+
+ +
+ + *ngIf="getProgress(job) && !job.hideProgress && (!job.relevant || job.relevant(settingsService.settings | async))">
+ [progress]="getProgress(job)">
diff --git a/src/frontend/app/ui/settings/template/template.component.ts b/src/frontend/app/ui/settings/template/template.component.ts index ef72e5c5..98a2710c 100644 --- a/src/frontend/app/ui/settings/template/template.component.ts +++ b/src/frontend/app/ui/settings/template/template.component.ts @@ -300,7 +300,10 @@ export class TemplateComponent implements OnInit, OnChanges, OnDestroy, ISetting return states.keys; } - getProgress(jobName: string): JobProgressDTO { - return this.jobsService.progress.value[JobDTOUtils.getHashName(jobName)]; + getProgress(uiJob: { job: string, config?: any }): JobProgressDTO { + if (!uiJob.config) { + uiJob.config = this.jobsService.getDefaultConfig(uiJob.job); + } + return this.jobsService.progress.value[JobDTOUtils.getHashName(uiJob.job, uiJob.config || {})]; } } diff --git a/src/frontend/app/ui/settings/workflow/button/job-button.settings.component.ts b/src/frontend/app/ui/settings/workflow/button/job-button.settings.component.ts index b63804e9..8ef38bcb 100644 --- a/src/frontend/app/ui/settings/workflow/button/job-button.settings.component.ts +++ b/src/frontend/app/ui/settings/workflow/button/job-button.settings.component.ts @@ -1,13 +1,10 @@ -import { Component, EventEmitter, Input, Output } from '@angular/core'; -import { - JobProgressDTO, - JobProgressStates, -} from '../../../../../../common/entities/job/JobProgressDTO'; -import { ErrorDTO } from '../../../../../../common/entities/Error'; -import { ScheduledJobsService } from '../../scheduled-jobs.service'; -import { NotificationService } from '../../../../model/notification.service'; -import { JobDTOUtils } from '../../../../../../common/entities/job/JobDTO'; -import { BackendtextService } from '../../../../model/backendtext.service'; +import {Component, EventEmitter, Input, Output} from '@angular/core'; +import {JobProgressDTO, JobProgressStates,} from '../../../../../../common/entities/job/JobProgressDTO'; +import {ErrorDTO} from '../../../../../../common/entities/Error'; +import {ScheduledJobsService} from '../../scheduled-jobs.service'; +import {NotificationService} from '../../../../model/notification.service'; +import {JobDTOUtils} from '../../../../../../common/entities/job/JobDTO'; +import {BackendtextService} from '../../../../model/backendtext.service'; @Component({ selector: 'app-settings-job-button', @@ -16,7 +13,7 @@ import { BackendtextService } from '../../../../model/backendtext.service'; }) export class JobButtonComponent { @Input() jobName: string; - @Input() config: any = {}; + @Input() config: any; @Input() shortName = false; @Input() disabled = false; @Input() soloRun = false; @@ -29,7 +26,18 @@ export class JobButtonComponent { private notification: NotificationService, public jobsService: ScheduledJobsService, public backendTextService: BackendtextService - ) {} + ) { + } + + private populateConfig() { + if (this.config) { + return; + } + const c = this.jobsService.getDefaultConfig(this.jobName); // can return with null + if (c) { + this.config = c; + } + } public get Running(): boolean { return ( @@ -40,13 +48,15 @@ export class JobButtonComponent { } get Progress(): JobProgressDTO { + this.populateConfig(); return this.jobsService.progress.value[ JobDTOUtils.getHashName(this.jobName, this.config) - ]; + ]; } public async start(): Promise { this.jobError.emit(''); + this.populateConfig(); try { await this.jobsService.start( this.jobName, @@ -56,8 +66,8 @@ export class JobButtonComponent { ); this.notification.success( $localize`Job started` + - ': ' + - this.backendTextService.getJobName(this.jobName) + ': ' + + this.backendTextService.getJobName(this.jobName) ); return true; } catch (err) { @@ -76,8 +86,8 @@ export class JobButtonComponent { await this.jobsService.stop(this.jobName); this.notification.info( $localize`Stopping job` + - ': ' + - this.backendTextService.getJobName(this.jobName) + ': ' + + this.backendTextService.getJobName(this.jobName) ); return true; } catch (err) { diff --git a/src/frontend/app/ui/settings/workflow/workflow.component.html b/src/frontend/app/ui/settings/workflow/workflow.component.html index 886ea4ca..0872e129 100644 --- a/src/frontend/app/ui/settings/workflow/workflow.component.html +++ b/src/frontend/app/ui/settings/workflow/workflow.component.html @@ -146,9 +146,9 @@
- +
-
+