feat(admin): terminate multiple selected process list sessions

Closes #9778
This commit is contained in:
t8y2 committed 2026-09-21 01:44:23 +08:00
1 parent 1ded0a7bcd
commit 216afec5e4
13 files changed
+555 -9

No files matched your search

@@ -59,13 +59,16 @@ const terminating = ref(false);
const batchSupported = computed(() => driver.value?.supportsBatchCancel === true);
// Engines without a terminate statement only expose query cancellation.
const terminateSupported = computed(() => typeof driver.value?.buildTerminateSessionSql === "function");
// Multi-selection serves batch cancel and/or batch terminate; either enables the checkboxes.
const selectionSupported = computed(() => batchSupported.value || terminateSupported.value);
const selectedIds = ref(new Set<number>());
// Anchor row for Shift+click range selection, in the current display order.
const selectionAnchorId = ref<number | null>(null);
const batchTargets = ref<ProcessRow[] | null>(null);
const batchResult = ref<{ succeeded: number; failures: { id: number; message: string }[] } | null>(null);
const batchTerminateTargets = ref<ProcessRow[] | null>(null);
const batchResult = ref<{ kind: "cancel" | "terminate"; succeeded: number; failures: { id: number; message: string }[] } | null>(null);
const refreshing = ref(false);
const actionsLocked = computed(() => canceling.value || cancelTarget.value !== null || terminating.value || terminateTarget.value !== null || batchTargets.value !== null);
const actionsLocked = computed(() => canceling.value || cancelTarget.value !== null || terminating.value || terminateTarget.value !== null || batchTargets.value !== null || batchTerminateTargets.value !== null);
let connectionGeneration = 0;
let disposed = false;
const fallbackListSql = ref<string | null>(null);
@@ -165,7 +168,7 @@ async function confirmBatchCancel() {
sql: statements.map((statement) => statement.sql).join(";\n"),
source: t("production.sourceAdmin"),
execute: async () => {
const summary = { succeeded: 0, failures: [] as { id: number; message: string }[] };
const summary = { kind: "cancel" as const, succeeded: 0, failures: [] as { id: number; message: string }[] };
for (const statement of statements) {
// A closed tab or changed connection must not dispatch remaining work.
if (!isCurrent()) break;
@@ -194,6 +197,79 @@ async function confirmBatchCancel() {
}
}
function requestBatchTerminate() {
if (!terminateSupported.value || actionsLocked.value || refreshing.value || ownSessionId.value === null) return;
const targets = selectableRows.value.filter((row) => selectedIds.value.has(row.id));
if (targets.length) batchTerminateTargets.value = targets.map((row) => ({ ...row }));
}
/**
* Close the selected sessions one by one. Each statement runs on its own so a
* failure on one session (permissions, an id that already exited) is reported in
* the summary instead of aborting the remaining terminations, mirroring batch
* cancel. The `terminating` mutex is shared with the single-row terminate action.
*/
async function confirmBatchTerminate() {
const targets = batchTerminateTargets.value;
const activeDriver = driver.value;
const buildTerminateSql = activeDriver?.buildTerminateSessionSql;
if (!targets?.length || !activeDriver || !buildTerminateSql || terminating.value) return;
const connection = { ...props.connection };
const generation = connectionGeneration;
const isCurrent = () => !disposed && generation === connectionGeneration;
terminating.value = true;
let executed = false;
try {
const statements = targets.map((row) => ({ id: row.id, sql: buildTerminateSql.call(activeDriver, row.id) }));
const executeTerminateSql = async (sql: string) => {
const results = await api.executeMulti(connection.id, "", sql, undefined, undefined, { maxRows: 1 });
const executionError = processListExecutionError(results);
if (executionError) throw new Error(executionError);
return results;
};
const result = await executeWithProductionSqlGuard({
connection,
database: "",
sql: statements.map((statement) => statement.sql).join(";\n"),
source: t("production.sourceAdmin"),
execute: async () => {
const summary = { kind: "terminate" as const, succeeded: 0, failures: [] as { id: number; message: string }[] };
for (const statement of statements) {
// A closed tab or changed connection must not dispatch remaining work.
if (!isCurrent()) break;
executed = true;
try {
let usedFallbackTerminateSql = false;
let results;
try {
results = await executeTerminateSql(statement.sql);
} catch (error) {
if (!activeDriver.buildFallbackTerminateSessionSql || !activeDriver.shouldUseFallbackTerminateSessionSql?.(error)) throw error;
usedFallbackTerminateSql = true;
results = await executeTerminateSql(activeDriver.buildFallbackTerminateSessionSql(statement.id));
}
const terminateResultError = usedFallbackTerminateSql ? activeDriver.fallbackTerminateSessionResultError?.(results) : activeDriver.terminateSessionResultError?.(results);
if (terminateResultError) throw new Error(terminateResultError);
summary.succeeded++;
} catch (error: unknown) {
summary.failures.push({ id: statement.id, message: error instanceof Error ? error.message : String(error) });
}
}
return summary;
},
});
if (result === undefined || !isCurrent()) return;
batchResult.value = result;
batchTerminateTargets.value = null;
clearSelection();
} catch (error: unknown) {
if (isCurrent()) toast(t("processList.terminateFailed", { message: error instanceof Error ? error.message : String(error) }), 5000);
} finally {
terminating.value = false;
if (!disposed && (executed || !isCurrent())) void load({ silent: true });
}
}
function toggleSort(key: string) {
if (sortKey.value === key) {
sortDir.value = sortDir.value === "asc" ? "desc" : "asc";
@@ -402,6 +478,7 @@ watch(
connectionGeneration++;
clearSelection();
batchTargets.value = null;
batchTerminateTargets.value = null;
batchResult.value = null;
cancelTarget.value = null;
fallbackListSql.value = null;
@@ -446,6 +523,19 @@ onBeforeUnmount(() => {
<Ban v-else class="h-3.5 w-3.5" />
{{ t("processList.batchCancel", { count: selectedIds.size }) }}
</Button>
<Button
v-if="terminateSupported"
variant="destructive"
size="sm"
class="h-7 gap-1.5 px-2 text-xs"
:disabled="actionsLocked || refreshing || selectedIds.size === 0 || ownSessionId === null"
:title="ownSessionId === null ? t('processList.batchNeedsSession') : undefined"
@click="requestBatchTerminate"
>
<Loader2 v-if="terminating && batchTerminateTargets" class="h-3.5 w-3.5 animate-spin" />
<PlugZap v-else class="h-3.5 w-3.5" />
{{ t("processList.batchTerminate", { count: selectedIds.size }) }}
</Button>
<div class="flex h-7 items-center gap-1.5 rounded-md border bg-background px-2">
<Search class="h-3.5 w-3.5 shrink-0 text-muted-foreground" />
<input v-model="search" :disabled="actionsLocked" class="h-full w-40 min-w-0 bg-transparent text-xs outline-none placeholder:text-muted-foreground" :placeholder="t('processList.filter')" />
@@ -466,7 +556,7 @@ onBeforeUnmount(() => {
</div>
</div>
<div v-if="batchSupported && ownSessionId === null && !refreshing" class="border-b px-3 py-2 text-xs text-muted-foreground">{{ t("processList.batchNeedsSession") }}</div>
<div v-if="selectionSupported && ownSessionId === null && !refreshing" class="border-b px-3 py-2 text-xs text-muted-foreground">{{ t("processList.batchNeedsSession") }}</div>
<div v-if="loadError" class="border-b bg-destructive/10 px-3 py-2 text-xs text-destructive">{{ loadError }}</div>
@@ -474,7 +564,7 @@ onBeforeUnmount(() => {
<table class="w-full border-collapse text-xs">
<thead class="sticky top-0 z-10 bg-muted/40 backdrop-blur">
<tr>
<th v-if="batchSupported" class="w-8 border-b px-3 py-2">
<th v-if="selectionSupported" class="w-8 border-b px-3 py-2">
<input
type="checkbox"
class="h-3.5 w-3.5 accent-primary"
@@ -498,7 +588,7 @@ onBeforeUnmount(() => {
</thead>
<tbody>
<tr v-for="row in filteredRows" :key="row.id" class="border-b hover:bg-accent/40" :class="{ 'bg-primary/5': isOwnSession(row) }">
<td v-if="batchSupported" class="px-3 py-1.5">
<td v-if="selectionSupported" class="px-3 py-1.5">
<input
type="checkbox"
class="h-3.5 w-3.5 accent-primary"
@@ -551,7 +641,7 @@ onBeforeUnmount(() => {
</td>
</tr>
<tr v-if="!loading && filteredRows.length === 0">
<td :colspan="columns.length + 1 + (batchSupported ? 1 : 0)" class="px-3 py-10 text-center text-muted-foreground">
<td :colspan="columns.length + 1 + (selectionSupported ? 1 : 0)" class="px-3 py-10 text-center text-muted-foreground">
{{ search ? t("grid.noSearchResults") : t("processList.empty") }}
</td>
</tr>
@@ -652,6 +742,43 @@ onBeforeUnmount(() => {
</DialogContent>
</Dialog>
<Dialog
:open="batchTerminateTargets !== null"
@update:open="
(open) => {
if (!open && !terminating) batchTerminateTargets = null;
}
"
>
<DialogContent
class="max-w-md"
:show-close-button="!terminating"
@interact-outside="
(event) => {
if (terminating) event.preventDefault();
}
"
@escape-key-down="
(event) => {
if (terminating) event.preventDefault();
}
"
>
<DialogHeader>
<DialogTitle>{{ t("processList.batchTerminateTitle") }}</DialogTitle>
</DialogHeader>
<p class="text-sm text-muted-foreground">{{ t("processList.batchTerminateConfirm", { count: batchTerminateTargets?.length ?? 0 }) }}</p>
<p class="max-h-40 overflow-auto break-words font-mono text-xs">{{ batchTerminateTargets?.map((row) => row.id).join(", ") }}</p>
<DialogFooter>
<Button variant="outline" :disabled="terminating" @click="batchTerminateTargets = null">{{ t("dangerDialog.cancel") }}</Button>
<Button variant="destructive" :disabled="terminating" @click="confirmBatchTerminate">
<Loader2 v-if="terminating" class="mr-1.5 h-3.5 w-3.5 animate-spin" />
{{ t(terminating ? "processList.batchTerminateRunning" : "processList.terminate") }}
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
<Dialog
:open="batchResult !== null"
@update:open="
@@ -662,9 +789,9 @@ onBeforeUnmount(() => {
>
<DialogContent class="max-w-lg">
<DialogHeader>
<DialogTitle>{{ t("processList.batchTitle") }}</DialogTitle>
<DialogTitle>{{ t(batchResult?.kind === "terminate" ? "processList.batchTerminateTitle" : "processList.batchTitle") }}</DialogTitle>
</DialogHeader>
<p v-if="batchResult" class="text-sm">{{ t("processList.batchSummary", { succeeded: batchResult.succeeded, failed: batchResult.failures.length }) }}</p>
<p v-if="batchResult" class="text-sm">{{ t(batchResult.kind === "terminate" ? "processList.batchTerminateSummary" : "processList.batchSummary", { succeeded: batchResult.succeeded, failed: batchResult.failures.length }) }}</p>
<ul v-if="batchResult?.failures.length" class="max-h-60 space-y-2 overflow-auto text-xs text-destructive">
<li v-for="failure in batchResult.failures" :key="failure.id" class="break-words">{{ failure.id }}: {{ failure.message }}</li>
</ul>