src/main/kotlin/cn/flightfeather/supervision/common/executor/BackgroundTaskCtrl.kt
@@ -1,5 +1,6 @@
package cn.flightfeather.supervision.common.executor
import cn.flightfeather.supervision.common.exception.ResponseErrorException
import org.springframework.stereotype.Component
import java.time.LocalDateTime
import java.util.concurrent.ConcurrentHashMap
@@ -20,13 +21,13 @@
    /**
     * 新增任务
     */
    @Throws(IllegalStateException::class)
    @Throws(ResponseErrorException::class)
    fun newTask(type: BgTaskType, id: String, name: String, task: () -> Boolean): BgTask {
        if (!taskCollection.containsKey(type)) {
            taskCollection[type] = ConcurrentHashMap<String, BgTask>()
        }
        val taskSet = taskCollection[type]!!
        if (taskSet.containsKey(id)) throw IllegalStateException("无法创建任务, 任务[${name}]的id重复")
        if (taskSet.containsKey(id)) throw ResponseErrorException("无法创建任务, 任务[${name}]的id重复")
        val t = BgTask(type, id, name, task)
        taskSet[id] = t
        return t
@@ -35,20 +36,20 @@
    /**
     * 开始任务
     */
    @Throws(IllegalStateException::class)
    @Throws(ResponseErrorException::class)
    fun startTask(type: BgTaskType, id: String): BgTask {
        val taskSet = taskCollection[type] ?: throw throw IllegalStateException("无法开启任务,该任务类型[${type.des}]不存在")
        val t = taskSet[id] ?: throw IllegalStateException("无法开启任务,该任务[${id}]不存在")
        val taskSet = taskCollection[type] ?: throw throw ResponseErrorException("无法开启任务,该任务类型[${type.des}]不存在")
        val t = taskSet[id] ?: throw ResponseErrorException("无法开启任务,该任务[${id}]不存在")
        return startTask(t)
    }
    @Throws(IllegalStateException::class)
    @Throws(ResponseErrorException::class)
    fun startTask(task: BgTask): BgTask {
        if (task.taskStatus.status != TaskStatus.WAITING) {
            if (task.taskStatus.status == TaskStatus.RUNNING) {
                throw IllegalStateException("无法开启任务,任务[${task.name}]正在执行")
                throw ResponseErrorException("无法开启任务,任务[${task.name}]正在执行")
            } else {
                throw IllegalStateException("无法开启任务,任务[${task.name}]已结束")
                throw ResponseErrorException("无法开启任务,任务[${task.name}]已结束")
            }
        } else {
            task.ready()
@@ -60,7 +61,7 @@
    /**
     * 新增并开始任务
     */
    @Throws(IllegalStateException::class)
    @Throws(ResponseErrorException::class)
    fun startNewTask(type: BgTaskType, id: String, name: String, task: () -> Boolean): BgTask {
        val t = newTask(type, id, name, task)
        return startTask(t)
@@ -95,11 +96,11 @@
    /**
     * 强制关闭任务
     */
    @Throws(IllegalStateException::class)
    @Throws(ResponseErrorException::class)
    fun shutDownTask(type: BgTaskType, id: String?): List<BgTaskStatus?> {
        val taskMap = taskCollection[type] ?: throw IllegalStateException("无法关闭任务,任务类型[${type.des}]未创建")
        val taskMap = taskCollection[type] ?: throw ResponseErrorException("无法关闭任务,任务类型[${type.des}]未创建")
        return if (id != null) {
            val task = taskMap[id] ?: throw IllegalStateException("无法关闭任务,任务[${id}]不存在")
            val task = taskMap[id] ?: throw ResponseErrorException("无法关闭任务,任务[${id}]不存在")
            task.shutdown()
            listOf(task.taskStatus)
        } else {
@@ -112,11 +113,11 @@
        }
    }
    @Throws(IllegalStateException::class)
    @Throws(ResponseErrorException::class)
    fun removeTask(type: BgTaskType, id: String): Boolean {
        val statusList = shutDownTask(type, id)
        if (statusList.isNotEmpty()) {
            val s = statusList.first() ?: throw IllegalStateException("无法移除任务,任务不存在")
            val s = statusList.first() ?: throw ResponseErrorException("无法移除任务,任务不存在")
            taskCollection[s.type]?.remove(s.id)
            return true
        }