package task // 任务状态 const ( Draft = "draft" PendingReview = "pending_review" Rejected = "rejected" Queued = "queued" Dispatched = "dispatched" Running = "running" Success = "success" Failed = "failed" Suspended = "suspended" Cancelled = "cancelled" ) // 动作 type Action string const ( ActionSubmit Action = "submit" ActionApprove Action = "approve" ActionReject Action = "reject" ActionResubmit Action = "resubmit" ActionDispatch Action = "dispatch" ActionStart Action = "start" ActionSuccess Action = "success" ActionFail Action = "fail" ActionRetry Action = "retry" ActionSuspend Action = "suspend" ActionResume Action = "resume" ActionCancel Action = "cancel" ) // transitions 状态转移表 var transitions = map[string]map[Action]string{ Draft: { ActionSubmit: PendingReview, ActionCancel: Cancelled, }, PendingReview: { ActionApprove: Queued, ActionReject: Rejected, ActionCancel: Cancelled, }, Rejected: { ActionResubmit: PendingReview, ActionCancel: Cancelled, }, Queued: { ActionDispatch: Dispatched, ActionCancel: Cancelled, }, Dispatched: { ActionStart: Running, ActionFail: Failed, ActionCancel: Cancelled, }, Running: { ActionSuccess: Success, ActionFail: Failed, ActionSuspend: Suspended, ActionCancel: Cancelled, }, Failed: { ActionRetry: Queued, ActionCancel: Cancelled, }, Suspended: { ActionResume: Queued, ActionCancel: Cancelled, }, Success: {}, Cancelled: {}, } // Next 校验并返回下一状态;非法转移返回 ok=false func Next(current string, a Action) (string, bool) { next, ok := transitions[current][a] return next, ok } // Can 当前状态是否接受该动作 func Can(current string, a Action) bool { _, ok := transitions[current][a] return ok }