Address Greptile review on agent runtime PR
Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
parent
19154d0fec
commit
55b26ed590
1 changed files with 48 additions and 16 deletions
|
|
@ -80,7 +80,10 @@ export function InstanceSettings() {
|
||||||
const disableAllMutation = useMutation({
|
const disableAllMutation = useMutation({
|
||||||
mutationFn: async (agentRows: InstanceSchedulerHeartbeatAgent[]) => {
|
mutationFn: async (agentRows: InstanceSchedulerHeartbeatAgent[]) => {
|
||||||
const enabled = agentRows.filter((a) => a.heartbeatEnabled);
|
const enabled = agentRows.filter((a) => a.heartbeatEnabled);
|
||||||
for (const agentRow of enabled) {
|
if (enabled.length === 0) return enabled;
|
||||||
|
|
||||||
|
const results = await Promise.allSettled(
|
||||||
|
enabled.map(async (agentRow) => {
|
||||||
const agent = await agentsApi.get(agentRow.id, agentRow.companyId);
|
const agent = await agentsApi.get(agentRow.id, agentRow.companyId);
|
||||||
const runtimeConfig = asRecord(agent.runtimeConfig) ?? {};
|
const runtimeConfig = asRecord(agent.runtimeConfig) ?? {};
|
||||||
const heartbeat = asRecord(runtimeConfig.heartbeat) ?? {};
|
const heartbeat = asRecord(runtimeConfig.heartbeat) ?? {};
|
||||||
|
|
@ -94,11 +97,33 @@ export function InstanceSettings() {
|
||||||
},
|
},
|
||||||
agentRow.companyId,
|
agentRow.companyId,
|
||||||
);
|
);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const failures = results.filter((result): result is PromiseRejectedResult => result.status === "rejected");
|
||||||
|
if (failures.length > 0) {
|
||||||
|
const firstError = failures[0]?.reason;
|
||||||
|
const detail = firstError instanceof Error ? firstError.message : "Unknown error";
|
||||||
|
throw new Error(
|
||||||
|
failures.length === 1
|
||||||
|
? `Failed to disable 1 timer heartbeat: ${detail}`
|
||||||
|
: `Failed to disable ${failures.length} of ${enabled.length} timer heartbeats. First error: ${detail}`,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
return enabled;
|
||||||
},
|
},
|
||||||
onSuccess: async () => {
|
onSuccess: async (updatedRows) => {
|
||||||
setActionError(null);
|
setActionError(null);
|
||||||
await queryClient.invalidateQueries({ queryKey: queryKeys.instance.schedulerHeartbeats });
|
const companies = new Set(updatedRows.map((row) => row.companyId));
|
||||||
|
await Promise.all([
|
||||||
|
queryClient.invalidateQueries({ queryKey: queryKeys.instance.schedulerHeartbeats }),
|
||||||
|
...Array.from(companies, (companyId) =>
|
||||||
|
queryClient.invalidateQueries({ queryKey: queryKeys.agents.list(companyId) }),
|
||||||
|
),
|
||||||
|
...updatedRows.map((row) =>
|
||||||
|
queryClient.invalidateQueries({ queryKey: queryKeys.agents.detail(row.id) }),
|
||||||
|
),
|
||||||
|
]);
|
||||||
},
|
},
|
||||||
onError: (error) => {
|
onError: (error) => {
|
||||||
setActionError(error instanceof Error ? error.message : "Failed to disable all heartbeats.");
|
setActionError(error instanceof Error ? error.message : "Failed to disable all heartbeats.");
|
||||||
|
|
@ -108,7 +133,8 @@ export function InstanceSettings() {
|
||||||
const agents = heartbeatsQuery.data ?? [];
|
const agents = heartbeatsQuery.data ?? [];
|
||||||
const activeCount = agents.filter((agent) => agent.schedulerActive).length;
|
const activeCount = agents.filter((agent) => agent.schedulerActive).length;
|
||||||
const disabledCount = agents.length - activeCount;
|
const disabledCount = agents.length - activeCount;
|
||||||
const anyEnabled = agents.some((a) => a.heartbeatEnabled);
|
const enabledCount = agents.filter((agent) => agent.heartbeatEnabled).length;
|
||||||
|
const anyEnabled = enabledCount > 0;
|
||||||
|
|
||||||
const grouped = useMemo(() => {
|
const grouped = useMemo(() => {
|
||||||
const map = new Map<string, { companyName: string; agents: InstanceSchedulerHeartbeatAgent[] }>();
|
const map = new Map<string, { companyName: string; agents: InstanceSchedulerHeartbeatAgent[] }>();
|
||||||
|
|
@ -159,7 +185,13 @@ export function InstanceSettings() {
|
||||||
size="sm"
|
size="sm"
|
||||||
className="ml-auto h-7 text-xs"
|
className="ml-auto h-7 text-xs"
|
||||||
disabled={disableAllMutation.isPending}
|
disabled={disableAllMutation.isPending}
|
||||||
onClick={() => disableAllMutation.mutate(agents)}
|
onClick={() => {
|
||||||
|
const noun = enabledCount === 1 ? "agent" : "agents";
|
||||||
|
if (!window.confirm(`Disable timer heartbeats for all ${enabledCount} enabled ${noun}?`)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
disableAllMutation.mutate(agents);
|
||||||
|
}}
|
||||||
>
|
>
|
||||||
{disableAllMutation.isPending ? "Disabling..." : "Disable All"}
|
{disableAllMutation.isPending ? "Disabling..." : "Disable All"}
|
||||||
</Button>
|
</Button>
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue