Files
orbit/pulse/model/state.go

58 lines
2.3 KiB
Go

package model
// TaskState 表示任务状态,与执行尝试状态独立;任务终态不重新打开。
type TaskState string
const (
TaskWaiting TaskState = "WAITING" // 等待派发、延时或退避到期。
TaskLeased TaskState = "LEASED" // 已领取但尚未获得开始授权。
TaskRunning TaskState = "RUNNING" // 已授权处理器开始执行。
TaskNeedsReview TaskState = "NEEDS_REVIEW" // 结果未知或需要人工核查。
TaskSucceeded TaskState = "SUCCEEDED" // 业务处理器成功结果已保存。
TaskFailed TaskState = "FAILED" // 已知失败且不再继续执行。
TaskCancelled TaskState = "CANCELLED" // 取消已确认,不能理解为业务回滚。
)
func (s TaskState) Terminal() bool {
return s == TaskSucceeded || s == TaskFailed || s == TaskCancelled
}
// CanTransitionTo 只检查状态图,不能代替数据库事务中的权限、版本、租约、
// 暂停门控、副作用证据和重试预算校验。
func (s TaskState) CanTransitionTo(next TaskState) bool {
switch s {
case TaskWaiting:
return next == TaskLeased || next == TaskCancelled || next == TaskFailed
case TaskLeased:
return next == TaskRunning || next == TaskWaiting || next == TaskCancelled || next == TaskFailed
case TaskRunning:
return next == TaskSucceeded || next == TaskWaiting || next == TaskFailed || next == TaskNeedsReview || next == TaskCancelled
case TaskNeedsReview:
return next == TaskSucceeded || next == TaskFailed
default:
return false
}
}
type AttemptState string
const (
AttemptLeased AttemptState = "LEASED"
AttemptRunning AttemptState = "RUNNING"
AttemptSucceeded AttemptState = "SUCCEEDED"
AttemptFailed AttemptState = "FAILED"
AttemptCancelled AttemptState = "CANCELLED"
AttemptExpired AttemptState = "EXPIRED"
AttemptUnknown AttemptState = "UNKNOWN"
)
// RecoverySafety 表示恢复安全等级,空值或未知值默认需要核查。
type RecoverySafety string
const (
RecoveryIdempotent RecoverySafety = "IDEMPOTENT" // 接入方明确保证可幂等恢复。
RecoveryReconcileOnly RecoverySafety = "RECONCILE_ONLY" // 恢复前必须查询结果或人工核查。
)
func (s RecoverySafety) CanRecoverAutomatically() bool { return s == RecoveryIdempotent }