Files
dify/web/app/components/plugins/install-plugin/base/check-task-status.ts
T
7cafd65d54 refactor(web): migrate handwritten console API calls to the generated oRPC contract (#44169)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
2026-10-16 03:47:31 +00:00

62 lines
1.5 KiB
TypeScript

import { checkTaskStatus as fetchCheckTaskStatus } from '@/service/plugins'
import { sleep } from '@/utils'
import { TaskStatus } from '../../types'
const INTERVAL = 10 * 1000 // 10 seconds
const isUnfinishedStatus = (status: TaskStatus) =>
status === TaskStatus.pending || status === TaskStatus.running
type Params = {
taskId: string
pluginUniqueIdentifier: string
}
function checkTaskStatus() {
let nextStatus: TaskStatus = TaskStatus.running
let isStop = false
const doCheckStatus = async ({ taskId, pluginUniqueIdentifier }: Params) => {
if (isStop) {
return {
status: TaskStatus.success,
}
}
const res = await fetchCheckTaskStatus(taskId)
const { plugins } = res.task
const plugin = plugins.find((p) => p.plugin_unique_identifier === pluginUniqueIdentifier)
if (!plugin) {
nextStatus = TaskStatus.failed
return {
status: TaskStatus.failed,
error: 'Plugin package not found',
}
}
nextStatus = plugin.status
if (isUnfinishedStatus(nextStatus)) {
await sleep(INTERVAL)
return await doCheckStatus({
taskId,
pluginUniqueIdentifier,
})
}
if (nextStatus === TaskStatus.failed) {
return {
status: TaskStatus.failed,
error: plugin.message,
}
}
return {
status: TaskStatus.success,
}
}
return {
check: doCheckStatus,
stop: () => {
isStop = true
},
}
}
export default checkTaskStatus