From a700aeb0a07d11da1e6b2ae999983ba17a415c70 Mon Sep 17 00:00:00 2001
From: feiyu02 <risaku@163.com>
Date: 星期三, 30 七月 2025 16:02:04 +0800
Subject: [PATCH] 2025.7.30 1. 新增基础数据产品相关接口(待完成)
---
src/main/kotlin/cn/flightfeather/supervision/common/executor/BackgroundTaskCtrl.kt | 50 +++++++++++++++++++++++++++++++++-----------------
1 files changed, 33 insertions(+), 17 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..492e15e 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,8 @@
package cn.flightfeather.supervision.common.executor
+import cn.flightfeather.supervision.common.exception.BizException
+import cn.flightfeather.supervision.socket.WebSocketSendMessageUtil
import org.springframework.stereotype.Component
-import java.time.LocalDateTime
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.Executors
import kotlin.jvm.Throws
@@ -20,14 +21,27 @@
/**
* 鏂板浠诲姟
*/
- @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閲嶅")
- val t = BgTask(type, id, name, task)
+ // 鍒ゆ柇浠诲姟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) { status ->
+ // 鍙戦�佹秷鎭�
+ WebSocketSendMessageUtil.sendBgTaskMessage(status)
+ }
taskSet[id] = t
return t
}
@@ -35,24 +49,26 @@
/**
* 寮�濮嬩换鍔�
*/
- @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()
- task.future = executorService.submit { task.execute() }
+ task.future = executorService.submit {
+ task.execute()
+ }
return task
}
}
@@ -60,7 +76,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 +111,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 +128,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