Skip to content

Commit

Permalink
Merge pull request #3767 from irwinsun/issue_3400
Browse files Browse the repository at this point in the history
feat: 引擎核心代码拆分出biz-process业务模块 #3400
  • Loading branch information
irwinsun authored Mar 10, 2021
2 parents 7de8de1 + a180a41 commit 01cd772
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,22 @@ enum class ActionType {
TERMINATE, // 终止
;

fun isStartOrRefresh() = isStart() || this == REFRESH

fun isStart() = START == this || RETRY == this

fun isEnd() = END == this || isTerminate()

fun isTerminate() = TERMINATE == this

companion object {
fun isStart(actionType: ActionType) = START == actionType || RETRY == actionType
fun isEnd(actionType: ActionType) = END == actionType || isTerminate(actionType)
fun isTerminate(actionType: ActionType) = TERMINATE == actionType
@Deprecated(replaceWith = ReplaceWith("isStart"), message = "replace by isStart")
fun isStart(actionType: ActionType) = actionType.isStart()

@Deprecated(replaceWith = ReplaceWith("isEnd"), message = "replace by isEnd")
fun isEnd(actionType: ActionType) = actionType.isEnd()

@Deprecated(replaceWith = ReplaceWith("isTerminate"), message = "replace by isTerminate")
fun isTerminate(actionType: ActionType) = actionType.isTerminate()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ class TaskControl @Autowired constructor(
// 构建机的任务不在此运行
if (taskAtomService.runByVmTask(buildTask!!)) {
// 构建机上运行中任务目前无法直接后台干预,便在此处设置状态,使流程继续
if (ActionType.isEnd(actionType)) {
if (actionType.isEnd()) {
LOG.info("ENGINE|$buildId|$source|ATOM_$actionType|$stageId|j($containerId)|t($taskId)|code=$errorCode")
val buildStatus = BuildStatus.CANCELED
val atomResponse = AtomResponse(
Expand Down Expand Up @@ -150,7 +150,7 @@ class TaskControl @Autowired constructor(
*/
private fun runTask(userId: String, actionType: ActionType, buildTask: PipelineBuildTask) = when {
buildTask.status.isReadyToRun() -> { // 准备启动执行
if (ActionType.isEnd(actionType)) { // #2400 因任务终止&结束的事件命令而未执行的原子设置为UNEXEC,而不是SKIP
if (actionType.isEnd()) { // #2400 因任务终止&结束的事件命令而未执行的原子设置为UNEXEC,而不是SKIP
pipelineRuntimeService.updateTaskStatus(
task = buildTask, userId = userId, buildStatus = BuildStatus.UNEXEC
)
Expand All @@ -160,7 +160,7 @@ class TaskControl @Autowired constructor(
}
}
buildTask.status.isRunning() -> { // 运行中的,检查是否运行结束,以及决定是否强制终止
atomBuildStatus(taskAtomService.tryFinish(task = buildTask, force = ActionType.isTerminate(actionType)))
atomBuildStatus(taskAtomService.tryFinish(task = buildTask, force = actionType.isTerminate()))
}
else -> buildTask.status // 其他状态不做动作
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@

package com.tencent.devops.process.engine.control.command.container.impl

import com.tencent.devops.common.event.enums.ActionType
import com.tencent.devops.common.pipeline.enums.BuildStatus
import com.tencent.devops.common.pipeline.enums.ContainerMutexStatus
import com.tencent.devops.process.engine.control.MutexControl
Expand Down Expand Up @@ -55,7 +54,7 @@ class CheckMutexContainerCmd(

override fun execute(commandContext: ContainerContext) {
// 终止或者结束事件不做互斥判断
if (!ActionType.isEnd(commandContext.event.actionType)) {
if (!commandContext.event.actionType.isEnd()) {
mutexCheck(commandContext)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,14 @@ class StartActionTaskContainerCmd(
val actionType = commandContext.event.actionType
commandContext.cmdFlowState = CmdFlowState.FINALLY
when {
ActionType.isStart(actionType) || ActionType.REFRESH == actionType || ActionType.isEnd(actionType) -> {
if (!ActionType.isTerminate(actionType)) {
actionType.isStartOrRefresh() || actionType.isEnd() -> {
if (!actionType.isTerminate()) {
commandContext.buildStatus = BuildStatus.SUCCEED
}
val waitToDoTask = findTask(commandContext)
if (waitToDoTask == null) { // 非fast kill的强制终止时到最后无任务,最终状态必定是FAILED
val fastKill = FastKillUtils.isFastKillCode(commandContext.event.errorCode)
if (!fastKill && ActionType.isTerminate(actionType) && !commandContext.buildStatus.isFailure()) {
if (!fastKill && actionType.isTerminate() && !commandContext.buildStatus.isFailure()) {
commandContext.buildStatus = BuildStatus.FAILED
}
commandContext.latestSummary = "status=${commandContext.buildStatus}"
Expand Down Expand Up @@ -113,8 +113,10 @@ class StartActionTaskContainerCmd(
if (t.status.isPause()) { // 若为暂停,则要确保拿到的任务为stopVM-关机或者空任务发送next stage任务
toDoTask = findNextTaskAfterPause(containerContext, currentTask = t)
breakFlag = toDoTask == null
} else if (t.status.isRunning()) {
breakFlag = ActionType.isStart(containerContext.event.actionType)
} else if (t.status.isRunning()) { // 当前有运行中任务
// 如果是要启动或者刷新, 当前已经有运行中任务,则需要break
breakFlag = containerContext.event.actionType.isStartOrRefresh()
// 如果是要终止,则需要拿出当前任务进行终止
toDoTask = findRunningTask(containerContext, currentTask = t)
} else if (t.status.isFailure() || t.status.isCancel()) {
// 当前任务已经失败or取消,并且没有设置[失败继续]的, 设置给容器最终FAILED状态
Expand Down Expand Up @@ -151,7 +153,7 @@ class StartActionTaskContainerCmd(
}

private fun isTerminate(containerContext: ContainerContext): Boolean {
return ActionType.isTerminate(containerContext.event.actionType) ||
return containerContext.event.actionType.isTerminate() ||
FastKillUtils.isFastKillCode(containerContext.event.errorCode)
}

Expand All @@ -161,7 +163,7 @@ class StartActionTaskContainerCmd(
): PipelineBuildTask? {
var toDoTask: PipelineBuildTask? = null
when {
ActionType.isTerminate(containerContext.event.actionType) -> { // 终止命令,需要设置失败,并返回
containerContext.event.actionType.isTerminate() -> { // 终止命令,需要设置失败,并返回
containerContext.buildStatus = BuildStatus.RUNNING
toDoTask = currentTask // 将当前任务传给TaskControl做终止
buildLogPrinter.addRedLine(
Expand All @@ -172,7 +174,7 @@ class StartActionTaskContainerCmd(
executeCount = toDoTask.executeCount ?: 1
)
}
ActionType.isEnd(containerContext.event.actionType) -> { // 将当前正在运行的任务传给TaskControl做结束
containerContext.event.actionType.isEnd() -> { // 将当前正在运行的任务传给TaskControl做结束
containerContext.buildStatus = BuildStatus.RUNNING
toDoTask = currentTask
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class CheckInterruptStageCmd : StageCmd {
val fastKillHasFailureJob = parseFastKill(commandContext = commandContext)
// [终止事件]或[等待审核超时] 直接结束流水线,不需要判断各个Stage的状态,可直接停止
val fastKill = commandContext.fastKill
if (fastKill || ActionType.isTerminate(commandContext.event.actionType)) {
if (fastKill || commandContext.event.actionType.isTerminate()) {
val event = commandContext.event
LOG.info("ENGINE|${event.buildId}|${event.source}|STAGE_INTERRUPT|${event.stageId}|fastKill=$fastKill")
commandContext.buildStatus = detectStageInterruptStatus(commandContext, fastKillHasFailureJob)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,8 @@ class StartContainerStageCmd(
}

when {
ActionType.isStart(newActionType) -> stageStatus = BuildStatus.RUNNING // 要启动Stage
ActionType.isEnd(newActionType) -> stageStatus = BuildStatus.CANCELED // 若为终止命令,直接设置为取消
newActionType.isStart() -> stageStatus = BuildStatus.RUNNING // 要启动Stage
newActionType.isEnd() -> stageStatus = BuildStatus.CANCELED // 若为终止命令,直接设置为取消
newActionType == ActionType.SKIP -> stageStatus = BuildStatus.SKIP // 要跳过Stage
}
}
Expand Down Expand Up @@ -118,7 +118,7 @@ class StartContainerStageCmd(
// } else if (c.status.isReadyToRun() && !ActionType.isStart(actionType)) {
// 失败或可重试的容器,如果不是重试动作,则跳过
// stageStatus = BuildStatus.RUNNING
} else if (c.status.isRunning() && !ActionType.isTerminate(actionType)) {
} else if (c.status.isRunning() && !actionType.isTerminate()) {
// 已经在运行中的, 只接受强制终止
stageStatus = BuildStatus.RUNNING
} else if (!c.status.isFinish()) {
Expand Down

0 comments on commit 01cd772

Please sign in to comment.