Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.5 KB
Raw Blame
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