swarm-ui: let a paused schedule's fire now send its message as a broadcast
Fire now was disabled on a paused schedule because the controller fires nothing while it is paused. It is enabled now and, after a confirmation, sends the schedule's message to its agents through the broadcast route, leaving the schedule paused with its start unchanged. A toast says who got it, and the error banner names the agents that did not.
This commit is contained in:
parent
48ce818bb7
commit
09de25b0ed
5 changed files with 111 additions and 9 deletions
|
|
@ -491,7 +491,10 @@ title, with the **agents** it fires for, the message, how often it repeats and
|
|||
when it fires next. **edit** turns the card into its own form; **fire now** sends
|
||||
`PATCH /api/schedules/{id}` with the current second as `anchor_unix`, so a start
|
||||
that has passed fires its latest time right away and a repeating schedule goes on
|
||||
from there at its interval (a paused schedule has no fire now); **pause** and
|
||||
from there at its interval. On a paused schedule **fire now** sends the message
|
||||
to its agents as a broadcast (`POST /api/agents/broadcast`) after a
|
||||
confirmation and leaves the schedule paused with its start unchanged; a toast
|
||||
says who got it, and the error banner names any agent that didn't. **pause** and
|
||||
**resume** stop and restart it; **remove** deletes it after a confirmation.
|
||||
**+ schedule** opens an empty form above the list, and the filters narrow the
|
||||
list by name, agent or state (`active`, `paused`, or `past` for a one-shot
|
||||
|
|
|
|||
|
|
@ -87,7 +87,7 @@ export function ScheduleCard({
|
|||
<span
|
||||
title={
|
||||
schedule.paused
|
||||
? "resume it first: a paused schedule does not fire"
|
||||
? "sends its message to its agents now and leaves it paused"
|
||||
: schedule.interval_seconds === null
|
||||
? "fires it now: its start moves to this moment"
|
||||
: "fires it now: its start moves to this moment, so it repeats from now on"
|
||||
|
|
@ -95,7 +95,7 @@ export function ScheduleCard({
|
|||
>
|
||||
<Button
|
||||
onClick={onFireNow}
|
||||
disabled={pending || schedule.paused}
|
||||
disabled={pending}
|
||||
ariaLabel={`fire now ${schedule.name}`}
|
||||
>
|
||||
fire now
|
||||
|
|
|
|||
|
|
@ -111,3 +111,31 @@ export function fmtLocal(unix: number): string {
|
|||
export function fireNowBody(nowMs: number): { anchor_unix: number } {
|
||||
return { anchor_unix: Math.floor(nowMs / 1000) };
|
||||
}
|
||||
|
||||
/**
|
||||
* What to tell the operator after a paused schedule's "fire now" sent its
|
||||
* message as a broadcast: `ok` when every agent got it, else the ones that
|
||||
* did not, with the controller's reason where it gave one.
|
||||
*/
|
||||
export function pausedFireOutcome(
|
||||
name: string,
|
||||
results: { agent: string; sent: boolean; reason?: string | null }[],
|
||||
): { ok: boolean; message: string } {
|
||||
const failed = results.filter((r) => !r.sent);
|
||||
if (failed.length === 0) {
|
||||
return {
|
||||
ok: true,
|
||||
message:
|
||||
results.length === 1
|
||||
? `sent ${name} to ${results[0].agent}`
|
||||
: `sent ${name} to ${results.length} agents`,
|
||||
};
|
||||
}
|
||||
const why = failed
|
||||
.map((r) => (r.reason ? `${r.agent} (${r.reason})` : r.agent))
|
||||
.join(", ");
|
||||
return {
|
||||
ok: false,
|
||||
message: `${name} did not reach ${failed.length} of ${results.length}: ${why}`,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,7 +29,18 @@ import type { AgentRow } from "../../agent/AgentTypes.js";
|
|||
import { ScheduleCard, ScheduleListSkeleton } from "./ScheduleCard.js";
|
||||
import { agentNamesFrom, agentOptionsFor } from "./agent-options.js";
|
||||
import { ScheduleForm } from "./ScheduleForm.js";
|
||||
import { fireNowBody, scheduleState, type Schedule } from "./ScheduleTypes.js";
|
||||
import {
|
||||
fireNowBody,
|
||||
pausedFireOutcome,
|
||||
scheduleState,
|
||||
type Schedule,
|
||||
} from "./ScheduleTypes.js";
|
||||
import { postBroadcast } from "../broadcast/broadcast-send.js";
|
||||
import { showToast } from "../../ui/toast/toasts.js";
|
||||
|
||||
// What the error banner says failed.
|
||||
const SCHEDULE_UNCHANGED = "the schedule was not changed";
|
||||
const SEND_FAILED = "the message was not sent to every agent";
|
||||
|
||||
// A refetch never touches an open form's own state (it lives in the form),
|
||||
// so the usual 30s default is safe here too.
|
||||
|
|
@ -92,6 +103,8 @@ function SchedulesBody() {
|
|||
const [removeTarget, setRemoveTarget] = useState<Schedule | null>(null);
|
||||
const [confirm, fireDialog] = useConfirm();
|
||||
const [actionError, setActionError] = useState<ProblemDetails | null>(null);
|
||||
// What the error banner says failed: a change to the schedule, or a send.
|
||||
const [errorContext, setErrorContext] = useState(SCHEDULE_UNCHANGED);
|
||||
|
||||
function markPending(id: string, on: boolean) {
|
||||
setPendingIds((cur) => {
|
||||
|
|
@ -108,6 +121,7 @@ function SchedulesBody() {
|
|||
async function act(id: string, request: () => Promise<Response>) {
|
||||
markPending(id, true);
|
||||
setActionError(null);
|
||||
setErrorContext(SCHEDULE_UNCHANGED);
|
||||
try {
|
||||
const r = await request();
|
||||
if (!r.ok) {
|
||||
|
|
@ -122,7 +136,37 @@ function SchedulesBody() {
|
|||
}
|
||||
}
|
||||
|
||||
// A paused schedule has no fire time to move, so its "fire now" sends the
|
||||
// message as a broadcast and leaves the schedule as it is.
|
||||
async function fireNowPaused(s: Schedule) {
|
||||
const yes = await confirm({
|
||||
label: "send paused schedule now",
|
||||
confirmLabel: "send now",
|
||||
body: (
|
||||
<p>
|
||||
Send <strong>{s.name}</strong> to {s.agents.join(", ")} now? The
|
||||
schedule stays paused and its start does not change.
|
||||
</p>
|
||||
),
|
||||
});
|
||||
if (!yes) return;
|
||||
markPending(s.id, true);
|
||||
setActionError(null);
|
||||
setErrorContext(SEND_FAILED);
|
||||
const answer = await postBroadcast(s.agents, s.body);
|
||||
markPending(s.id, false);
|
||||
if (answer.status === "error") {
|
||||
setActionError(answer.problem);
|
||||
return;
|
||||
}
|
||||
if (answer.status !== "done") return;
|
||||
const outcome = pausedFireOutcome(s.name, answer.results);
|
||||
if (outcome.ok) showToast(outcome.message, "ok");
|
||||
else setActionError({ detail: outcome.message });
|
||||
}
|
||||
|
||||
async function fireNow(s: Schedule) {
|
||||
if (s.paused) return fireNowPaused(s);
|
||||
const repeats = s.interval_seconds !== null;
|
||||
const yes = await confirm({
|
||||
label: "fire schedule now",
|
||||
|
|
@ -203,10 +247,7 @@ function SchedulesBody() {
|
|||
hasData={schedules !== null}
|
||||
/>
|
||||
{actionError ? (
|
||||
<ApiErrorPanel
|
||||
context="the schedule was not changed"
|
||||
problem={actionError}
|
||||
/>
|
||||
<ApiErrorPanel context={errorContext} problem={actionError} />
|
||||
) : null}
|
||||
{creating ? (
|
||||
<ScheduleForm
|
||||
|
|
|
|||
|
|
@ -1,7 +1,10 @@
|
|||
// The body of a schedule's "fire now". Run by `npm test`.
|
||||
import assert from "node:assert/strict";
|
||||
import { test } from "node:test";
|
||||
import { fireNowBody } from "../src/pages/schedules/ScheduleTypes.ts";
|
||||
import {
|
||||
fireNowBody,
|
||||
pausedFireOutcome,
|
||||
} from "../src/pages/schedules/ScheduleTypes.ts";
|
||||
|
||||
test("fire now moves the start to the current second", () => {
|
||||
assert.deepEqual(fireNowBody(1_700_000_123_456), {
|
||||
|
|
@ -12,3 +15,30 @@ test("fire now moves the start to the current second", () => {
|
|||
test("fire now sends nothing but the start", () => {
|
||||
assert.deepEqual(Object.keys(fireNowBody(0)), ["anchor_unix"]);
|
||||
});
|
||||
|
||||
test("a paused schedule's send says who got it", () => {
|
||||
assert.deepEqual(
|
||||
pausedFireOutcome("nightly", [{ agent: "iris", sent: true }]),
|
||||
{ ok: true, message: "sent nightly to iris" },
|
||||
);
|
||||
assert.deepEqual(
|
||||
pausedFireOutcome("nightly", [
|
||||
{ agent: "iris", sent: true },
|
||||
{ agent: "coder", sent: true },
|
||||
]),
|
||||
{ ok: true, message: "sent nightly to 2 agents" },
|
||||
);
|
||||
});
|
||||
|
||||
test("a paused schedule's send names the agents that did not get it", () => {
|
||||
const out = pausedFireOutcome("nightly", [
|
||||
{ agent: "iris", sent: true },
|
||||
{ agent: "coder", sent: false, reason: "not running" },
|
||||
{ agent: "gui", sent: false },
|
||||
]);
|
||||
assert.equal(out.ok, false);
|
||||
assert.equal(
|
||||
out.message,
|
||||
"nightly did not reach 2 of 3: coder (not running), gui",
|
||||
);
|
||||
});
|
||||
|
|
|
|||
Loading…
Reference in a new issue