feiyu02
2024-01-09 c1becf4cbd2e99601ce011c14b8742427249cfb4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package cn.flightfeather.supervision.lightshare.service.impl
 
import cn.flightfeather.supervision.common.executor.BackgroundTaskCtrl
import cn.flightfeather.supervision.common.executor.BgTaskConditionVo
import cn.flightfeather.supervision.common.executor.BgTaskStatus
import cn.flightfeather.supervision.common.executor.BgTaskType
import cn.flightfeather.supervision.lightshare.service.BgTaskService
import org.springframework.stereotype.Service
 
@Service
class BgTaskServiceImpl(private val backgroundTaskCtrl: BackgroundTaskCtrl) : BgTaskService {
 
    override fun getTaskStatus(condition: BgTaskConditionVo): List<BgTaskStatus?> {
        return backgroundTaskCtrl.getTaskStatus(condition.type, condition.id)
    }
 
    override fun startTask(condition: BgTaskConditionVo): BgTaskStatus? {
        condition.type ?: throw IllegalStateException("任务类型不能为空")
        condition.id ?: throw IllegalStateException("任务id不能为空")
 
        val task = backgroundTaskCtrl.startTask(condition.type!!, condition.id!!)
        return task.taskStatus
    }
 
    override fun shutDownTask(condition: BgTaskConditionVo): List<BgTaskStatus?> {
        condition.type ?: throw IllegalStateException("任务类型不能为空")
        return backgroundTaskCtrl.shutDownTask(condition.type!!, condition.id)
    }
 
    override fun removeTask(condition: BgTaskConditionVo): Boolean {
        condition.type ?: throw IllegalStateException("任务类型不能为空")
        condition.id ?: throw IllegalStateException("任务id不能为空")
        return backgroundTaskCtrl.removeTask(condition.type!!, condition.id!!)
    }
 
    override fun startNewTestTask(taskId: String): BgTaskStatus? {
        val task = backgroundTaskCtrl.startNewTask(BgTaskType.TEST, taskId, "测试任务-${taskId}") { testTask() }
        return task.taskStatus
    }
 
    override fun newTestTask(taskId: String): BgTaskStatus? {
        val task = backgroundTaskCtrl.newTask(BgTaskType.TEST, taskId, "测试任务-${taskId}") { testTask() }
        return task.taskStatus
    }
 
    private fun testTask(): Boolean {
        var times = 10
        while (times > 0) {
            Thread.sleep(2000)
            times--
        }
        return true
    }
}