Skip to content

Commit

Permalink
修复文件延迟访问的问题,加个轮询 (#92)
Browse files Browse the repository at this point in the history
<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

- **New Features**
- Enhanced synchronization process with real-time updates on log status.
	- Dynamic button text changes based on synchronization state.

- **Bug Fixes**
	- Improved control flow for showing logs and handling synchronization.

- **Refactor**
- Updated functions for better clarity and efficiency in handling log
states.
	- Introduced a new polling mechanism to check log readiness.
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
  • Loading branch information
yoyo837 authored Oct 17, 2024
1 parent f965cac commit 7d98385
Showing 1 changed file with 65 additions and 7 deletions.
72 changes: 65 additions & 7 deletions src/components/Sync.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,71 @@ interface SyncProps {
pkgName: string;
}

const LogStatus = {
WAIT: 1,
ERROR: 2,
SUCCESS: 3,
}

export default function Sync({ pkgName }: SyncProps) {
const [logId, setLogId] = React.useState<string>();
const [logState, setLogState] = React.useState<number>(LogStatus.WAIT);
const retryCountRef = React.useRef(0);
const [modal, contextHolder] = Modal.useModal();

function genLogFileUrl(id: string) {
return `${REGISTRY}/-/package/${pkgName}/syncs/${id}/log`;
}

async function showLog(id: string) {
modal.success({
title: '等待调度',
content: (
<>
创建同步任务成功,正在等待调度,如遇日志 404 请稍后刷新重试,通常需要几十秒钟的时间
<Link target="_blank" href={`${REGISTRY}/-/package/${pkgName}/syncs/${id}/log`}>
<Link target="_blank" href={genLogFileUrl(id)}>
查看日志
</Link>
</>
),
});
}

async function logPolling(id:string) {
if (!id) {
return;
}
retryCountRef.current += 1;
try {
const response = await fetch(genLogFileUrl(id));
if (response.status === 200) {
setLogState(LogStatus.SUCCESS);
return;
}
throw new Error('Not ready');
} catch {
if (retryCountRef.current > 30) {
setLogState(LogStatus.ERROR);
return;
}
setTimeout(() => {
logPolling(id);
}, 1000);
}
}

async function doSync() {
try {
const res = await fetch(`${SYNC_REGISTRY}/-/package/${pkgName}/syncs`, {
const response = await fetch(`${SYNC_REGISTRY}/-/package/${pkgName}/syncs`, {
method: 'PUT',
}).then((res) => res.json());
})
const res = await response.json();
if (res.ok) {
setLogId(res.id);
logPolling(res.id);
return;
}
showLog(res.id);
throw new Error('Not ok');
} catch (e) {
message.error('创建同步任务失败');
}
Expand All @@ -43,9 +81,29 @@ export default function Sync({ pkgName }: SyncProps) {
return (
<>
{contextHolder}
<Button size={'small'} type="primary" onClick={logId ? () => showLog(logId) : doSync}>
{logId ? '查看日志' : '进行同步'}
<Button size={'small'} type="primary" loading={ !!logId && logState === 1 } onClick={() => {
if (!logId) {
doSync();
return;
}
if (logState === LogStatus.SUCCESS) {
showLog(logId);
}
}}>
{(() => {
if (logId) {
switch (logState) {
case LogStatus.SUCCESS:
return <>查看日志</>;
case LogStatus.ERROR:
return <>调度失败</>;
default:
return <>等待调度</>;
}
}
return <>进行同步</>;
})()}
</Button>
</>
);
}
}

0 comments on commit 7d98385

Please sign in to comment.