@quazardous/qdadm 2.46.0 → 2.48.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/styles/qdadm.css +1 -1
- package/package.json +1 -1
- package/src/components/SeverityTag.vue +15 -1
- package/src/components/display/QdPending.vue +58 -0
- package/src/components/display/QdPendingZone.vue +104 -0
- package/src/components/index.ts +5 -0
- package/src/components/lists/ListPage.vue +33 -4
- package/src/components/show/ShowDisplay.vue +13 -2
- package/src/components/show/ShowPage.vue +6 -6
- package/src/components/tasks/TaskDialog.vue +13 -157
- package/src/components/tasks/TaskHost.vue +28 -2
- package/src/components/tasks/TaskRunStatus.vue +63 -0
- package/src/components/tasks/TaskRunView.vue +197 -0
- package/src/components/tasks/TaskSequenceDialog.vue +157 -0
- package/src/components/tasks/TaskTray.vue +33 -13
- package/src/composables/index.ts +6 -0
- package/src/composables/useAsyncRead.ts +109 -0
- package/src/composables/useEntityItemFormPage.ts +13 -6
- package/src/composables/useEntityItemFormPage.types.ts +4 -1
- package/src/composables/useEntityItemShowPage.ts +18 -7
- package/src/composables/useFieldManager.ts +11 -2
- package/src/composables/useListPage.filters.ts +21 -1
- package/src/composables/useListPage.ts +68 -9
- package/src/composables/useListPage.types.ts +4 -1
- package/src/composables/useNavContext.ts +5 -4
- package/src/entity/EntityManager.ts +17 -0
- package/src/entity/EntityManager.types.ts +2 -0
- package/src/entity/storage/MockApiStorage.ts +31 -7
- package/src/i18n/defaults/core.en.generated.ts +32 -0
- package/src/i18n/defaults/core.en.yml +32 -0
- package/src/i18n/defaults/core.fr.generated.ts +32 -0
- package/src/i18n/defaults/core.fr.yml +32 -0
- package/src/i18n/optionLabels.ts +35 -0
- package/src/i18n/types.ts +5 -0
- package/src/index.ts +11 -0
- package/src/kernel/Kernel.i18n.ts +36 -3
- package/src/kernel/Kernel.registries.ts +11 -7
- package/src/kernel/Kernel.ts +2 -0
- package/src/kernel/KernelContext.permissions.ts +11 -0
- package/src/kernel/KernelContext.ts +6 -0
- package/src/kernel/KernelContext.types.ts +2 -0
- package/src/orchestrator/Orchestrator.ts +3 -0
- package/src/security/PersistableRolesProvider.ts +61 -3
- package/src/security/StaticRolesProvider.ts +6 -0
- package/src/styles/_pending.scss +125 -0
- package/src/styles/_tasks.scss +126 -0
- package/src/styles/index.scss +1 -0
- package/src/tasks/taskStore.ts +449 -14
- package/src/tasks/taskUi.ts +6 -0
- package/src/tasks/useTaskNotifications.ts +6 -2
- package/src/tasks/useTaskRunner.ts +24 -0
package/src/tasks/taskStore.ts
CHANGED
|
@@ -72,6 +72,13 @@ export interface TaskKind {
|
|
|
72
72
|
slots?: TaskUiOptions['slots']
|
|
73
73
|
/** A dialog of its own for this kind, instead of TaskDialog. */
|
|
74
74
|
component?: TaskUiOptions['component']
|
|
75
|
+
/**
|
|
76
|
+
* Start a task of this kind from its meta alone. With it, `run({ kind, meta })`
|
|
77
|
+
* needs no `start`, and a sequence of such kinds goes on after a reload (#4934).
|
|
78
|
+
*/
|
|
79
|
+
start?: (meta: Record<string, unknown> | null) => Promise<unknown>
|
|
80
|
+
/** A run's line in a sequence's list (#4934). Default: the error, else the step. */
|
|
81
|
+
summary?: (run: TaskRun) => string | null | undefined
|
|
75
82
|
}
|
|
76
83
|
|
|
77
84
|
export interface TaskConfirm {
|
|
@@ -84,15 +91,25 @@ export interface TaskConfirm {
|
|
|
84
91
|
severity?: 'primary' | 'secondary' | 'success' | 'info' | 'warn' | 'danger' | 'contrast'
|
|
85
92
|
}
|
|
86
93
|
|
|
87
|
-
export interface RunOptions extends Partial<TaskKind> {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
94
|
+
export interface RunOptions extends Omit<Partial<TaskKind>, 'label' | 'start'> {
|
|
95
|
+
/** A string, or a function read each time it shows (tray, dialog, toast), so it follows the locale (#4692) */
|
|
96
|
+
label: string | (() => string)
|
|
97
|
+
/**
|
|
98
|
+
* Queue the task; returns its id, or `{ task_id | taskId | id }`. In a
|
|
99
|
+
* sequence it gets the previous step's run (#4934). Optional when the kind
|
|
100
|
+
* has a `start`.
|
|
101
|
+
*/
|
|
102
|
+
start?: (previous: TaskRun | null) => Promise<unknown>
|
|
91
103
|
/** A registered kind; its settings are the defaults of this run, and the run resumes after a reload. */
|
|
92
104
|
kind?: string
|
|
93
105
|
confirm?: TaskConfirm
|
|
94
106
|
/** Open the follow dialog at once. Default true. */
|
|
95
107
|
open?: boolean
|
|
108
|
+
/**
|
|
109
|
+
* What the run is about, for a page to find it again: `{ supplierId: 7 }`.
|
|
110
|
+
* Kept with the run, across a reload too (#4687). Plain values only.
|
|
111
|
+
*/
|
|
112
|
+
meta?: Record<string, unknown>
|
|
96
113
|
}
|
|
97
114
|
|
|
98
115
|
export interface TaskRun {
|
|
@@ -108,10 +125,61 @@ export interface TaskRun {
|
|
|
108
125
|
startedAt: number
|
|
109
126
|
endedAt: number | null
|
|
110
127
|
cancelling: boolean
|
|
128
|
+
/** What the run is about, as given to run() (#4687) */
|
|
129
|
+
meta: Record<string, unknown> | null
|
|
130
|
+
/** The sequence this run is a step of, or null (#4934) */
|
|
131
|
+
sequenceId: string | null
|
|
111
132
|
/** Settles with the run once it ends: succeeded, failed or cancelled (it never rejects). */
|
|
112
133
|
finished: Promise<TaskRun>
|
|
113
134
|
}
|
|
114
135
|
|
|
136
|
+
/** A step of a sequence: run options, or a function of the previous step's run that returns them (#4934). */
|
|
137
|
+
export type SequenceStep = RunOptions | ((previous: TaskRun | null) => RunOptions)
|
|
138
|
+
|
|
139
|
+
export interface SequenceOptions {
|
|
140
|
+
/** A failure stops the steps that follow (they show "not run"). Default true. */
|
|
141
|
+
stopOnError?: boolean
|
|
142
|
+
/** Open the sequence's window at once. Default true. */
|
|
143
|
+
open?: boolean
|
|
144
|
+
/** Asked once, before the first step. */
|
|
145
|
+
confirm?: TaskConfirm
|
|
146
|
+
/** What the sequence is about, for a page to find it again (sequenceFor). Plain values only. */
|
|
147
|
+
meta?: Record<string, unknown>
|
|
148
|
+
/** One toast when it ends. Default true. */
|
|
149
|
+
notify?: boolean
|
|
150
|
+
/** Called once the sequence ends. */
|
|
151
|
+
onSettled?: (sequence: TaskSequence) => void
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
/** A step that has no run: waiting its turn, or never run (and why). */
|
|
155
|
+
export type SequenceStepStatus = TaskStatus | 'pending' | 'skipped'
|
|
156
|
+
|
|
157
|
+
export interface TaskSequenceStep {
|
|
158
|
+
/** The run's label once started, else the declared one ("Step 2" for a step given as a function) */
|
|
159
|
+
label: string
|
|
160
|
+
runId: string | null
|
|
161
|
+
/** The state recorded: pending, skipped, or the run's once it ended. Live: `store.stepStatus(step)` */
|
|
162
|
+
status: SequenceStepStatus
|
|
163
|
+
/** Why a step was not run: an earlier step failed, the sequence was cancelled, the page was reloaded */
|
|
164
|
+
skipped: 'error' | 'cancelled' | 'reload' | null
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export interface TaskSequence {
|
|
168
|
+
id: string
|
|
169
|
+
label: string
|
|
170
|
+
status: 'running' | 'succeeded' | 'failed' | 'cancelled'
|
|
171
|
+
steps: TaskSequenceStep[]
|
|
172
|
+
/** The index of the step running, or of the last one run */
|
|
173
|
+
current: number
|
|
174
|
+
stopOnError: boolean
|
|
175
|
+
cancelling: boolean
|
|
176
|
+
startedAt: number
|
|
177
|
+
endedAt: number | null
|
|
178
|
+
meta: Record<string, unknown> | null
|
|
179
|
+
/** Settles with the sequence once it ends (it never rejects). */
|
|
180
|
+
finished: Promise<TaskSequence>
|
|
181
|
+
}
|
|
182
|
+
|
|
115
183
|
interface TaskHostDeps {
|
|
116
184
|
orchestrator?: {
|
|
117
185
|
get: (name: string) => unknown
|
|
@@ -128,6 +196,7 @@ interface PendingConfirm {
|
|
|
128
196
|
|
|
129
197
|
const FINAL: TaskStatus[] = ['succeeded', 'failed', 'cancelled']
|
|
130
198
|
const STORAGE_KEY = 'qdadm:tasks'
|
|
199
|
+
const SEQUENCES_KEY = 'qdadm:task-sequences'
|
|
131
200
|
const KEEP_FINISHED = 20
|
|
132
201
|
|
|
133
202
|
const USUAL: Record<string, Exclude<TaskStatus, 'starting'>> = {
|
|
@@ -165,6 +234,9 @@ function errorText(err: unknown): string {
|
|
|
165
234
|
function createTaskStore() {
|
|
166
235
|
const state = reactive({
|
|
167
236
|
runs: [] as TaskRun[],
|
|
237
|
+
/** Runs chained in one window (#4934) */
|
|
238
|
+
sequences: [] as TaskSequence[],
|
|
239
|
+
/** The run or the sequence whose window is open */
|
|
168
240
|
openId: null as string | null,
|
|
169
241
|
pendingConfirm: null as PendingConfirm | null,
|
|
170
242
|
hosts: 0,
|
|
@@ -183,7 +255,7 @@ function createTaskStore() {
|
|
|
183
255
|
try {
|
|
184
256
|
const saved = state.runs
|
|
185
257
|
.filter((r) => r.kind && kinds.has(r.kind) && r.taskId !== null && !FINAL.includes(r.status))
|
|
186
|
-
.map((r) => ({ id: r.id, kind: r.kind, label: r.label, taskId: r.taskId, startedAt: r.startedAt }))
|
|
258
|
+
.map((r) => ({ id: r.id, kind: r.kind, label: r.label, taskId: r.taskId, startedAt: r.startedAt, meta: r.meta, sequenceId: r.sequenceId }))
|
|
187
259
|
if (saved.length) sessionStorage.setItem(STORAGE_KEY, JSON.stringify(saved))
|
|
188
260
|
else sessionStorage.removeItem(STORAGE_KEY)
|
|
189
261
|
} catch {
|
|
@@ -194,7 +266,7 @@ function createTaskStore() {
|
|
|
194
266
|
function restore(): void {
|
|
195
267
|
if (restored) return
|
|
196
268
|
restored = true
|
|
197
|
-
let saved: Array<Pick<TaskRun, 'id' | 'kind' | 'label' | 'taskId' | 'startedAt'
|
|
269
|
+
let saved: Array<Pick<TaskRun, 'id' | 'kind' | 'label' | 'taskId' | 'startedAt'> & { meta?: TaskRun['meta']; sequenceId?: string | null }> = []
|
|
198
270
|
try {
|
|
199
271
|
saved = JSON.parse(sessionStorage.getItem(STORAGE_KEY) ?? '[]')
|
|
200
272
|
} catch {
|
|
@@ -203,15 +275,17 @@ function createTaskStore() {
|
|
|
203
275
|
for (const s of saved) {
|
|
204
276
|
const kind = s.kind ? kinds.get(s.kind) : undefined
|
|
205
277
|
if (!kind || s.taskId === null || state.runs.some((r) => r.id === s.id)) continue
|
|
206
|
-
const run = addRun({ id: s.id, kind: s.kind, label: s.label, taskId: s.taskId, status: 'running', startedAt: s.startedAt })
|
|
207
|
-
|
|
278
|
+
const run = addRun({ id: s.id, kind: s.kind, label: s.label, taskId: s.taskId, status: 'running', startedAt: s.startedAt, meta: s.meta ?? null, sequenceId: s.sequenceId ?? null })
|
|
279
|
+
// A step of a sequence: the sequence gives the one toast
|
|
280
|
+
defs.set(run.id, s.sequenceId ? { ...kind, notify: false } : kind)
|
|
208
281
|
follow(run.id)
|
|
209
282
|
}
|
|
283
|
+
restoreSequences()
|
|
210
284
|
}
|
|
211
285
|
|
|
212
286
|
// --- Runs ---
|
|
213
287
|
|
|
214
|
-
function addRun(partial: Partial<TaskRun> & Pick<TaskRun, 'label'
|
|
288
|
+
function addRun(partial: Partial<TaskRun> & Pick<TaskRun, 'label'>, labelOf?: () => string): TaskRun {
|
|
215
289
|
const id = partial.id ?? `task-${Date.now().toString(36)}-${++seq}`
|
|
216
290
|
const run: TaskRun = {
|
|
217
291
|
id,
|
|
@@ -226,9 +300,13 @@ function createTaskStore() {
|
|
|
226
300
|
startedAt: partial.startedAt ?? Date.now(),
|
|
227
301
|
endedAt: null,
|
|
228
302
|
cancelling: false,
|
|
303
|
+
meta: partial.meta ?? null,
|
|
304
|
+
sequenceId: partial.sequenceId ?? null,
|
|
229
305
|
// A promise is not made reactive: run.finished stays the raw promise.
|
|
230
306
|
finished: new Promise<TaskRun>((resolve) => settles.set(id, resolve)),
|
|
231
307
|
}
|
|
308
|
+
// A label given as a function is read each time it shows (#4692)
|
|
309
|
+
if (labelOf) Object.defineProperty(run, 'label', { get: labelOf, enumerable: true, configurable: true })
|
|
232
310
|
state.runs.unshift(run)
|
|
233
311
|
// Keep every active run, and the most recent finished ones.
|
|
234
312
|
const finished = state.runs.filter((r) => FINAL.includes(r.status))
|
|
@@ -389,18 +467,31 @@ function createTaskStore() {
|
|
|
389
467
|
* pass `onSettled`) for its outcome.
|
|
390
468
|
*/
|
|
391
469
|
async function run(options: RunOptions): Promise<TaskRun | null> {
|
|
470
|
+
return startRun(options, {})
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
async function startRun(options: RunOptions, ctx: { previous?: TaskRun | null; sequenceId?: string }): Promise<TaskRun | null> {
|
|
392
474
|
const base = options.kind ? kinds.get(options.kind) : undefined
|
|
393
475
|
if (options.kind && !base && !options.task) {
|
|
394
476
|
throw new Error(`TaskRunner: unknown task kind "${options.kind}"`)
|
|
395
477
|
}
|
|
396
478
|
const def: TaskKind = { ...base, ...options, task: { ...base?.task, ...options.task } } as TaskKind
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
const
|
|
479
|
+
const labelOf = typeof options.label === 'function' ? options.label : undefined
|
|
480
|
+
const label = labelOf ? labelOf() : (options.label as string)
|
|
481
|
+
const meta = options.meta ? { ...options.meta } : null
|
|
482
|
+
const start = options.start
|
|
483
|
+
? () => options.start!(ctx.previous ?? null)
|
|
484
|
+
: base?.start
|
|
485
|
+
? () => base.start!(meta)
|
|
486
|
+
: null
|
|
487
|
+
if (!start) throw new Error(`TaskRunner: "${label}" has no start, and its kind none either`)
|
|
488
|
+
if (options.confirm && !(await requestConfirm(label, options.confirm))) return null
|
|
489
|
+
|
|
490
|
+
const r = addRun({ label, kind: options.kind ?? null, meta, sequenceId: ctx.sequenceId ?? null }, labelOf)
|
|
400
491
|
defs.set(r.id, def)
|
|
401
492
|
if (options.open !== false) state.openId = r.id
|
|
402
493
|
try {
|
|
403
|
-
const taskId = taskIdOf(await
|
|
494
|
+
const taskId = taskIdOf(await start())
|
|
404
495
|
if (taskId === null) throw new Error('the start handler returned no task id')
|
|
405
496
|
const current = find(r.id)!
|
|
406
497
|
current.taskId = taskId
|
|
@@ -441,6 +532,7 @@ function createTaskStore() {
|
|
|
441
532
|
|
|
442
533
|
function clearFinished(): void {
|
|
443
534
|
for (const r of state.runs.filter((x) => FINAL.includes(x.status))) dismiss(r.id)
|
|
535
|
+
for (const x of state.sequences.filter((x) => x.status !== 'running')) dismissSequence(x.id)
|
|
444
536
|
}
|
|
445
537
|
|
|
446
538
|
/** Wire the orchestrator (manager reads, toasts) and the signal bus (live). Called by useTaskRunner. */
|
|
@@ -452,6 +544,332 @@ function createTaskStore() {
|
|
|
452
544
|
|
|
453
545
|
const active: ComputedRef<TaskRun[]> = computed(() => state.runs.filter((r) => !FINAL.includes(r.status)))
|
|
454
546
|
|
|
547
|
+
// --- Sequences: runs chained in one window (#4934) ---
|
|
548
|
+
|
|
549
|
+
interface SequenceInternals {
|
|
550
|
+
/** Each step's options; null for a step that cannot start again after a reload */
|
|
551
|
+
specs: Array<SequenceStep | null>
|
|
552
|
+
options: SequenceOptions
|
|
553
|
+
/** One step only: it is a plain run, with its own window and toast */
|
|
554
|
+
single: boolean
|
|
555
|
+
settle: (sequence: TaskSequence) => void
|
|
556
|
+
}
|
|
557
|
+
const sequenceInternals = new Map<string, SequenceInternals>()
|
|
558
|
+
let sequenceSeq = 0
|
|
559
|
+
|
|
560
|
+
const findSequence = (id: string) => state.sequences.find((s) => s.id === id)
|
|
561
|
+
|
|
562
|
+
/** A step's state: its run's while the run is in the store, else what was recorded */
|
|
563
|
+
function stepStatus(step: TaskSequenceStep): SequenceStepStatus {
|
|
564
|
+
const run = step.runId ? find(step.runId) : undefined
|
|
565
|
+
return run?.status ?? step.status ?? 'pending'
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/** What the list shows on a step's line: the kind's summary, else the error, else the step reached */
|
|
569
|
+
function stepSummary(step: TaskSequenceStep): string | null {
|
|
570
|
+
const run = step.runId ? find(step.runId) : undefined
|
|
571
|
+
if (!run) return null
|
|
572
|
+
const custom = defs.get(run.id)?.summary
|
|
573
|
+
if (custom) {
|
|
574
|
+
try {
|
|
575
|
+
const text = custom(run)
|
|
576
|
+
if (text !== undefined) return text
|
|
577
|
+
} catch (err) {
|
|
578
|
+
console.error(`[qdadm] Task "${run.label}": summary threw`, err)
|
|
579
|
+
}
|
|
580
|
+
}
|
|
581
|
+
if (run.status === 'failed') return run.error
|
|
582
|
+
if (run.status === 'succeeded' && run.result !== null && typeof run.result !== 'object') return String(run.result)
|
|
583
|
+
return run.step
|
|
584
|
+
}
|
|
585
|
+
|
|
586
|
+
const declaredLabel = (spec: SequenceStep | null | undefined): string | (() => string) =>
|
|
587
|
+
!spec || typeof spec === 'function' ? '' : spec.label
|
|
588
|
+
|
|
589
|
+
function addSequence(
|
|
590
|
+
partial: Pick<TaskSequence, 'label' | 'stopOnError' | 'meta'> & Partial<Pick<TaskSequence, 'id' | 'startedAt' | 'current' | 'cancelling'>>,
|
|
591
|
+
steps: Array<{ label: string | (() => string); runId?: string | null; status?: SequenceStepStatus; skipped?: TaskSequenceStep['skipped'] }>,
|
|
592
|
+
internals: Omit<SequenceInternals, 'settle'>,
|
|
593
|
+
labelOf?: () => string
|
|
594
|
+
): TaskSequence {
|
|
595
|
+
const id = partial.id ?? `seq-${Date.now().toString(36)}-${++sequenceSeq}`
|
|
596
|
+
let settle!: (sequence: TaskSequence) => void
|
|
597
|
+
const sequence: TaskSequence = {
|
|
598
|
+
id,
|
|
599
|
+
label: partial.label,
|
|
600
|
+
status: 'running',
|
|
601
|
+
steps: steps.map((st) => {
|
|
602
|
+
const declared = st.label
|
|
603
|
+
return {
|
|
604
|
+
runId: st.runId ?? null,
|
|
605
|
+
status: st.status ?? 'pending',
|
|
606
|
+
skipped: st.skipped ?? null,
|
|
607
|
+
// The run's label once started (it follows the locale), else the declared one
|
|
608
|
+
get label(): string {
|
|
609
|
+
const run = this.runId ? find(this.runId) : undefined
|
|
610
|
+
if (run) return run.label
|
|
611
|
+
return typeof declared === 'function' ? declared() : declared
|
|
612
|
+
},
|
|
613
|
+
} as TaskSequenceStep
|
|
614
|
+
}),
|
|
615
|
+
current: partial.current ?? 0,
|
|
616
|
+
stopOnError: partial.stopOnError,
|
|
617
|
+
cancelling: partial.cancelling ?? false,
|
|
618
|
+
startedAt: partial.startedAt ?? Date.now(),
|
|
619
|
+
endedAt: null,
|
|
620
|
+
meta: partial.meta,
|
|
621
|
+
finished: new Promise<TaskSequence>((resolve) => (settle = resolve)),
|
|
622
|
+
}
|
|
623
|
+
if (labelOf) Object.defineProperty(sequence, 'label', { get: labelOf, enumerable: true, configurable: true })
|
|
624
|
+
sequenceInternals.set(id, { ...internals, settle })
|
|
625
|
+
state.sequences.unshift(sequence)
|
|
626
|
+
const finished = state.sequences.filter((x) => FINAL.includes(x.status as TaskStatus))
|
|
627
|
+
for (const old of finished.slice(KEEP_FINISHED)) dismissSequence(old.id)
|
|
628
|
+
return findSequence(id)!
|
|
629
|
+
}
|
|
630
|
+
|
|
631
|
+
function persistSequences(): void {
|
|
632
|
+
try {
|
|
633
|
+
const saved = state.sequences
|
|
634
|
+
.filter((x) => x.status === 'running' && !sequenceInternals.get(x.id)?.single)
|
|
635
|
+
.map((x) => {
|
|
636
|
+
const specs = sequenceInternals.get(x.id)?.specs ?? []
|
|
637
|
+
return {
|
|
638
|
+
id: x.id,
|
|
639
|
+
label: x.label,
|
|
640
|
+
meta: x.meta,
|
|
641
|
+
stopOnError: x.stopOnError,
|
|
642
|
+
startedAt: x.startedAt,
|
|
643
|
+
current: x.current,
|
|
644
|
+
cancelling: x.cancelling,
|
|
645
|
+
steps: x.steps.map((st, i) => {
|
|
646
|
+
const spec = specs[i]
|
|
647
|
+
const plain = spec && typeof spec !== 'function' ? spec : null
|
|
648
|
+
return { label: st.label, runId: st.runId, status: stepStatus(st), skipped: st.skipped, kind: plain?.kind ?? null, meta: plain?.meta ?? null }
|
|
649
|
+
}),
|
|
650
|
+
}
|
|
651
|
+
})
|
|
652
|
+
if (saved.length) sessionStorage.setItem(SEQUENCES_KEY, JSON.stringify(saved))
|
|
653
|
+
else sessionStorage.removeItem(SEQUENCES_KEY)
|
|
654
|
+
} catch {
|
|
655
|
+
// no storage: a sequence does not survive a reload
|
|
656
|
+
}
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
/** A sequence that was running before a reload: its running step was restored with the runs; the steps after it start again when their kind can */
|
|
660
|
+
function restoreSequences(): void {
|
|
661
|
+
type Saved = Pick<TaskSequence, 'id' | 'label' | 'meta' | 'stopOnError' | 'startedAt' | 'current' | 'cancelling'> & {
|
|
662
|
+
steps: Array<{ label: string; runId: string | null; status: SequenceStepStatus; skipped: TaskSequenceStep['skipped']; kind: string | null; meta: Record<string, unknown> | null }>
|
|
663
|
+
}
|
|
664
|
+
let saved: Saved[] = []
|
|
665
|
+
try {
|
|
666
|
+
saved = JSON.parse(sessionStorage.getItem(SEQUENCES_KEY) ?? '[]')
|
|
667
|
+
} catch {
|
|
668
|
+
return
|
|
669
|
+
}
|
|
670
|
+
for (const x of saved) {
|
|
671
|
+
if (findSequence(x.id)) continue
|
|
672
|
+
const specs = x.steps.map((st) => {
|
|
673
|
+
const kind = st.kind ? kinds.get(st.kind) : undefined
|
|
674
|
+
return kind?.start ? ({ kind: st.kind!, label: st.label, meta: st.meta ?? undefined } as RunOptions) : null
|
|
675
|
+
})
|
|
676
|
+
const steps = x.steps.map((st) => ({
|
|
677
|
+
label: st.label,
|
|
678
|
+
// A run that is not back (its kind is not registered) cannot be followed
|
|
679
|
+
runId: st.runId && find(st.runId) ? st.runId : null,
|
|
680
|
+
status: st.runId && !find(st.runId) && !FINAL.includes(st.status as TaskStatus) ? ('skipped' as const) : st.status,
|
|
681
|
+
skipped: st.runId && !find(st.runId) && !FINAL.includes(st.status as TaskStatus) ? ('reload' as const) : st.skipped,
|
|
682
|
+
}))
|
|
683
|
+
const sequence = addSequence(x, steps, { specs, options: {}, single: false })
|
|
684
|
+
const current = sequence.steps[sequence.current]
|
|
685
|
+
const previous = current?.runId ? find(current.runId) ?? null : null
|
|
686
|
+
void drive(sequence.id, previous)
|
|
687
|
+
}
|
|
688
|
+
}
|
|
689
|
+
|
|
690
|
+
function skipFrom(sequence: TaskSequence, index: number, why: NonNullable<TaskSequenceStep['skipped']>): void {
|
|
691
|
+
for (const step of sequence.steps.slice(index)) {
|
|
692
|
+
if (step.runId) continue
|
|
693
|
+
step.status = 'skipped'
|
|
694
|
+
step.skipped = why
|
|
695
|
+
}
|
|
696
|
+
}
|
|
697
|
+
|
|
698
|
+
/** Run the steps one after another, from the sequence's current one */
|
|
699
|
+
async function drive(id: string, previous: TaskRun | null): Promise<void> {
|
|
700
|
+
const internals = sequenceInternals.get(id)
|
|
701
|
+
if (!internals) return
|
|
702
|
+
let failed = false
|
|
703
|
+
for (let i = findSequence(id)?.current ?? 0; i < (findSequence(id)?.steps.length ?? 0); i++) {
|
|
704
|
+
const sequence = findSequence(id)
|
|
705
|
+
if (!sequence) return
|
|
706
|
+
const step = sequence.steps[i]!
|
|
707
|
+
if (step.status === 'skipped') continue
|
|
708
|
+
if (step.runId && FINAL.includes(stepStatus(step) as TaskStatus)) {
|
|
709
|
+
// Already over before a reload
|
|
710
|
+
failed ||= stepStatus(step) === 'failed'
|
|
711
|
+
continue
|
|
712
|
+
}
|
|
713
|
+
if (sequence.cancelling) {
|
|
714
|
+
skipFrom(sequence, i, 'cancelled')
|
|
715
|
+
break
|
|
716
|
+
}
|
|
717
|
+
if (failed && sequence.stopOnError) {
|
|
718
|
+
skipFrom(sequence, i, 'error')
|
|
719
|
+
break
|
|
720
|
+
}
|
|
721
|
+
sequence.current = i
|
|
722
|
+
let run: TaskRun | null = step.runId ? find(step.runId) ?? null : null
|
|
723
|
+
if (!run) {
|
|
724
|
+
const spec = internals.specs[i]
|
|
725
|
+
if (!spec) {
|
|
726
|
+
skipFrom(sequence, i, 'reload')
|
|
727
|
+
break
|
|
728
|
+
}
|
|
729
|
+
let options: RunOptions
|
|
730
|
+
try {
|
|
731
|
+
options = typeof spec === 'function' ? spec(previous) : spec
|
|
732
|
+
} catch (err) {
|
|
733
|
+
options = { label: step.label || `#${i + 1}`, start: () => Promise.reject(err), task: {} }
|
|
734
|
+
}
|
|
735
|
+
const stepOptions: RunOptions = {
|
|
736
|
+
...options,
|
|
737
|
+
confirm: undefined,
|
|
738
|
+
open: internals.single ? internals.options.open !== false : false,
|
|
739
|
+
...(internals.single ? {} : { notify: false }),
|
|
740
|
+
}
|
|
741
|
+
try {
|
|
742
|
+
run = await startRun(stepOptions, { previous, sequenceId: id })
|
|
743
|
+
} catch (err) {
|
|
744
|
+
run = await startRun({ label: options.label, start: () => Promise.reject(err), task: {}, open: false, notify: internals.single ? undefined : false }, { sequenceId: id })
|
|
745
|
+
}
|
|
746
|
+
if (!run) break
|
|
747
|
+
step.runId = run.id
|
|
748
|
+
persistSequences()
|
|
749
|
+
}
|
|
750
|
+
const end = await run.finished
|
|
751
|
+
step.status = end.status
|
|
752
|
+
if (end.status === 'failed') failed = true
|
|
753
|
+
if (end.status === 'cancelled') findSequence(id)!.cancelling = true
|
|
754
|
+
previous = end
|
|
755
|
+
persistSequences()
|
|
756
|
+
}
|
|
757
|
+
finishSequence(id)
|
|
758
|
+
}
|
|
759
|
+
|
|
760
|
+
function finishSequence(id: string): void {
|
|
761
|
+
const sequence = findSequence(id)
|
|
762
|
+
const internals = sequenceInternals.get(id)
|
|
763
|
+
if (!sequence || !internals || sequence.status !== 'running') return
|
|
764
|
+
const statuses = sequence.steps.map((st) => stepStatus(st))
|
|
765
|
+
sequence.status = statuses.includes('failed')
|
|
766
|
+
? 'failed'
|
|
767
|
+
: statuses.some((st) => st === 'cancelled' || st === 'skipped')
|
|
768
|
+
? 'cancelled'
|
|
769
|
+
: 'succeeded'
|
|
770
|
+
sequence.endedAt = Date.now()
|
|
771
|
+
sequence.cancelling = false
|
|
772
|
+
persistSequences()
|
|
773
|
+
if (!internals.single && internals.options.notify !== false) notifySequenceEnd(sequence)
|
|
774
|
+
internals.settle(sequence)
|
|
775
|
+
try {
|
|
776
|
+
internals.options.onSettled?.(sequence)
|
|
777
|
+
} catch (err) {
|
|
778
|
+
console.error(`[qdadm] Task sequence "${sequence.label}": onSettled threw`, err)
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
const MARKS: Partial<Record<SequenceStepStatus, string>> = { succeeded: '✓', failed: '✗', cancelled: '⊘', skipped: '–' }
|
|
783
|
+
|
|
784
|
+
/** One toast for the whole sequence: each step marked, the failure's error after it */
|
|
785
|
+
function notifySequenceEnd(sequence: TaskSequence): void {
|
|
786
|
+
const toast = deps.orchestrator?.toast
|
|
787
|
+
if (!toast) return
|
|
788
|
+
const detail = sequence.steps
|
|
789
|
+
.map((st, i) => {
|
|
790
|
+
const status = stepStatus(st)
|
|
791
|
+
const label = st.label || `#${i + 1}`
|
|
792
|
+
const error = status === 'failed' && st.runId ? find(st.runId)?.error : null
|
|
793
|
+
return `${MARKS[status] ?? '…'} ${label}${error ? `: ${error}` : ''}`
|
|
794
|
+
})
|
|
795
|
+
.join(' · ')
|
|
796
|
+
const opts = { emitter: 'TaskRunner' }
|
|
797
|
+
if (sequence.status === 'succeeded') toast.success(sequence.label, detail, opts)
|
|
798
|
+
else if (sequence.status === 'failed') toast.error(sequence.label, detail, { ...opts, keep: 'long' })
|
|
799
|
+
else toast.warn(sequence.label, detail, opts)
|
|
800
|
+
}
|
|
801
|
+
|
|
802
|
+
/**
|
|
803
|
+
* Run tasks one after another, followed in one window: the list of steps,
|
|
804
|
+
* each step's detail on a click (#4934). Resolves once the sequence is
|
|
805
|
+
* under way (null when its confirmation is declined); await
|
|
806
|
+
* `sequence.finished` for its end. One step only is a plain run.
|
|
807
|
+
*/
|
|
808
|
+
async function sequence(
|
|
809
|
+
label: string | (() => string),
|
|
810
|
+
steps: SequenceStep[],
|
|
811
|
+
options: SequenceOptions = {}
|
|
812
|
+
): Promise<TaskSequence | null> {
|
|
813
|
+
if (!steps.length) throw new Error('TaskRunner: a sequence needs at least one step')
|
|
814
|
+
const labelOf = typeof label === 'function' ? label : undefined
|
|
815
|
+
const text = labelOf ? labelOf() : (label as string)
|
|
816
|
+
if (options.confirm && !(await requestConfirm(text, options.confirm))) return null
|
|
817
|
+
const single = steps.length === 1
|
|
818
|
+
const created = addSequence(
|
|
819
|
+
{ label: text, stopOnError: options.stopOnError !== false, meta: options.meta ? { ...options.meta } : null },
|
|
820
|
+
steps.map((spec) => ({ label: declaredLabel(spec) })),
|
|
821
|
+
{ specs: steps.slice(), options, single },
|
|
822
|
+
labelOf
|
|
823
|
+
)
|
|
824
|
+
if (!single && options.open !== false) state.openId = created.id
|
|
825
|
+
void drive(created.id, null)
|
|
826
|
+
return created
|
|
827
|
+
}
|
|
828
|
+
|
|
829
|
+
/** Stop a sequence: its running step is cancelled when its kind can, and the steps after it are not run. */
|
|
830
|
+
async function cancelSequence(id: string): Promise<void> {
|
|
831
|
+
const sequence = findSequence(id)
|
|
832
|
+
if (!sequence || sequence.status !== 'running') return
|
|
833
|
+
sequence.cancelling = true
|
|
834
|
+
persistSequences()
|
|
835
|
+
const runId = sequence.steps[sequence.current]?.runId
|
|
836
|
+
if (runId) await cancel(runId)
|
|
837
|
+
}
|
|
838
|
+
|
|
839
|
+
function dismissSequence(id: string): void {
|
|
840
|
+
const i = state.sequences.findIndex((x) => x.id === id)
|
|
841
|
+
if (i !== -1) state.sequences.splice(i, 1)
|
|
842
|
+
sequenceInternals.delete(id)
|
|
843
|
+
if (state.openId === id) state.openId = null
|
|
844
|
+
persistSequences()
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
/** The sequences matching a meta (each given key equal), newest first. */
|
|
848
|
+
function sequencesFor(meta: Record<string, unknown> = {}): TaskSequence[] {
|
|
849
|
+
return state.sequences
|
|
850
|
+
.filter((x) => Object.entries(meta).every(([k, v]) => x.meta?.[k] === v))
|
|
851
|
+
.sort((a, b) => b.startedAt - a.startedAt)
|
|
852
|
+
}
|
|
853
|
+
|
|
854
|
+
// --- Finding a run again (#4687) ---
|
|
855
|
+
|
|
856
|
+
/** The runs matching a kind and/or a meta (each given key equal), newest first. */
|
|
857
|
+
function runsFor(filter: { kind?: string; taskId?: string | number; meta?: Record<string, unknown> } = {}): TaskRun[] {
|
|
858
|
+
return state.runs
|
|
859
|
+
.filter((r) =>
|
|
860
|
+
(filter.kind === undefined || r.kind === filter.kind) &&
|
|
861
|
+
(filter.taskId === undefined || String(r.taskId) === String(filter.taskId)) &&
|
|
862
|
+
Object.entries(filter.meta ?? {}).every(([k, v]) => r.meta?.[k] === v)
|
|
863
|
+
)
|
|
864
|
+
.sort((a, b) => b.startedAt - a.startedAt)
|
|
865
|
+
}
|
|
866
|
+
|
|
867
|
+
/** The latest run of a kind about one record: `runFor('stock-check', { supplierId: 7 })`, or by task id. */
|
|
868
|
+
function runFor(kind: string, about: Record<string, unknown> | string | number): TaskRun | null {
|
|
869
|
+
const filter = typeof about === 'object' ? { kind, meta: about } : { kind, taskId: about }
|
|
870
|
+
return runsFor(filter)[0] ?? null
|
|
871
|
+
}
|
|
872
|
+
|
|
455
873
|
return {
|
|
456
874
|
state,
|
|
457
875
|
active,
|
|
@@ -466,7 +884,22 @@ function createTaskStore() {
|
|
|
466
884
|
answerConfirm,
|
|
467
885
|
attach,
|
|
468
886
|
definition: (id: string) => defs.get(id),
|
|
469
|
-
|
|
887
|
+
runFor,
|
|
888
|
+
runsFor,
|
|
889
|
+
isFinal: (status: TaskStatus | SequenceStepStatus | TaskSequence['status']) => FINAL.includes(status as TaskStatus),
|
|
890
|
+
/** A run or a sequence still going (#4695, #4934) */
|
|
891
|
+
isActive: (run: TaskRun | TaskSequence | null | undefined) => !!run && !FINAL.includes(run.status as TaskStatus),
|
|
892
|
+
sequence,
|
|
893
|
+
cancelSequence,
|
|
894
|
+
dismissSequence,
|
|
895
|
+
/** The sequence a run is a step of, or a sequence by its id */
|
|
896
|
+
sequenceOf: (id: string | null | undefined): TaskSequence | null =>
|
|
897
|
+
!id ? null : (findSequence(id) ?? (find(id)?.sequenceId ? findSequence(find(id)!.sequenceId!) ?? null : null)),
|
|
898
|
+
sequencesFor,
|
|
899
|
+
/** The latest sequence about a record: `sequenceFor({ box: 'b1' })` */
|
|
900
|
+
sequenceFor: (meta: Record<string, unknown>) => sequencesFor(meta)[0] ?? null,
|
|
901
|
+
stepStatus,
|
|
902
|
+
stepSummary,
|
|
470
903
|
/** For tests: stop everything and forget every run. */
|
|
471
904
|
reset(): void {
|
|
472
905
|
for (const stop of stops.values()) stop()
|
|
@@ -476,6 +909,8 @@ function createTaskStore() {
|
|
|
476
909
|
defs.clear()
|
|
477
910
|
kinds.clear()
|
|
478
911
|
state.runs.splice(0)
|
|
912
|
+
state.sequences.splice(0)
|
|
913
|
+
sequenceInternals.clear()
|
|
479
914
|
state.openId = null
|
|
480
915
|
state.pendingConfirm = null
|
|
481
916
|
state.hosts = 0
|
package/src/tasks/taskUi.ts
CHANGED
|
@@ -27,6 +27,11 @@ export interface TaskDialogOptions {
|
|
|
27
27
|
class?: string
|
|
28
28
|
/** Keep size, position and full screen in localStorage, per app */
|
|
29
29
|
rememberLayout?: boolean
|
|
30
|
+
/**
|
|
31
|
+
* Stay open when the user goes to another page. Off by default: the dialog
|
|
32
|
+
* is modal, so it closes and the run goes on in the tray (#4695).
|
|
33
|
+
*/
|
|
34
|
+
keepOnNavigate?: boolean
|
|
30
35
|
}
|
|
31
36
|
|
|
32
37
|
/** The TaskDialog slots an app fills with components; each gets `{ run, kind, final }`. */
|
|
@@ -64,6 +69,7 @@ export const TASK_DIALOG_DEFAULTS: Required<Omit<TaskDialogOptions, 'position' |
|
|
|
64
69
|
resizable: true,
|
|
65
70
|
maximizable: true,
|
|
66
71
|
rememberLayout: true,
|
|
72
|
+
keepOnNavigate: false,
|
|
67
73
|
position: undefined,
|
|
68
74
|
class: undefined,
|
|
69
75
|
}
|
|
@@ -46,12 +46,16 @@ export function useTaskNotifications(): void {
|
|
|
46
46
|
for (const { run } of entries) {
|
|
47
47
|
const id = STATUS_PREFIX + run.id
|
|
48
48
|
ids.add(id)
|
|
49
|
+
// A step of a sequence says which sequence, and opens its window (#4934)
|
|
50
|
+
const sequence = run.sequenceId ? store.sequenceOf(run.sequenceId) : null
|
|
51
|
+
const index = sequence ? sequence.steps.findIndex((st) => st.runId === run.id) : -1
|
|
52
|
+
const label = sequence && index !== -1 ? `${sequence.label} · ${run.label} (${index + 1}/${sequence.steps.length})` : run.label
|
|
49
53
|
notifications.registerStatus({
|
|
50
54
|
id,
|
|
51
|
-
label: `${
|
|
55
|
+
label: `${label} — ${stateText(run)}`,
|
|
52
56
|
severity: 'nominal',
|
|
53
57
|
icon: 'pi pi-spin pi-spinner',
|
|
54
|
-
onClick: () => store.open(run.id),
|
|
58
|
+
onClick: () => store.open(sequence ? sequence.id : run.id),
|
|
55
59
|
})
|
|
56
60
|
shown.add(id)
|
|
57
61
|
if (!tracked.has(run.id)) {
|
|
@@ -20,6 +20,18 @@
|
|
|
20
20
|
* // Or react to the end without awaiting it (also fires after a reload resume)
|
|
21
21
|
* tasks.run({ label: 'Refresh db', start, task: { entity: 'tasks' }, onSettled: () => list.refresh() })
|
|
22
22
|
*
|
|
23
|
+
* // A page finds the run about its record again, live, across a reload
|
|
24
|
+
* tasks.run({ kind: 'stock-check', label, start, meta: { supplierId: id } })
|
|
25
|
+
* const check = computed(() => tasks.runFor('stock-check', { supplierId: id }))
|
|
26
|
+
*
|
|
27
|
+
* // Tasks chained in one window: the list of steps, each step's detail on a click (#4934).
|
|
28
|
+
* // A step's start gets the previous step's run; a failure stops the rest.
|
|
29
|
+
* const seq = await tasks.sequence('Verify', [
|
|
30
|
+
* { kind: 'check', label: 'Check', start: () => api.check(box) },
|
|
31
|
+
* { kind: 'conf-check', label: 'Conf check', start: (prev) => api.confCheck(box, prev?.result) },
|
|
32
|
+
* ], { meta: { box } })
|
|
33
|
+
* const busy = computed(() => tasks.isActive(tasks.sequenceFor({ box })))
|
|
34
|
+
*
|
|
23
35
|
* // As a row action, with a typed confirmation
|
|
24
36
|
* list.addAction('destroy', {
|
|
25
37
|
* icon: 'pi pi-trash', severity: 'danger',
|
|
@@ -49,6 +61,18 @@ export function useTaskRunner() {
|
|
|
49
61
|
open: store.open,
|
|
50
62
|
runs: store.state.runs,
|
|
51
63
|
active: store.active,
|
|
64
|
+
/** The latest run of a kind about one record (its meta, or its task id), or null (#4687). */
|
|
65
|
+
runFor: store.runFor,
|
|
66
|
+
/** The runs matching `{ kind, taskId, meta }`, newest first. */
|
|
67
|
+
runsFor: store.runsFor,
|
|
68
|
+
/** A run or a sequence still going, false for none (#4695). */
|
|
69
|
+
isActive: store.isActive,
|
|
70
|
+
/** Run tasks one after another, followed in one window (#4934). Resolves once under way; await `sequence.finished`. */
|
|
71
|
+
sequence: store.sequence,
|
|
72
|
+
cancelSequence: store.cancelSequence,
|
|
73
|
+
/** The latest sequence about a record (its meta), or null. */
|
|
74
|
+
sequenceFor: store.sequenceFor,
|
|
75
|
+
sequencesFor: store.sequencesFor,
|
|
52
76
|
}
|
|
53
77
|
}
|
|
54
78
|
|