新增迁移50与每5分钟/手动持久同步,API只读本地;同步失败保留、权限阻断、租约与质量失效原子激活。 保留全量稳定分页/固定列,已存质量批量读取,元数据不反复解压长答案;详情安全Markdown与原文。 已运行整仓 make verify、源与副本隔离PG -race、真实只读源及隔离浏览器。 实际同步2525条;固定窗口640条本地中位1.46秒,对照直接源33.10秒。 停worker、缺源凭据、重启API后仍可查询;未更新8088,未修改生产原问答。
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
import { useEffect, useRef, useState } from 'react'
|
||||
import { fetchQAMirrorStatus, mirrorSyncActive, mirrorStateChanged, startQAMirrorSync } from './qaMirror'
|
||||
import type { QAMirrorStatus } from './qaMirror'
|
||||
import { newOperationID } from './operationKey'
|
||||
import './qaMirror.css'
|
||||
interface ViewProps {
|
||||
status: QAMirrorStatus | null
|
||||
error: string
|
||||
submitting: boolean
|
||||
onSync: () => void
|
||||
}
|
||||
const stamp = (v: string) => new Date(v).toLocaleString('zh-CN', { hour12: false })
|
||||
export function QAMirrorSyncView(p: ViewProps) {
|
||||
const s = p.status
|
||||
const active = mirrorSyncActive(s)
|
||||
return (
|
||||
<section className="qa-mirror-sync" aria-label="问答本地数据库与同步状态">
|
||||
<div className="qa-mirror-sync-line">
|
||||
<strong>本地数据库</strong>
|
||||
<span>每 5 分钟后台同步 · 查询不请求远端</span>
|
||||
{s?.ready ? <span>已同步 {s.records} 条</span> : null}
|
||||
<button type="button" onClick={p.onSync} disabled={p.submitting || !s?.worker_ready || active}>
|
||||
{p.submitting ? '正在入队…' : active ? '后台同步中…' : '立即同步'}
|
||||
</button>
|
||||
</div>
|
||||
<div role="status" className="qa-ops-muted">
|
||||
{!s
|
||||
? '正在读取同步状态…'
|
||||
: s.blocked
|
||||
? '源端权限校验未通过,本地读取已阻断;修复授权后可重新同步。'
|
||||
: !s.ready
|
||||
? '首次同步尚未完成,暂不把未同步数据视为空结果。'
|
||||
: `最近成功同步:${stamp(s.last_success_at!)};页面基于该本地副本,不承诺实时源快照。`}
|
||||
{s?.job && active ? (
|
||||
<span>
|
||||
{' '}
|
||||
{s.job.expected > 0
|
||||
? `已检查 ${s.job.loaded} / ${s.job.expected} 条,取回变化 ${s.job.changed} 条。`
|
||||
: '正在检查源端变化。'}
|
||||
</span>
|
||||
) : null}
|
||||
{s?.error_code && s.error_code !== 'initial_sync_pending' && !s.blocked ? (
|
||||
<span className="qa-ops-warning"> 最近同步失败,保留上次成功数据;请检查源端连接后重试。</span>
|
||||
) : null}
|
||||
{s && !s.worker_ready ? <span className="qa-ops-warning"> 同步执行器未在线。</span> : null}
|
||||
</div>
|
||||
{p.error ? (
|
||||
<p className="error-banner" role="alert">
|
||||
{p.error}
|
||||
</p>
|
||||
) : null}
|
||||
</section>
|
||||
)
|
||||
}
|
||||
export function QAMirrorSync({ onChanged }: { onChanged: () => void }) {
|
||||
const [status, setStatus] = useState<QAMirrorStatus | null>(null),
|
||||
[error, setError] = useState(''),
|
||||
[submitting, setSubmitting] = useState(false)
|
||||
const key = useRef('')
|
||||
const acceptedJob = useRef('')
|
||||
const changed = useRef(onChanged)
|
||||
changed.current = onChanged
|
||||
const observed = useRef<{ revision: number; blocked: boolean; ready: boolean } | undefined>(undefined)
|
||||
const kick = useRef<() => void>(() => {})
|
||||
useEffect(() => {
|
||||
let disposed = false,
|
||||
timer: ReturnType<typeof setTimeout> | undefined
|
||||
const controller = new AbortController()
|
||||
let inFlight = false
|
||||
const read = async () => {
|
||||
if (disposed || inFlight) return
|
||||
inFlight = true
|
||||
let next = 15000
|
||||
try {
|
||||
const fresh = await fetchQAMirrorStatus(controller.signal)
|
||||
if (disposed) return
|
||||
const previous = observed.current
|
||||
observed.current = { revision: fresh.revision, blocked: fresh.blocked, ready: fresh.ready }
|
||||
setStatus(fresh)
|
||||
setError('')
|
||||
if (mirrorStateChanged(previous, fresh)) changed.current()
|
||||
if (!mirrorSyncActive(fresh) && fresh.job?.id === acceptedJob.current) {
|
||||
key.current = ''
|
||||
acceptedJob.current = ''
|
||||
}
|
||||
if (mirrorSyncActive(fresh) || !fresh.ready) next = 2500
|
||||
} catch (e) {
|
||||
if (!disposed) setError(e instanceof Error ? e.message : '本地同步状态读取失败')
|
||||
} finally {
|
||||
inFlight = false
|
||||
if (!disposed) {
|
||||
clearTimeout(timer)
|
||||
timer = setTimeout(() => void read(), next)
|
||||
}
|
||||
}
|
||||
}
|
||||
kick.current = () => {
|
||||
clearTimeout(timer)
|
||||
void read()
|
||||
}
|
||||
void read()
|
||||
return () => {
|
||||
disposed = true
|
||||
controller.abort()
|
||||
clearTimeout(timer)
|
||||
kick.current = () => {}
|
||||
}
|
||||
}, [])
|
||||
const synchronize = async () => {
|
||||
if (submitting || mirrorSyncActive(status)) return
|
||||
setSubmitting(true)
|
||||
setError('')
|
||||
if (!key.current) key.current = newOperationID()
|
||||
try {
|
||||
acceptedJob.current = await startQAMirrorSync(key.current)
|
||||
kick.current()
|
||||
} catch (e) {
|
||||
setError(e instanceof Error ? e.message : '同步请求未确认,请重试,已有本地数据不变')
|
||||
} finally {
|
||||
setSubmitting(false)
|
||||
}
|
||||
}
|
||||
return <QAMirrorSyncView status={status} error={error} submitting={submitting} onSync={() => void synchronize()} />
|
||||
}
|
||||
Reference in New Issue
Block a user