From 68ecd7199183dcfa2b4f72a01ba2f97afc071ecf Mon Sep 17 00:00:00 2001 From: Joel Date: Thu, 21 Sep 2023 18:22:36 +0800 Subject: [PATCH 1/3] fix: handle timeout and get last batch --- .../share/text-generation/index.tsx | 3 ++- .../share/text-generation/result/index.tsx | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/web/app/components/share/text-generation/index.tsx b/web/app/components/share/text-generation/index.tsx index 4eba536b743a98..0a730519fe21e2 100644 --- a/web/app/components/share/text-generation/index.tsx +++ b/web/app/components/share/text-generation/index.tsx @@ -257,7 +257,8 @@ const TextGeneration: FC = ({ const batchCompletionResLatest = getBatchCompletionRes() const pendingTaskList = allTasklistLatest.filter(task => task.status === TaskStatus.pending) const hadRunedTaskNum = allTasklistLatest.filter(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)).length - const needToAddNextGroupTask = hadRunedTaskNum % GROUP_SIZE === 0 && pendingTaskList.length > 0 + const needToAddNextGroupTask = pendingTaskList.length > 0 && (hadRunedTaskNum % GROUP_SIZE === 0 || (allTasklistLatest.length - hadRunedTaskNum < GROUP_SIZE)) + console.log(`[#${taskId}]: ${isSuccess ? 'success' : 'fail'}. hadRunedTaskNum: ${hadRunedTaskNum}, needToAddNextGroupTask: ${needToAddNextGroupTask}`) const nextPendingTaskIds = needToAddNextGroupTask ? pendingTaskList.slice(0, GROUP_SIZE).map(item => item.id) : [] const newAllTaskList = allTasklistLatest.map((item) => { if (item.id === taskId) { diff --git a/web/app/components/share/text-generation/result/index.tsx b/web/app/components/share/text-generation/result/index.tsx index 4aba6a2574d982..b645b2b7028810 100644 --- a/web/app/components/share/text-generation/result/index.tsx +++ b/web/app/components/share/text-generation/result/index.tsx @@ -49,6 +49,9 @@ const Result: FC = ({ taskId, onCompleted, }) => { + useEffect(() => { + console.log(`[#${taskId}]: start`) + }, []) const [isResponsing, { setTrue: setResponsingTrue, setFalse: setResponsingFalse }] = useBoolean(false) useEffect(() => { if (controlStopResponding) @@ -128,6 +131,17 @@ const Result: FC = ({ onShowRes() setResponsingTrue() + const startTime = Date.now() + let isTimeout = false + const runId = setInterval(() => { + if (Date.now() - startTime > 1000 * 60) { // 1min timeout + clearInterval(runId) + setResponsingFalse() + onCompleted(getCompletionRes(), taskId, false) + isTimeout = true + console.log(`[#${taskId}]: timeout`) + } + }, 1000) sendCompletionMessage(data, { onData: (data: string, _isFirstMessage: boolean, { messageId }) => { tempMessageId = messageId @@ -135,13 +149,23 @@ const Result: FC = ({ setCompletionRes(res.join('')) }, onCompleted: () => { + if (isTimeout) + return + setResponsingFalse() setMessageId(tempMessageId) onCompleted(getCompletionRes(), taskId, true) + clearInterval(runId) + console.log(`[#${taskId}]: success`) }, onError() { + if (isTimeout) + return + setResponsingFalse() onCompleted(getCompletionRes(), taskId, false) + clearInterval(runId) + console.log(`[#${taskId}]: failed`) }, }, isInstalledApp, installedAppInfo?.id) } From bcd91b592f86bbb2a099c2e044e698a562c88882 Mon Sep 17 00:00:00 2001 From: Joel Date: Fri, 22 Sep 2023 11:01:15 +0800 Subject: [PATCH 2/3] fix: batch run bug --- .../share/text-generation/index.tsx | 21 +++++++++++++++---- .../share/text-generation/result/index.tsx | 5 ----- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/web/app/components/share/text-generation/index.tsx b/web/app/components/share/text-generation/index.tsx index 0a730519fe21e2..916254ab963411 100644 --- a/web/app/components/share/text-generation/index.tsx +++ b/web/app/components/share/text-generation/index.tsx @@ -104,10 +104,17 @@ const TextGeneration: FC = ({ const handleRetryAllFailedTask = () => { setControlRetry(Date.now()) } - const [allTaskList, setAllTaskList, getLatestTaskList] = useGetState([]) + const [allTaskList, doSetAllTaskList] = useState([]) + const allTaskListRef = useRef([]) + const getLatestTaskList = () => allTaskListRef.current + const setAllTaskList = (taskList: Task[]) => { + doSetAllTaskList(taskList) + allTaskListRef.current = taskList + } const pendingTaskList = allTaskList.filter(task => task.status === TaskStatus.pending) const noPendingTask = pendingTaskList.length === 0 const showTaskList = allTaskList.filter(task => task.status !== TaskStatus.pending) + const [currGroupNum, setCurrGroupNum, getCurrGroupNum] = useGetState(0) const allSuccessTaskList = allTaskList.filter(task => task.status === TaskStatus.completed) const allFailedTaskList = allTaskList.filter(task => task.status === TaskStatus.failed) const allTaskFinished = allTaskList.every(task => task.status === TaskStatus.completed) @@ -256,9 +263,15 @@ const TextGeneration: FC = ({ const allTasklistLatest = getLatestTaskList() const batchCompletionResLatest = getBatchCompletionRes() const pendingTaskList = allTasklistLatest.filter(task => task.status === TaskStatus.pending) - const hadRunedTaskNum = allTasklistLatest.filter(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)).length - const needToAddNextGroupTask = pendingTaskList.length > 0 && (hadRunedTaskNum % GROUP_SIZE === 0 || (allTasklistLatest.length - hadRunedTaskNum < GROUP_SIZE)) - console.log(`[#${taskId}]: ${isSuccess ? 'success' : 'fail'}. hadRunedTaskNum: ${hadRunedTaskNum}, needToAddNextGroupTask: ${needToAddNextGroupTask}`) + const hadRunedTaskNum = 1 + allTasklistLatest.filter(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)).length + const needToAddNextGroupTask = (getCurrGroupNum() !== hadRunedTaskNum) && pendingTaskList.length > 0 && (hadRunedTaskNum % GROUP_SIZE === 0 || (allTasklistLatest.length - hadRunedTaskNum < GROUP_SIZE)) + // avoid add many task at the same time + if (needToAddNextGroupTask) + setCurrGroupNum(hadRunedTaskNum) + // console.group() + // console.log(`[#${taskId}]: ${isSuccess ? 'success' : 'fail'}.currGroupNum: ${getCurrGroupNum()}.hadRunedTaskNum: ${hadRunedTaskNum}, needToAddNextGroupTask: ${needToAddNextGroupTask}`) + // console.log([...allTasklistLatest.filter(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)).map(item => item.id), taskId].sort((a: any, b: any) => a - b).join(',')) + // console.groupEnd() const nextPendingTaskIds = needToAddNextGroupTask ? pendingTaskList.slice(0, GROUP_SIZE).map(item => item.id) : [] const newAllTaskList = allTasklistLatest.map((item) => { if (item.id === taskId) { diff --git a/web/app/components/share/text-generation/result/index.tsx b/web/app/components/share/text-generation/result/index.tsx index b645b2b7028810..73ec5c2c7468cd 100644 --- a/web/app/components/share/text-generation/result/index.tsx +++ b/web/app/components/share/text-generation/result/index.tsx @@ -49,9 +49,6 @@ const Result: FC = ({ taskId, onCompleted, }) => { - useEffect(() => { - console.log(`[#${taskId}]: start`) - }, []) const [isResponsing, { setTrue: setResponsingTrue, setFalse: setResponsingFalse }] = useBoolean(false) useEffect(() => { if (controlStopResponding) @@ -156,7 +153,6 @@ const Result: FC = ({ setMessageId(tempMessageId) onCompleted(getCompletionRes(), taskId, true) clearInterval(runId) - console.log(`[#${taskId}]: success`) }, onError() { if (isTimeout) @@ -165,7 +161,6 @@ const Result: FC = ({ setResponsingFalse() onCompleted(getCompletionRes(), taskId, false) clearInterval(runId) - console.log(`[#${taskId}]: failed`) }, }, isInstalledApp, installedAppInfo?.id) } From 93666bd599a2ec211885a999cce346ee2d5ebeb8 Mon Sep 17 00:00:00 2001 From: Joel Date: Fri, 22 Sep 2023 11:12:04 +0800 Subject: [PATCH 3/3] feat: handle runing status --- web/app/components/share/text-generation/index.tsx | 2 ++ .../share/text-generation/run-batch/index.tsx | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/web/app/components/share/text-generation/index.tsx b/web/app/components/share/text-generation/index.tsx index 916254ab963411..e82b67d817c53a 100644 --- a/web/app/components/share/text-generation/index.tsx +++ b/web/app/components/share/text-generation/index.tsx @@ -118,6 +118,7 @@ const TextGeneration: FC = ({ const allSuccessTaskList = allTaskList.filter(task => task.status === TaskStatus.completed) const allFailedTaskList = allTaskList.filter(task => task.status === TaskStatus.failed) const allTaskFinished = allTaskList.every(task => task.status === TaskStatus.completed) + const allTaskRuned = allTaskList.every(task => [TaskStatus.completed, TaskStatus.failed].includes(task.status)) const [batchCompletionRes, setBatchCompletionRes, getBatchCompletionRes] = useGetState>({}) const exportRes = allTaskList.map((task) => { const batchCompletionResLatest = getBatchCompletionRes() @@ -502,6 +503,7 @@ const TextGeneration: FC = ({ diff --git a/web/app/components/share/text-generation/run-batch/index.tsx b/web/app/components/share/text-generation/run-batch/index.tsx index 78645bc87d10b9..849d02445c891d 100644 --- a/web/app/components/share/text-generation/run-batch/index.tsx +++ b/web/app/components/share/text-generation/run-batch/index.tsx @@ -5,18 +5,21 @@ import { PlayIcon, } from '@heroicons/react/24/solid' import { useTranslation } from 'react-i18next' +import cn from 'classnames' import CSVReader from './csv-reader' import CSVDownload from './csv-download' import Button from '@/app/components/base/button' - +import { Loading02 } from '@/app/components/base/icons/src/vender/line/general' export type IRunBatchProps = { vars: { name: string }[] onSend: (data: string[][]) => void + isAllFinished: boolean } const RunBatch: FC = ({ vars, onSend, + isAllFinished, }) => { const { t } = useTranslation() @@ -31,6 +34,7 @@ const RunBatch: FC = ({ const handleSend = () => { onSend(csvData) } + const Icon = isAllFinished ? PlayIcon : Loading02 return (
@@ -41,9 +45,9 @@ const RunBatch: FC = ({ type="primary" className='mt-4 !h-8 !pl-3 !pr-4' onClick={handleSend} - disabled={!isParsed} + disabled={!isParsed || !isAllFinished} > -