From c95f66f4b81e81df9432c0c6d306ad22e3f5e587 Mon Sep 17 00:00:00 2001 From: hcong <1050828145@qq.com> Date: 星期一, 02 十二月 2024 11:41:09 +0800 Subject: [PATCH] socket以及后台任务状态实时刷新 --- src/main/kotlin/cn/flightfeather/supervision/common/executor/BackgroundTaskCtrl.kt | 50 ++++++++++++++++++++++++++++++++++---------------- 1 files changed, 34 insertions(+), 16 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..730c3a6 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.common.utils.SendSocketMessageUtil 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 +21,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,24 +46,29 @@ /** * 寮�濮嬩换鍔� */ - @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() + println(task.taskStatus.runTime) + SendSocketMessageUtil.sendBgTaskMessage(task.taskStatus) + } + SendSocketMessageUtil.sendBgTaskMessage(task.taskStatus) 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,28 +111,30 @@ /** * 寮哄埗鍏抽棴浠诲姟 */ - @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() + SendSocketMessageUtil.sendBgTaskMessage(task.taskStatus) listOf(task.taskStatus) } else { val res = mutableListOf<BgTaskStatus?>() taskMap.forEach { (t, u) -> u.shutdown() + SendSocketMessageUtil.sendBgTaskMessage(u.taskStatus) res.add(u.taskStatus) } res.sortedBy { it?.createTime } } } - @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