merge(douyu): 修复履约待办高频自动刷新

This commit is contained in:
yml
2026-11-18 19:54:11 +08:00
5 changed files with 52 additions and 3 deletions
@@ -0,0 +1,30 @@
import assert from 'node:assert/strict'
import test from 'node:test'
import { isFinancePipelineRealtimeJobId } from './scheduler-service.js'
test('只有财务履约管道任务允许广播 fulfillment_pipeline.changed', () => {
for (const jobId of [
'channel-assign',
'carrier-dispatch',
'product-normalize',
'cost-snapshot',
'sales-facts',
'ledger-post',
]) {
assert.equal(isFinancePipelineRealtimeJobId(jobId), true)
}
for (const jobId of [
'voucher-consume-dispatch',
'pdd_order_sync',
'work-order-timeout',
'deposit-unfreeze',
'order-service-auto-publish',
'data-retention',
'',
null,
]) {
assert.equal(isFinancePipelineRealtimeJobId(jobId), false)
}
})
@@ -20,6 +20,18 @@ import { publishRealtimeEvent } from '../realtime/realtime-event-service.js'
const timers = new Map<string, NodeJS.Timeout>() const timers = new Map<string, NodeJS.Timeout>()
const jobStates = new Map<string, JsonObject>() const jobStates = new Map<string, JsonObject>()
const FINANCE_PIPELINE_REALTIME_JOB_IDS = new Set([
'channel-assign',
'carrier-dispatch',
'product-normalize',
'cost-snapshot',
'sales-facts',
'ledger-post',
])
export function isFinancePipelineRealtimeJobId(jobId: unknown) {
return FINANCE_PIPELINE_REALTIME_JOB_IDS.has(String(jobId || '').trim())
}
export function startScheduledJobs() { export function startScheduledJobs() {
reloadScheduledJobs() reloadScheduledJobs()
@@ -204,13 +216,15 @@ async function runJob(job: JsonObject, { manual = false }: { manual?: boolean }
} }
function publishPipelineRealtimeChange(jobId: unknown, phase: string) { function publishPipelineRealtimeChange(jobId: unknown, phase: string) {
const normalizedJobId = String(jobId || '').trim()
if (!isFinancePipelineRealtimeJobId(normalizedJobId)) return
publishRealtimeEvent({ publishRealtimeEvent({
type: 'fulfillment_pipeline.changed', type: 'fulfillment_pipeline.changed',
entityId: 0, entityId: 0,
scopes: ['admin_fulfillment_pipelines'], scopes: ['admin_fulfillment_pipelines'],
operation: 'refresh', operation: 'refresh',
data: { data: {
jobId: String(jobId || '').trim(), jobId: normalizedJobId,
phase: String(phase || '').trim(), phase: String(phase || '').trim(),
}, },
admin: true, admin: true,
@@ -564,7 +564,6 @@ function applyAdminRealtimeEvent(
event.scopes.includes('admin_fulfillment_pipelines') event.scopes.includes('admin_fulfillment_pipelines')
) { ) {
void queryClient.invalidateQueries({ queryKey: ['fulfillment-pipelines'] }) void queryClient.invalidateQueries({ queryKey: ['fulfillment-pipelines'] })
void queryClient.invalidateQueries({ queryKey: ['fulfillment-pending'] })
} }
if (event.type === 'work_order.changed' && event.scopes.includes('admin_work_orders')) { if (event.type === 'work_order.changed' && event.scopes.includes('admin_work_orders')) {
@@ -668,7 +668,7 @@ function PendingPanel() {
<PercentTable <PercentTable
rowKey={(row) => rowKey(row)} rowKey={(row) => rowKey(row)}
size="small" size="small"
loading={query.isFetching} loading={query.isLoading}
dataSource={query.data?.data?.items || []} dataSource={query.data?.data?.items || []}
columns={columns} columns={columns}
pagination={buildAdminTablePagination({ pagination={buildAdminTablePagination({
@@ -26,6 +26,12 @@ test('履约管道状态由后台 SSE 失效刷新,不再 30 秒轮询', () =>
assert.match(page, /SSE\s+自动刷新/) assert.match(page, /SSE\s+自动刷新/)
assert.ok(layout.includes("event.type === 'fulfillment_pipeline.changed'")) assert.ok(layout.includes("event.type === 'fulfillment_pipeline.changed'"))
assert.ok(layout.includes("queryKey: ['fulfillment-pipelines']")) assert.ok(layout.includes("queryKey: ['fulfillment-pipelines']"))
const pipelineRealtimeBlock = layout.slice(
layout.indexOf("event.type === 'fulfillment_pipeline.changed'"),
layout.indexOf("event.type === 'work_order.changed'"),
)
assert.ok(!pipelineRealtimeBlock.includes("queryKey: ['fulfillment-pending']"))
assert.ok(page.includes('loading={query.isLoading}'))
}) })
test('公共订单 SSE 允许匿名连接且只携带重读信号', () => { test('公共订单 SSE 允许匿名连接且只携带重读信号', () => {