From 6c7f45871b93ef26d353a5a3596701ac2f39ed9c Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期二, 10 九月 2024 17:48:27 +0800
Subject: [PATCH] 1. 新增问题更新接口

---
 src/main/kotlin/cn/flightfeather/supervision/common/executor/BackgroundTaskCtrl.kt |   40 +++++++++++++++++++++++++---------------
 1 files changed, 25 insertions(+), 15 deletions(-)

diff --git a/src/main/kotlin/cn/flightfeather/supervision/common/executor/BackgroundTaskCtrl.kt b/src/main/kotlin/cn/flightfeather/supervision/common/executor/BackgroundTaskCtrl.kt
index 72423b9..c8f23f6 100644
--- a/src/main/kotlin/cn/flightfeather/supervision/common/executor/BackgroundTaskCtrl.kt
+++ b/src/main/kotlin/cn/flightfeather/supervision/common/executor/BackgroundTaskCtrl.kt
@@ -1,7 +1,7 @@
 package cn.flightfeather.supervision.common.executor
 
+import cn.flightfeather.supervision.common.exception.BizException
 import org.springframework.stereotype.Component
-import java.time.LocalDateTime
 import java.util.concurrent.ConcurrentHashMap
 import java.util.concurrent.Executors
 import kotlin.jvm.Throws
@@ -20,13 +20,23 @@
     /**
      * 鏂板浠诲姟
      */
-    @Throws(IllegalStateException::class)
+    @Throws(BizException::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}]鐨刬d閲嶅")
+        // 鍒ゆ柇浠诲姟id鏄惁瀛樺湪
+        if (taskSet.containsKey(id)) {
+            val oldTask = taskSet[id]
+            // 褰撲换鍔″凡缁忕粨鏉熸椂锛岀洿鎺ュ垹闄わ紝鏇挎崲涓烘柊浠诲姟
+            if (oldTask?.taskStatus?.status != TaskStatus.WAITING || oldTask.taskStatus.status != TaskStatus.RUNNING) {
+                taskSet.remove(id)
+            }
+            else {
+                throw BizException("鏃犳硶閲嶅鍒涘缓浠诲姟")
+            }
+        }
         val t = BgTask(type, id, name, task)
         taskSet[id] = t
         return t
@@ -35,20 +45,20 @@
     /**
      * 寮�濮嬩换鍔�
      */
-    @Throws(IllegalStateException::class)
+    @Throws(BizException::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 BizException("鏃犳硶寮�鍚换鍔★紝璇ヤ换鍔$被鍨媅${type.des}]涓嶅瓨鍦�")
+        val t = taskSet[id] ?: throw BizException("鏃犳硶寮�鍚换鍔★紝璇ヤ换鍔${id}]涓嶅瓨鍦�")
         return startTask(t)
     }
 
-    @Throws(IllegalStateException::class)
+    @Throws(BizException::class)
     fun startTask(task: BgTask): BgTask {
         if (task.taskStatus.status != TaskStatus.WAITING) {
             if (task.taskStatus.status == TaskStatus.RUNNING) {
-                throw IllegalStateException("鏃犳硶寮�鍚换鍔★紝浠诲姟[${task.name}]姝e湪鎵ц")
+                throw BizException("鏃犳硶寮�鍚换鍔★紝浠诲姟[${task.name}]姝e湪鎵ц")
             } else {
-                throw IllegalStateException("鏃犳硶寮�鍚换鍔★紝浠诲姟[${task.name}]宸茬粨鏉�")
+                throw BizException("鏃犳硶寮�鍚换鍔★紝浠诲姟[${task.name}]宸茬粨鏉�")
             }
         } else {
             task.ready()
@@ -60,7 +70,7 @@
     /**
      * 鏂板骞跺紑濮嬩换鍔�
      */
-    @Throws(IllegalStateException::class)
+    @Throws(BizException::class)
     fun startNewTask(type: BgTaskType, id: String, name: String, task: () -> Boolean): BgTask {
         val t = newTask(type, id, name, task)
         return startTask(t)
@@ -95,11 +105,11 @@
     /**
      * 寮哄埗鍏抽棴浠诲姟
      */
-    @Throws(IllegalStateException::class)
+    @Throws(BizException::class)
     fun shutDownTask(type: BgTaskType, id: String?): List<BgTaskStatus?> {
-        val taskMap = taskCollection[type] ?: throw IllegalStateException("鏃犳硶鍏抽棴浠诲姟锛屼换鍔$被鍨媅${type.des}]鏈垱寤�")
+        val taskMap = taskCollection[type] ?: throw BizException("鏃犳硶鍏抽棴浠诲姟锛屼换鍔$被鍨媅${type.des}]鏈垱寤�")
         return if (id != null) {
-            val task = taskMap[id] ?: throw IllegalStateException("鏃犳硶鍏抽棴浠诲姟锛屼换鍔${id}]涓嶅瓨鍦�")
+            val task = taskMap[id] ?: throw BizException("鏃犳硶鍏抽棴浠诲姟锛屼换鍔${id}]涓嶅瓨鍦�")
             task.shutdown()
             listOf(task.taskStatus)
         } else {
@@ -112,11 +122,11 @@
         }
     }
 
-    @Throws(IllegalStateException::class)
+    @Throws(BizException::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 BizException("鏃犳硶绉婚櫎浠诲姟锛屼换鍔′笉瀛樺湪")
             taskCollection[s.type]?.remove(s.id)
             return true
         }

--
Gitblit v1.9.3