feat(desktop): push publish tasks via AMQP topic dispatch WebSocket
Introduce a desktop.task.dispatch topic exchange with per-client routing keys. Tenant-api publishes a task_available frame keyed by target client ID when a publish job is created; every instance binds its own transient queue and forwards to the matching WebSocket (/api/desktop/dispatch) it owns. Desktop-client now prefers this push channel and only falls back to HTTP /lease polling when the socket is down. Also drop admin-web monitoring-plugin remnants and scope the publish modal account list to publish platforms. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -11,7 +11,12 @@ import { articlesApi, mediaApi, publishJobsApi, resolveApiURL, tenantAccountsApi
|
||||
import { coverUploadRequired, deriveCoverFileName } from "@/lib/cover-requirements";
|
||||
import { formatDateTime } from "@/lib/display";
|
||||
import { formatError } from "@/lib/errors";
|
||||
import { getPublishPlatformMeta, normalizePublishPlatformId, normalizePublishPlatformIds } from "@/lib/publish-platforms";
|
||||
import {
|
||||
getPublishPlatformMeta,
|
||||
isPublishPlatformId,
|
||||
normalizePublishPlatformId,
|
||||
normalizePublishPlatformIds,
|
||||
} from "@/lib/publish-platforms";
|
||||
|
||||
type PublishState = "immediate" | "queued" | "unavailable";
|
||||
|
||||
@@ -109,8 +114,19 @@ const platformMap = computed(() => {
|
||||
);
|
||||
});
|
||||
|
||||
const publishAccounts = computed(() => {
|
||||
return (accountsQuery.data.value ?? []).filter((account) => {
|
||||
if (account.deleted_at) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const platformId = normalizePublishPlatformId(account.platform);
|
||||
return platformMap.value.has(platformId) || isPublishPlatformId(platformId);
|
||||
});
|
||||
});
|
||||
|
||||
const rawAccountMap = computed(() => {
|
||||
return new Map((accountsQuery.data.value ?? []).map((account) => [account.id, account]));
|
||||
return new Map(publishAccounts.value.map((account) => [account.id, account]));
|
||||
});
|
||||
|
||||
const articlePlatformIds = computed(() => normalizePublishPlatformIds(detailQuery.data.value?.platforms ?? []));
|
||||
@@ -118,8 +134,7 @@ const articlePlatformIds = computed(() => normalizePublishPlatformIds(detailQuer
|
||||
const accountCards = computed<PublishAccountCard[]>(() => {
|
||||
const preferredPlatforms = new Set(articlePlatformIds.value);
|
||||
|
||||
return [...(accountsQuery.data.value ?? [])]
|
||||
.filter((account) => !account.deleted_at)
|
||||
return [...publishAccounts.value]
|
||||
.map((account) => {
|
||||
const platformId = normalizePublishPlatformId(account.platform);
|
||||
const platform = platformMap.value.get(platformId);
|
||||
|
||||
@@ -349,7 +349,7 @@ const zhCN = {
|
||||
collectBrandRequired: "请先选择品牌,再发起采集",
|
||||
collectKeywordRequired: "请先选择关键词,再立即采集当前问题",
|
||||
collectClientChecking: "正在检查当前账号的桌面客户端状态",
|
||||
collectClientOnlineRequired: "当前登录账号的 desktop-client 未在线,暂时不能立即采集",
|
||||
collectClientOnlineRequired: "当前登录账号的桌面客户端未在线,暂时不能立即采集",
|
||||
pluginMode: "插件采样",
|
||||
pluginHint: "插件模式下结果受设备在线率影响,低覆盖样本仅供趋势参考。",
|
||||
awaitingSnapshot: "等待下一次有效快照",
|
||||
|
||||
@@ -59,12 +59,7 @@ const errorMessageMap: Record<string, string> = {
|
||||
publisher_plugin_empty_response: "浏览器插件未返回数据,请刷新当前页面后重试",
|
||||
publisher_plugin_invalid_response: "浏览器插件返回了无效数据,请刷新当前页面后重试",
|
||||
publisher_plugin_unknown_error: "浏览器插件调用失败,请稍后重试",
|
||||
monitoring_plugin_timeout: "监测插件响应超时,请稍后重试",
|
||||
monitoring_plugin_invalid_response: "监测插件返回了无效数据,请稍后重试",
|
||||
monitoring_plugin_unknown_error: "监测插件调用失败,请稍后重试",
|
||||
monitoring_plugin_required: "请先安装并打开浏览器插件",
|
||||
monitoring_plugin_register_required: "浏览器插件正在初始化,请稍后重试",
|
||||
installation_offline: "监测插件离线,请打开浏览器插件后重试",
|
||||
installation_offline: "桌面客户端未在线,请打开桌面客户端后重试",
|
||||
network_error: "网络连接失败,请稍后重试",
|
||||
unknown_error: "发生未知错误",
|
||||
};
|
||||
|
||||
@@ -1,155 +0,0 @@
|
||||
import type {
|
||||
MonitoringCycleKickoffResponse,
|
||||
MonitoringCycleResult,
|
||||
MonitoringHeartbeatSyncResult,
|
||||
MonitoringLocalPlatformState,
|
||||
MonitoringPluginPingResponse,
|
||||
MonitoringRuntimeStateSnapshot,
|
||||
} from "@geo/shared-types";
|
||||
|
||||
type MonitoringAction = "ping" | "detectPlatforms" | "syncHeartbeat" | "runCycle" | "kickoffCycle" | "getState";
|
||||
|
||||
type MonitoringPluginSuccessPayload<T> = {
|
||||
source: "geo-extension";
|
||||
type: "geo.monitoring.response";
|
||||
requestId: string;
|
||||
success: true;
|
||||
data: T;
|
||||
};
|
||||
|
||||
type MonitoringPluginErrorPayload = {
|
||||
source: "geo-extension";
|
||||
type: "geo.monitoring.response";
|
||||
requestId: string;
|
||||
success: false;
|
||||
error: string;
|
||||
};
|
||||
|
||||
type MonitoringPluginPayloadMap = {
|
||||
ping: MonitoringPluginPingResponse;
|
||||
detectPlatforms: MonitoringLocalPlatformState[];
|
||||
syncHeartbeat: MonitoringHeartbeatSyncResult;
|
||||
runCycle: MonitoringCycleResult;
|
||||
kickoffCycle: MonitoringCycleKickoffResponse;
|
||||
getState: MonitoringRuntimeStateSnapshot;
|
||||
};
|
||||
|
||||
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
||||
return typeof value === "object" && value !== null;
|
||||
}
|
||||
|
||||
function isMonitoringPingPayload(value: unknown): value is MonitoringPluginPingResponse {
|
||||
return isPlainObject(value) && typeof value.installed === "boolean";
|
||||
}
|
||||
|
||||
function isMonitoringDetectPayload(value: unknown): value is MonitoringLocalPlatformState[] {
|
||||
return Array.isArray(value);
|
||||
}
|
||||
|
||||
function isMonitoringHeartbeatPayload(value: unknown): value is MonitoringHeartbeatSyncResult {
|
||||
return isPlainObject(value) && typeof value.success === "boolean" && Array.isArray(value.platform_states);
|
||||
}
|
||||
|
||||
function isMonitoringCyclePayload(value: unknown): value is MonitoringCycleResult {
|
||||
return (
|
||||
isPlainObject(value) &&
|
||||
typeof value.success === "boolean" &&
|
||||
typeof value.leased_task_count === "number" &&
|
||||
typeof value.processed_task_count === "number" &&
|
||||
typeof value.failed_task_count === "number" &&
|
||||
typeof value.skipped_task_count === "number"
|
||||
);
|
||||
}
|
||||
|
||||
function isMonitoringKickoffPayload(value: unknown): value is MonitoringCycleKickoffResponse {
|
||||
return isPlainObject(value) && value.accepted === true && typeof value.status === "string";
|
||||
}
|
||||
|
||||
function isMonitoringStatePayload(value: unknown): value is MonitoringRuntimeStateSnapshot {
|
||||
return isPlainObject(value) && typeof value.installation_key === "string" && Array.isArray(value.monitoring_platforms);
|
||||
}
|
||||
|
||||
function isMonitoringPayloadValid<T extends MonitoringAction>(action: T, value: unknown): value is MonitoringPluginPayloadMap[T] {
|
||||
switch (action) {
|
||||
case "ping":
|
||||
return isMonitoringPingPayload(value);
|
||||
case "detectPlatforms":
|
||||
return isMonitoringDetectPayload(value);
|
||||
case "syncHeartbeat":
|
||||
return isMonitoringHeartbeatPayload(value);
|
||||
case "runCycle":
|
||||
return isMonitoringCyclePayload(value);
|
||||
case "kickoffCycle":
|
||||
return isMonitoringKickoffPayload(value);
|
||||
case "getState":
|
||||
return isMonitoringStatePayload(value);
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
function nextRequestId(): string {
|
||||
if (typeof crypto !== "undefined" && "randomUUID" in crypto) {
|
||||
return crypto.randomUUID();
|
||||
}
|
||||
return `geo-monitoring-${Date.now()}-${Math.random().toString(36).slice(2)}`;
|
||||
}
|
||||
|
||||
async function requestMonitoringPlugin<T extends MonitoringAction>(action: T, timeoutMs = 5000): Promise<MonitoringPluginPayloadMap[T]> {
|
||||
if (typeof window === "undefined") {
|
||||
throw new Error("plugin bridge is only available in browser contexts");
|
||||
}
|
||||
|
||||
const requestId = nextRequestId();
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
const timer = window.setTimeout(() => {
|
||||
cleanup();
|
||||
reject(new Error("monitoring_plugin_timeout"));
|
||||
}, timeoutMs);
|
||||
|
||||
const handleMessage = (event: MessageEvent<MonitoringPluginSuccessPayload<MonitoringPluginPayloadMap[T]> | MonitoringPluginErrorPayload>) => {
|
||||
if (event.source !== window || !event.data || event.data.type !== "geo.monitoring.response") {
|
||||
return;
|
||||
}
|
||||
if (event.data.requestId !== requestId || event.data.source !== "geo-extension") {
|
||||
return;
|
||||
}
|
||||
|
||||
cleanup();
|
||||
if (event.data.success) {
|
||||
if (!isMonitoringPayloadValid(action, event.data.data)) {
|
||||
reject(new Error("monitoring_plugin_invalid_response"));
|
||||
return;
|
||||
}
|
||||
resolve(event.data.data);
|
||||
return;
|
||||
}
|
||||
reject(new Error(event.data.error || "monitoring_plugin_unknown_error"));
|
||||
};
|
||||
|
||||
const cleanup = () => {
|
||||
window.clearTimeout(timer);
|
||||
window.removeEventListener("message", handleMessage);
|
||||
};
|
||||
|
||||
window.addEventListener("message", handleMessage);
|
||||
window.postMessage(
|
||||
{
|
||||
source: "geo-admin",
|
||||
type: "geo.monitoring.request",
|
||||
requestId,
|
||||
action,
|
||||
},
|
||||
window.location.origin,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
export async function runMonitoringCycle(): Promise<MonitoringCycleResult> {
|
||||
return requestMonitoringPlugin("runCycle", 30000);
|
||||
}
|
||||
|
||||
export async function kickoffMonitoringCycle(): Promise<MonitoringCycleKickoffResponse> {
|
||||
return requestMonitoringPlugin("kickoffCycle", 5000);
|
||||
}
|
||||
Reference in New Issue
Block a user