feat(client): add code-split Workflows, Broadcast, Snippets, Alerts views, pinned widgets, and service cron tasks
This commit is contained in:
parent
7272ad58bd
commit
41e79c3df3
9 changed files with 1925 additions and 72 deletions
|
|
@ -513,26 +513,188 @@ export async function renderSystemsView(
|
|||
}
|
||||
|
||||
// Per-Service Tasks & Cron workflows matching scope
|
||||
const scopedWfs = (state.overview?.workflows ?? []).filter((wf) => {
|
||||
const scope = wf.scope as { serverId?: string; systemId?: string; item?: string } | undefined;
|
||||
if (!scope) return false;
|
||||
if (scope.systemId && scope.systemId !== systemId) return false;
|
||||
if (scope.serverId && scope.serverId !== targetServerId) return false;
|
||||
if (scope.item && scope.item !== item.name) return false;
|
||||
return Boolean(scope.systemId);
|
||||
});
|
||||
const tasksPanel = el('section', { class: 'panel' });
|
||||
tasksPanel.append(
|
||||
el('div', { class: 'row-between' }, el('h2', {}, `Tasks & Cron (${scopedWfs.length})`), el('a', { href: '#config', class: 'btn btn-sm' }, 'Configure Workflows')),
|
||||
const tasksListHost = el('div', { class: 'stack' });
|
||||
const taskOutputPre = el('pre', { class: 'terminal-preview muted', hidden: true }, '');
|
||||
const scheduleFormWrap = el('form', { class: 'stack panel', hidden: true });
|
||||
|
||||
const taskActionInput = el('input', {
|
||||
type: 'text',
|
||||
value: 'restart',
|
||||
placeholder: 'restart / reload / status',
|
||||
required: true,
|
||||
});
|
||||
const taskNameInput = el('input', {
|
||||
type: 'text',
|
||||
value: `${item.name} scheduled task`,
|
||||
required: true,
|
||||
});
|
||||
const taskCronInput = el('input', {
|
||||
type: 'text',
|
||||
placeholder: '0 4 * * * (blank = manual)',
|
||||
});
|
||||
const taskTzInput = el('input', { type: 'text', value: 'UTC' });
|
||||
const taskFormErr = el('p', { class: 'err-text', hidden: true }, '');
|
||||
const toggleScheduleBtn = el(
|
||||
'button',
|
||||
{ type: 'button', class: 'btn btn-sm btn-primary' },
|
||||
'+ Schedule Service Task',
|
||||
);
|
||||
if (scopedWfs.length === 0) {
|
||||
tasksPanel.append(el('p', { class: 'muted' }, 'No scoped workflows yet. Phase 4 adds interactive scheduling and execution here.'));
|
||||
} else {
|
||||
for (const wf of scopedWfs) {
|
||||
tasksPanel.append(el('div', { class: 'mono' }, `• ${String(wf.name ?? wf.id)} (${String(wf.id)})`));
|
||||
toggleScheduleBtn.addEventListener('click', () => {
|
||||
scheduleFormWrap.hidden = !scheduleFormWrap.hidden;
|
||||
});
|
||||
|
||||
const refreshScopedTasks = async (): Promise<void> => {
|
||||
const wfRes = await apiFetch<{
|
||||
workflows: Array<{
|
||||
workflow: {
|
||||
id: string;
|
||||
name: string;
|
||||
trigger: { type: string; expression?: string };
|
||||
scope?: { serverId?: string; systemId?: string; item?: string };
|
||||
};
|
||||
nextRunAt: number | null;
|
||||
latestRun: { runId: string; status: string } | null;
|
||||
}>;
|
||||
}>('/api/workflows').catch(() => ({ workflows: [] }));
|
||||
const scopedWfs = wfRes.workflows.filter((entry) => {
|
||||
const sc = entry.workflow.scope;
|
||||
if (!sc || sc.systemId !== systemId) return false;
|
||||
if (sc.serverId && sc.serverId !== targetServerId) return false;
|
||||
if (sc.item && sc.item !== item.name) return false;
|
||||
return true;
|
||||
});
|
||||
tasksListHost.replaceChildren();
|
||||
if (scopedWfs.length === 0) {
|
||||
tasksListHost.append(
|
||||
el('p', { class: 'muted' }, 'No scoped tasks or cron workflows for this service yet.'),
|
||||
);
|
||||
return;
|
||||
}
|
||||
}
|
||||
for (const entry of scopedWfs) {
|
||||
const wf = entry.workflow;
|
||||
const trigText =
|
||||
wf.trigger.type === 'cron'
|
||||
? `cron: ${wf.trigger.expression ?? ''}${entry.nextRunAt ? ` (next ${new Date(entry.nextRunAt).toISOString().slice(0, 19).replace('T', ' ')})` : ''}`
|
||||
: wf.trigger.type;
|
||||
const statusBadge = el(
|
||||
'span',
|
||||
{
|
||||
class:
|
||||
entry.latestRun?.status === 'success'
|
||||
? 'badge badge-ok'
|
||||
: entry.latestRun?.status === 'failed'
|
||||
? 'badge badge-err'
|
||||
: 'badge',
|
||||
},
|
||||
entry.latestRun?.status ?? 'idle',
|
||||
);
|
||||
const runTaskBtn = el('button', { type: 'button', class: 'btn btn-sm btn-primary' }, 'Run Task');
|
||||
runTaskBtn.addEventListener('click', async () => {
|
||||
runTaskBtn.disabled = true;
|
||||
taskOutputPre.hidden = false;
|
||||
taskOutputPre.textContent = `Running ${wf.name}…`;
|
||||
try {
|
||||
const started = await apiFetch<{ runId: string; status: string }>(
|
||||
`/api/workflows/${encodeURIComponent(wf.id)}/run`,
|
||||
{ method: 'POST', body: {} },
|
||||
);
|
||||
taskOutputPre.textContent = `Run ${started.runId}: ${started.status}`;
|
||||
for (let i = 0; i < 15; i++) {
|
||||
await new Promise((r) => setTimeout(r, 400));
|
||||
const detail = await apiFetch<{
|
||||
run: { status: string };
|
||||
stepLogs: Array<{ stepId: string; exitCode: number | null; stdout: string; stderr: string }>;
|
||||
}>(`/api/workflows/runs/${encodeURIComponent(started.runId)}`);
|
||||
const logs = detail.stepLogs
|
||||
.map((l) => `[${l.stepId}] exit=${l.exitCode ?? '-'}\n${l.stdout}${l.stderr ? `\nSTDERR:\n${l.stderr}` : ''}`)
|
||||
.join('\n');
|
||||
taskOutputPre.textContent = `Status: ${detail.run.status}\n${logs}`.trim();
|
||||
if (detail.run.status !== 'running' && detail.run.status !== 'queued') break;
|
||||
}
|
||||
await refreshScopedTasks();
|
||||
} catch (err) {
|
||||
taskOutputPre.textContent = err instanceof Error ? err.message : String(err);
|
||||
} finally {
|
||||
runTaskBtn.disabled = false;
|
||||
}
|
||||
});
|
||||
tasksListHost.append(
|
||||
el(
|
||||
'div',
|
||||
{ class: 'row-between' },
|
||||
el('div', {}, el('strong', {}, wf.name), el('span', { class: 'muted mono' }, ` (${trigText})`)),
|
||||
el('div', { class: 'row' }, statusBadge, runTaskBtn),
|
||||
),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const saveTaskBtn = el('button', { type: 'submit', class: 'btn btn-sm btn-primary' }, 'Create Task');
|
||||
scheduleFormWrap.append(
|
||||
el('label', {}, 'Task Name', taskNameInput),
|
||||
el('label', {}, 'System Action ID', taskActionInput),
|
||||
el('div', { class: 'grid-2' }, el('label', {}, 'Cron (optional)', taskCronInput), el('label', {}, 'Timezone', taskTzInput)),
|
||||
taskFormErr,
|
||||
el('div', { class: 'row' }, saveTaskBtn),
|
||||
);
|
||||
scheduleFormWrap.addEventListener('submit', async (ev) => {
|
||||
ev.preventDefault();
|
||||
taskFormErr.hidden = true;
|
||||
const slug = `${systemId}-${item.name}`
|
||||
.toLowerCase()
|
||||
.replace(/[^a-z0-9_-]+/g, '-')
|
||||
.replace(/^-+|-+$/g, '')
|
||||
.slice(0, 28);
|
||||
const id = `svc-${slug || 'task'}-${Date.now().toString(36)}`;
|
||||
const cronExpr = taskCronInput.value.trim();
|
||||
try {
|
||||
await apiFetch('/api/workflows', {
|
||||
method: 'POST',
|
||||
body: {
|
||||
id,
|
||||
name: taskNameInput.value.trim() || `${item.name} task`,
|
||||
enabled: true,
|
||||
trigger: cronExpr
|
||||
? { type: 'cron', expression: cronExpr, timezone: taskTzInput.value.trim() || 'UTC' }
|
||||
: { type: 'manual' },
|
||||
overlapPolicy: 'skip',
|
||||
display: { mode: 'log', pinToDashboard: false },
|
||||
scope: { serverId: targetServerId, systemId, item: item.name },
|
||||
inputs: [],
|
||||
steps: [
|
||||
{
|
||||
id: 'service-action',
|
||||
type: 'system-action',
|
||||
target: { serverId: targetServerId },
|
||||
systemId,
|
||||
action: taskActionInput.value.trim() || 'status',
|
||||
item: item.name,
|
||||
},
|
||||
],
|
||||
},
|
||||
});
|
||||
scheduleFormWrap.hidden = true;
|
||||
await refreshScopedTasks();
|
||||
} catch (err) {
|
||||
taskFormErr.hidden = false;
|
||||
taskFormErr.textContent = err instanceof Error ? err.message : String(err);
|
||||
}
|
||||
});
|
||||
|
||||
tasksPanel.append(
|
||||
el(
|
||||
'div',
|
||||
{ class: 'row-between' },
|
||||
el('h2', {}, 'Tasks & Cron'),
|
||||
el('div', { class: 'row' }, toggleScheduleBtn, el('a', { href: '#workflows', class: 'btn btn-sm' }, 'All Workflows')),
|
||||
),
|
||||
scheduleFormWrap,
|
||||
tasksListHost,
|
||||
taskOutputPre,
|
||||
);
|
||||
root.append(tasksPanel);
|
||||
void refreshScopedTasks();
|
||||
return;
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue