refactor(jobs): simplify create-dialog to free-text command
This commit is contained in:
@@ -7,6 +7,14 @@
|
||||
ausführen oder per Cron-Zeitplan automatisieren.
|
||||
</p>
|
||||
</div>
|
||||
<button
|
||||
mat-flat-button
|
||||
color="primary"
|
||||
type="button"
|
||||
(click)="openCreateDialog()">
|
||||
<mat-icon>add</mat-icon>
|
||||
Neuer Job
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<mat-card appearance="outlined" class="docker-health-card">
|
||||
|
||||
@@ -41,6 +41,7 @@ import {
|
||||
JobLogsDialogComponent,
|
||||
JobLogsDialogData,
|
||||
} from './job-logs-dialog/job-logs-dialog.component';
|
||||
import { TaskCreateDialogComponent } from './task-create-dialog/task-create-dialog.component';
|
||||
|
||||
@Component({
|
||||
selector: 'app-jobs',
|
||||
@@ -255,6 +256,29 @@ export class JobsComponent implements OnInit {
|
||||
);
|
||||
}
|
||||
|
||||
protected openCreateDialog(): void {
|
||||
const dialogRef = this.dialog.open<
|
||||
TaskCreateDialogComponent,
|
||||
void,
|
||||
BackgroundJobTask | undefined
|
||||
>(TaskCreateDialogComponent, {
|
||||
width: '560px',
|
||||
maxWidth: 'calc(100vw - 32px)',
|
||||
});
|
||||
|
||||
dialogRef.afterClosed().subscribe((task) => {
|
||||
if (!task) return;
|
||||
|
||||
this.tasks.update((tasks) => [...tasks, task]);
|
||||
this.snackBar.open(`„${task.name}“ wurde angelegt.`, 'OK', {
|
||||
duration: 3000,
|
||||
horizontalPosition: 'center',
|
||||
verticalPosition: 'bottom',
|
||||
});
|
||||
this.refreshTasks();
|
||||
});
|
||||
}
|
||||
|
||||
protected openScheduleDialog(task: BackgroundJobTask): void {
|
||||
const dialogRef = this.dialog.open<
|
||||
TaskScheduleDialogComponent,
|
||||
|
||||
@@ -0,0 +1,74 @@
|
||||
<h2 mat-dialog-title>Neuer Job</h2>
|
||||
|
||||
<mat-dialog-content>
|
||||
<form class="create-form" [formGroup]="form">
|
||||
<mat-form-field appearance="outline">
|
||||
<mat-label>Name</mat-label>
|
||||
<input matInput formControlName="name" />
|
||||
@if (form.controls.name.invalid) {
|
||||
<mat-error>Bitte einen Namen eingeben.</mat-error>
|
||||
}
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field appearance="outline">
|
||||
<mat-label>Befehl (optional)</mat-label>
|
||||
<input matInput formControlName="command" />
|
||||
<mat-hint
|
||||
>Der Befehl, den der Container ausführt. Leer = Standard-Entrypoint des
|
||||
Images.</mat-hint
|
||||
>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field appearance="outline">
|
||||
<mat-label>Image (optional)</mat-label>
|
||||
<input matInput formControlName="image" />
|
||||
<mat-hint
|
||||
>Docker-Image (Standard:
|
||||
docker.nes.uni-due.de/inse/background-jobs:latest).</mat-hint
|
||||
>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field appearance="outline">
|
||||
<mat-label>Beschreibung</mat-label>
|
||||
<textarea matInput rows="2" formControlName="description"></textarea>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field appearance="outline">
|
||||
<mat-label>Ziel-Index (optional)</mat-label>
|
||||
<input matInput formControlName="targetIndex" />
|
||||
<mat-hint
|
||||
>Elasticsearch-Index, in den der Job schreibt (Anzeige).</mat-hint
|
||||
>
|
||||
</mat-form-field>
|
||||
|
||||
<mat-form-field appearance="outline">
|
||||
<mat-label>Umgebungsvariablen (optional)</mat-label>
|
||||
<textarea
|
||||
matInput
|
||||
rows="3"
|
||||
formControlName="env"
|
||||
placeholder="KEY=value"></textarea>
|
||||
<mat-hint>Eine Variable pro Zeile im Format KEY=value.</mat-hint>
|
||||
@if (form.controls.env.hasError('invalidEnv')) {
|
||||
<mat-error>Ungültige Zeile — Format: KEY=value.</mat-error>
|
||||
}
|
||||
</mat-form-field>
|
||||
</form>
|
||||
|
||||
@if (serverError()) {
|
||||
<p class="server-error">{{ serverError() }}</p>
|
||||
}
|
||||
</mat-dialog-content>
|
||||
|
||||
<mat-dialog-actions align="end">
|
||||
<button mat-button type="button" (click)="close()">Abbrechen</button>
|
||||
<button
|
||||
mat-flat-button
|
||||
color="primary"
|
||||
type="button"
|
||||
[disabled]="creating()"
|
||||
(click)="submit()">
|
||||
<mat-icon>add</mat-icon>
|
||||
Anlegen
|
||||
</button>
|
||||
</mat-dialog-actions>
|
||||
@@ -0,0 +1,32 @@
|
||||
.create-form {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 12px;
|
||||
min-width: 400px;
|
||||
}
|
||||
|
||||
.commands-loading {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 8px;
|
||||
color: #64748b;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
.field-hint {
|
||||
margin: -6px 0 0;
|
||||
color: #64748b;
|
||||
font-size: 12px;
|
||||
}
|
||||
|
||||
.server-error {
|
||||
margin: 8px 0 0;
|
||||
color: #b3261e;
|
||||
font-size: 14px;
|
||||
}
|
||||
|
||||
@media (max-width: 640px) {
|
||||
.create-form {
|
||||
min-width: 0;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import { CommonModule } from '@angular/common';
|
||||
import { Component, inject, signal } from '@angular/core';
|
||||
import {
|
||||
FormControl,
|
||||
FormGroup,
|
||||
ReactiveFormsModule,
|
||||
Validators,
|
||||
} from '@angular/forms';
|
||||
import { MatButtonModule } from '@angular/material/button';
|
||||
import { MatDialogModule, MatDialogRef } from '@angular/material/dialog';
|
||||
import { MatFormFieldModule } from '@angular/material/form-field';
|
||||
import { MatIconModule } from '@angular/material/icon';
|
||||
import { MatInputModule } from '@angular/material/input';
|
||||
import { BackgroundJobTask, BackgroundJobsService } from '../../../api';
|
||||
|
||||
const ENV_LINE_PATTERN = /^[A-Za-z_][A-Za-z0-9_]*=.*$/;
|
||||
|
||||
@Component({
|
||||
selector: 'app-task-create-dialog',
|
||||
imports: [
|
||||
CommonModule,
|
||||
ReactiveFormsModule,
|
||||
MatButtonModule,
|
||||
MatDialogModule,
|
||||
MatFormFieldModule,
|
||||
MatIconModule,
|
||||
MatInputModule,
|
||||
],
|
||||
templateUrl: './task-create-dialog.component.html',
|
||||
styleUrl: './task-create-dialog.component.scss',
|
||||
standalone: true,
|
||||
})
|
||||
export class TaskCreateDialogComponent {
|
||||
private readonly dialogRef =
|
||||
inject<
|
||||
MatDialogRef<TaskCreateDialogComponent, BackgroundJobTask | undefined>
|
||||
>(MatDialogRef);
|
||||
private readonly backgroundJobsService = inject(BackgroundJobsService);
|
||||
|
||||
protected readonly creating = signal(false);
|
||||
protected readonly serverError = signal('');
|
||||
|
||||
protected readonly form = new FormGroup({
|
||||
name: new FormControl('', {
|
||||
nonNullable: true,
|
||||
validators: [Validators.required],
|
||||
}),
|
||||
description: new FormControl('', { nonNullable: true }),
|
||||
targetIndex: new FormControl('', { nonNullable: true }),
|
||||
command: new FormControl('', { nonNullable: true }),
|
||||
image: new FormControl('', { nonNullable: true }),
|
||||
env: new FormControl('', { nonNullable: true }),
|
||||
});
|
||||
|
||||
protected submit(): void {
|
||||
this.serverError.set('');
|
||||
const value = this.form.getRawValue();
|
||||
|
||||
const name = value.name.trim();
|
||||
if (!name) {
|
||||
this.form.controls.name.markAsTouched();
|
||||
return;
|
||||
}
|
||||
|
||||
const command = value.command.trim() || null;
|
||||
const env = this.parseEnv(value.env);
|
||||
if (env === undefined) return;
|
||||
|
||||
this.creating.set(true);
|
||||
this.backgroundJobsService
|
||||
.createBackgroundJobTask({
|
||||
name,
|
||||
description: value.description.trim(),
|
||||
target_index: value.targetIndex.trim(),
|
||||
image: value.image.trim() || null,
|
||||
command,
|
||||
env,
|
||||
})
|
||||
.subscribe({
|
||||
next: (task) => this.dialogRef.close(task),
|
||||
error: (error) => {
|
||||
this.creating.set(false);
|
||||
const detail = error?.error?.detail;
|
||||
this.serverError.set(
|
||||
typeof detail === 'string'
|
||||
? detail
|
||||
: 'Der Job konnte nicht angelegt werden.',
|
||||
);
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
protected close(): void {
|
||||
this.dialogRef.close();
|
||||
}
|
||||
|
||||
private parseEnv(text: string): { [key: string]: string } | undefined {
|
||||
const env: { [key: string]: string } = {};
|
||||
for (const line of text.split('\n')) {
|
||||
const trimmed = line.trim();
|
||||
if (!trimmed) continue;
|
||||
if (!ENV_LINE_PATTERN.test(trimmed)) {
|
||||
this.form.controls.env.setErrors({ invalidEnv: true });
|
||||
this.form.controls.env.markAsTouched();
|
||||
return undefined;
|
||||
}
|
||||
const separatorIndex = trimmed.indexOf('=');
|
||||
env[trimmed.slice(0, separatorIndex)] = trimmed.slice(separatorIndex + 1);
|
||||
}
|
||||
return env;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user