完成手动续期后台任务及权限隔离并更新回归进度
保留管理与业务续期调度,增加系统级幂等任务、取消传播和统计审计原子收尾;前端跟踪真实任务状态。原生 Linux 全后端、静态检查、相关专项二十次重复及前端二百七十二项测试和生产构建通过,未进行正式生产部署。
This commit is contained in:
1 parent
4ab3f93beb
commit
413a0e66a9
25 files changed
+739
-107
No files matched your search
@@ -3,6 +3,7 @@ import { settingsPayload } from "./SettingsPayload.js";
|
||||
import { deploymentSummary, serverAuthLabel } from "./DeploymentFeedback.js";
|
||||
import { useDomainForm } from "./DomainForm.js";
|
||||
import { useTaskMonitor } from "./TaskMonitor.js";
|
||||
import { useRenewalAction } from "./RenewalAction.js";
|
||||
|
||||
const defaultDNSVerificationTimeoutSeconds = 60;
|
||||
const aliyunDNSVerificationTimeoutSeconds = 6;
|
||||
@@ -82,6 +83,9 @@ export function useDashboard() {
|
||||
taskAttempts, taskDetailsLoading, taskDetailsError,
|
||||
refreshResources: refreshTaskResources,
|
||||
});
|
||||
const { runRenewalNow, renewalSubmitting } = useRenewalAction({
|
||||
canRunRenewal, error, notice, fetchAdmin, taskMonitor,
|
||||
});
|
||||
const hasOpenModal = computed(
|
||||
() =>
|
||||
showForm.value ||
|
||||
@@ -143,6 +147,7 @@ export function useDashboard() {
|
||||
};
|
||||
taskStatusLabels.error = taskStatusLabels.failed;
|
||||
const taskTypeLabels = {
|
||||
renewal_check: "全局续期检查",
|
||||
management_renewal: "管理证书续期",
|
||||
certificate_issue: "申请证书",
|
||||
certificate_import: "导入证书",
|
||||
@@ -153,6 +158,7 @@ export function useDashboard() {
|
||||
dns_test: "DNS 在线测试",
|
||||
};
|
||||
const auditActionLabels = {
|
||||
renewal_check: "全局续期检查",
|
||||
management_renewal: "管理证书续期",
|
||||
certificate_issue: "申请证书",
|
||||
certificate_import: "导入证书",
|
||||
@@ -170,6 +176,7 @@ export function useDashboard() {
|
||||
renewal_run: "执行自动续期",
|
||||
};
|
||||
const taskStageLabels = {
|
||||
renewal_check: "检查管理证书和业务域名续期",
|
||||
management_renewal: "管理证书续期",
|
||||
preflight: "续期前置检查",
|
||||
issue: "申请管理证书",
|
||||
@@ -680,25 +687,6 @@ export function useDashboard() {
|
||||
}
|
||||
}
|
||||
|
||||
async function runRenewalNow() {
|
||||
if (!canRunRenewal.value) {
|
||||
error.value = "只有超级管理员可以执行续期检查";
|
||||
return;
|
||||
}
|
||||
error.value = "";
|
||||
notice.value = "";
|
||||
try {
|
||||
const response = await fetchAdmin("/api/v1/renewal/run", { method: "POST" });
|
||||
const result = await response.json().catch(() => ({}));
|
||||
if (!response.ok) throw new Error(result.error || "执行续期检查失败");
|
||||
const report = result.report || {};
|
||||
notice.value = `续期检查完成:检查 ${report.checked || 0} 项,选中 ${report.selected || 0} 项,成功 ${report.succeeded || 0} 项,失败 ${report.failed || 0} 项`;
|
||||
await loadData();
|
||||
} catch (err) {
|
||||
error.value = err.message || "执行续期检查失败";
|
||||
}
|
||||
}
|
||||
|
||||
function handleDNSProviderChange() {
|
||||
dnsRequestVersion += 1;
|
||||
stopDNSVerificationTimer();
|
||||
@@ -1159,6 +1147,7 @@ export function useDashboard() {
|
||||
taskStageLabel,
|
||||
attemptStatusLabel,
|
||||
runRenewalNow,
|
||||
renewalSubmitting,
|
||||
openSettings,
|
||||
closeSettings,
|
||||
saveSettings,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import assert from "node:assert/strict";
|
||||
import "./DeploymentResult.test.js";
|
||||
import "./TaskMonitor.test.js";
|
||||
import "./RenewalAction.test.js";
|
||||
import "./DashboardLogout.test.js";
|
||||
import "./DomainModal.test.js";
|
||||
import "./DomainForm.test.js";
|
||||
@@ -1087,7 +1088,7 @@ test("workflowTasks.renderOnlySupportedActions", async () => {
|
||||
});
|
||||
assert.deepEqual(compiled.errors, []);
|
||||
const render = new Function("Vue", compiled.code)(Vue);
|
||||
for (const taskType of ["certificate_issue", "renewal_run", "certificate_deploy", "management_renewal", "unknown"]) {
|
||||
for (const taskType of ["certificate_issue", "certificate_import", "certificate_validate", "renewal_check", "renewal_run", "certificate_deploy", "management_renewal", "unknown"]) {
|
||||
for (const status of ["queued", "running", "success", "failed", "cancelled", "partial_success"]) {
|
||||
const html = await renderToString(Vue.createSSRApp({
|
||||
render,
|
||||
@@ -1104,7 +1105,7 @@ test("workflowTasks.renderOnlySupportedActions", async () => {
|
||||
retryTask: () => {},
|
||||
}),
|
||||
}));
|
||||
const canCancel = ["certificate_issue", "certificate_deploy"].includes(taskType) && ["queued", "running"].includes(status);
|
||||
const canCancel = ["certificate_issue", "certificate_deploy", "certificate_import", "certificate_validate", "renewal_check"].includes(taskType) && ["queued", "running"].includes(status);
|
||||
const canRetry = (taskType === "certificate_issue" && ["failed", "cancelled"].includes(status)) || (taskType === "certificate_deploy" && ["failed", "cancelled", "partial_success"].includes(status));
|
||||
const buttonCount = (html.match(/<button\b/g) || []).length;
|
||||
assert.equal(buttonCount, canCancel || canRetry ? 2 : 1, `${taskType}:${status}`);
|
||||
|
||||
@@ -107,6 +107,7 @@ const {
|
||||
validateDomain,
|
||||
deployDomain,
|
||||
runRenewalNow,
|
||||
renewalSubmitting,
|
||||
canRunRenewal,
|
||||
cancelTask,
|
||||
openTaskDetails,
|
||||
@@ -137,8 +138,14 @@ const { activeSection, activeLabel, sections, selectSection } = useDashboardNavi
|
||||
<button class="secondary-button" type="button" @click="loadData">
|
||||
刷新状态
|
||||
</button>
|
||||
<button v-if="activeSection === 'activity' && canRunRenewal" class="secondary-button" type="button" @click="runRenewalNow">
|
||||
立即检查续期
|
||||
<button
|
||||
v-if="activeSection === 'activity' && canRunRenewal"
|
||||
class="secondary-button"
|
||||
type="button"
|
||||
:disabled="renewalSubmitting"
|
||||
@click="runRenewalNow"
|
||||
>
|
||||
{{ renewalSubmitting ? "正在提交" : "立即检查续期" }}
|
||||
</button>
|
||||
<button
|
||||
class="secondary-button"
|
||||
|
||||
@@ -0,0 +1,44 @@
|
||||
import { ref } from "vue";
|
||||
|
||||
export function useRenewalAction({ canRunRenewal, error, notice, fetchAdmin, taskMonitor }) {
|
||||
const renewalSubmitting = ref(false);
|
||||
let idempotencyKey = "";
|
||||
|
||||
async function runRenewalNow() {
|
||||
if (!canRunRenewal.value) {
|
||||
error.value = "只有超级管理员可以执行续期检查";
|
||||
return;
|
||||
}
|
||||
if (renewalSubmitting.value) return;
|
||||
renewalSubmitting.value = true;
|
||||
error.value = "";
|
||||
notice.value = "";
|
||||
try {
|
||||
if (!idempotencyKey) {
|
||||
const bytes = crypto.getRandomValues(new Uint8Array(16));
|
||||
idempotencyKey = `renewal-check-${Array.from(bytes, (value) => value.toString(16).padStart(2, "0")).join("")}`;
|
||||
}
|
||||
const response = await fetchAdmin("/api/v1/renewal/run", {
|
||||
method: "POST", headers: { "Idempotency-Key": idempotencyKey },
|
||||
});
|
||||
const result = await response.json().catch(() => ({}));
|
||||
if ((response.ok || response.status === 409 && result.replayed) &&
|
||||
result.task?.id && result.task.task_type === "renewal_check" &&
|
||||
(response.status === 202 || result.replayed)) {
|
||||
idempotencyKey = "";
|
||||
notice.value = `续期检查任务已提交:${result.task.id},请在任务详情查看最终结果`;
|
||||
await taskMonitor.openTaskDetails(result.task);
|
||||
return;
|
||||
}
|
||||
if (!response.ok) throw new Error(result.error || "提交续期检查失败");
|
||||
throw new Error("服务未返回有效续期任务,请在运行概览确认是否已接收,勿重复提交");
|
||||
} catch (err) {
|
||||
// 未确认受理结果时保留幂等键,重试只找回原任务,不重放续期。
|
||||
error.value = err.message || "无法确认续期检查是否已接收,请先查看运行概览";
|
||||
} finally {
|
||||
renewalSubmitting.value = false;
|
||||
}
|
||||
}
|
||||
|
||||
return { runRenewalNow, renewalSubmitting };
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
import assert from "node:assert/strict";
|
||||
import test from "node:test";
|
||||
import { ref } from "vue";
|
||||
import { useRenewalAction } from "./RenewalAction.js";
|
||||
import { useDashboard } from "./Dashboard.js";
|
||||
import { deferred, mountComposable } from "../../test-support/mount.js";
|
||||
|
||||
const task = { id: "renewal-check-task", task_type: "renewal_check", status: "queued" };
|
||||
|
||||
test("续期提交防重复点击,响应丢失后保留幂等键找回原任务", async () => {
|
||||
const pending = deferred();
|
||||
const keys = [];
|
||||
const opened = [];
|
||||
const error = ref("");
|
||||
const notice = ref("");
|
||||
const state = useRenewalAction({
|
||||
canRunRenewal: ref(true), error, notice,
|
||||
fetchAdmin: async (_, options) => {
|
||||
keys.push(options.headers["Idempotency-Key"]);
|
||||
if (keys.length === 1) return pending.promise;
|
||||
return Response.json({ task, replayed: true });
|
||||
},
|
||||
taskMonitor: { openTaskDetails: async (item) => opened.push(item) },
|
||||
});
|
||||
const request = state.runRenewalNow();
|
||||
assert.equal(state.renewalSubmitting.value, true);
|
||||
await state.runRenewalNow();
|
||||
assert.equal(keys.length, 1);
|
||||
pending.reject(new Error("连接中断"));
|
||||
await request;
|
||||
assert.equal(state.renewalSubmitting.value, false);
|
||||
assert.equal(notice.value, "");
|
||||
await state.runRenewalNow();
|
||||
assert.equal(keys[0], keys[1]);
|
||||
assert.match(keys[0], /^[A-Za-z0-9._-]{8,128}$/);
|
||||
assert.equal(opened.length, 1);
|
||||
assert.equal(error.value, "");
|
||||
assert.match(notice.value, /已提交/);
|
||||
assert.doesNotMatch(notice.value, /续期检查完成/);
|
||||
await state.runRenewalNow();
|
||||
assert.notEqual(keys[1], keys[2]);
|
||||
});
|
||||
|
||||
for (const response of [
|
||||
{ status: 202, body: {} },
|
||||
{ status: 200, body: { report: { succeeded: 5 } } },
|
||||
{ status: 202, body: { task: { ...task, task_type: "certificate_issue" } } },
|
||||
{ status: 503, body: { error: "任务队列已满" } },
|
||||
]) {
|
||||
test(`续期无有效受理结果不显示完成:${response.status} ${JSON.stringify(response.body)}`, async () => {
|
||||
const error = ref("");
|
||||
const notice = ref("");
|
||||
const state = useRenewalAction({
|
||||
canRunRenewal: ref(true), error, notice,
|
||||
fetchAdmin: async () => Response.json(response.body, { status: response.status }),
|
||||
taskMonitor: { openTaskDetails: () => assert.fail("无有效任务不得打开详情") },
|
||||
});
|
||||
await state.runRenewalNow();
|
||||
assert.ok(error.value);
|
||||
assert.equal(notice.value, "");
|
||||
});
|
||||
}
|
||||
|
||||
test("真实续期入口跟踪已有任务,失败摘要持久化后可重新打开", async (t) => {
|
||||
let current = { ...task };
|
||||
let posts = 0;
|
||||
const { state } = await mountComposable(t, useDashboard, async (path, options = {}) => {
|
||||
if (path === "/api/v1/renewal/run") {
|
||||
posts++;
|
||||
assert.match(options.headers["Idempotency-Key"], /^[A-Za-z0-9._-]{8,128}$/);
|
||||
return Response.json({ task: current, replayed: true }, { status: 409 });
|
||||
}
|
||||
if (path === `/api/v1/tasks/${task.id}`) return Response.json(current);
|
||||
if (path.endsWith("/attempts")) return Response.json({ items: [{ attempt_number: 1, status: current.status }] });
|
||||
if (path.startsWith("/api/v1/tasks?")) return Response.json({ items: [current] });
|
||||
return Response.json({ items: [] });
|
||||
});
|
||||
state.currentUser.value = { role: "super_admin" };
|
||||
await state.runRenewalNow();
|
||||
assert.equal(posts, 1);
|
||||
assert.equal(state.error.value, "");
|
||||
assert.equal(state.showTaskDetails.value, true);
|
||||
assert.equal(state.taskDetails.value.status, "queued");
|
||||
current = { ...current, status: "partial_success", message: "续期检查未全部完成:成功 1 项,失败 1 项" };
|
||||
await state.refreshTaskStatus();
|
||||
assert.equal(state.taskDetails.value.message, current.message);
|
||||
assert.equal(state.taskDeploymentReport.value, null);
|
||||
state.closeTaskDetails();
|
||||
await state.openTaskDetails(current);
|
||||
assert.equal(state.taskDetails.value.status, "partial_success");
|
||||
assert.equal(state.taskTypeLabel("renewal_check"), "全局续期检查");
|
||||
assert.equal(posts, 1);
|
||||
});
|
||||
@@ -78,6 +78,7 @@ export function useRuntimeOverview(state) {
|
||||
}, clock.value));
|
||||
const visibleTasks = computed(() => detailView.value === "tasks" ? summary.value.tasks : summary.value.tasks.slice(0, 5));
|
||||
function taskDomain(task) {
|
||||
if (task.task_type === "renewal_check") return "管理证书与全部业务域名";
|
||||
if (task.task_type === "management_renewal") return "管理页面证书";
|
||||
return state.domains.value.find((item) => item.id === task.domain_id)?.name || "系统任务或已移除域名";
|
||||
}
|
||||
|
||||
@@ -92,7 +92,7 @@ const { detailView, summary, visibleTasks, taskDomain } = useRuntimeOverview(pro
|
||||
<td>
|
||||
<button type="button" @click="openTaskDetails(item)">查看详情</button>
|
||||
<button
|
||||
v-if="(item.task_type === 'certificate_issue' || item.task_type === 'certificate_deploy') && (item.status === 'queued' || item.status === 'running')"
|
||||
v-if="['certificate_issue', 'certificate_deploy', 'certificate_import', 'certificate_validate', 'renewal_check'].includes(item.task_type) && (item.status === 'queued' || item.status === 'running')"
|
||||
type="button"
|
||||
@click="cancelTask(item)"
|
||||
>取消任务</button>
|
||||
|
||||
Reference in new issue
Block a user