94 lines
5.8 KiB
Go
94 lines
5.8 KiB
Go
package instance
|
||
|
||
import (
|
||
"context"
|
||
"encoding/json"
|
||
"time"
|
||
|
||
"git.yuntuo.hk/k2safe/orbit/internal/fault"
|
||
core "git.yuntuo.hk/k2safe/orbit/model"
|
||
"git.yuntuo.hk/k2safe/orbit/platform/auth"
|
||
"git.yuntuo.hk/k2safe/orbit/pulse/service"
|
||
)
|
||
|
||
// ActivationSummary 描述指定历史激活的当前记录,不携带大输入/结果或执行凭据。
|
||
type ActivationSummary struct {
|
||
ID core.ID `json:"id"` // ID 激活身份,人工恢复时新建且保留原记录。
|
||
StepID core.ID `json:"step_id"` // StepID 本激活所属逻辑步骤。
|
||
Sequence uint32 `json:"sequence"` // Sequence 该步骤的激活代次,和POLL业务轮次分开。
|
||
State string `json:"state"` // State 此激活状态,不代表实例当前状态。
|
||
Revision uint64 `json:"revision,string"` // Revision 此激活记录的版本,保留整数精度。
|
||
TaskID *core.ID `json:"task_id,omitempty"` // TaskID TASK的直接任务;POLL从轮次列表查询。
|
||
CreatedAt time.Time `json:"created_at"` // CreatedAt 激活创建时间,不代表恢复获得新截止。
|
||
UpdatedAt time.Time `json:"updated_at"` // UpdatedAt 此记录最后变更时间。
|
||
CompletedAt *time.Time `json:"completed_at,omitempty"` // CompletedAt 此激活已保存的结束时间;未记录时省略。
|
||
}
|
||
type ActivationPage struct {
|
||
InstanceID core.ID `json:"instance_id"` // InstanceID 已授权的所属实例。
|
||
Revision uint64 `json:"revision,string"` // Revision 本页读取时的实例版本;跨页不保证同一快照。
|
||
StepID core.ID `json:"step_id"` // StepID 已核验的逻辑步骤。
|
||
CurrentActivationID *core.ID `json:"current_activation_id,omitempty"` // CurrentActivationID 本页读取时的当前激活,未激活时省略。
|
||
Items []ActivationSummary `json:"items"` // Items 按代次倒序的有界摘要,空页为[]。
|
||
NextCursor string `json:"next_cursor,omitempty"` // NextCursor 绑定实例、步骤和查询类型的后续位置。
|
||
}
|
||
type ActivationDetail struct {
|
||
InstanceID core.ID `json:"instance_id"` // InstanceID 所属实例。
|
||
Revision uint64 `json:"revision,string"` // Revision 读取详情时的实例版本。
|
||
Step StepView `json:"step"` // Step 逻辑步骤的当前视图,不能替代Activation中的历史代次。
|
||
Activation ActivationSummary `json:"activation"` // Activation 指定的历史激活,不自动改为当前激活。
|
||
Input json.RawMessage `json:"input"` // Input 此激活原输入,显式null保留。
|
||
Output json.RawMessage `json:"output,omitempty"` // Output 此激活当前保存的结果,缺失与显式null不同。
|
||
Poll *PollView `json:"poll,omitempty"` // Poll 此激活的查询预算及最后任务,只用于POLL。
|
||
Wait *WaitView `json:"wait,omitempty"` // Wait 此激活的等待记录,恢复不修改旧记录的截止。
|
||
}
|
||
type PollRoundSummary struct {
|
||
ID core.ID `json:"id"` // ID 该任务唯一对应的轮次记录。
|
||
ActivationID core.ID `json:"activation_id"` // ActivationID 产生本次查询的激活。
|
||
Round uint32 `json:"round"` // Round 从首次激活累计的业务轮编号;恢复同轮不重置。
|
||
TaskID core.ID `json:"task_id"` // TaskID 该轮Pulse任务,尝试和结果由任务接口读取。
|
||
CreatedAt time.Time `json:"created_at"` // CreatedAt 本次轮次记录创建时间。
|
||
}
|
||
type PollRoundPage struct {
|
||
InstanceID core.ID `json:"instance_id"` // InstanceID 所属实例。
|
||
Revision uint64 `json:"revision,string"` // Revision 本页读取时的实例版本。
|
||
ActivationID core.ID `json:"activation_id"` // ActivationID 明确指定的历史激活。
|
||
Items []PollRoundSummary `json:"items"` // Items 本激活内按业务轮倒序的摘要,空页为[]。
|
||
NextCursor string `json:"next_cursor,omitempty"` // NextCursor 绑定实例、激活及查询类型的后续位置。
|
||
}
|
||
|
||
func historyPage(in service.HistoryPageInput) (service.HistoryPageInput, error) {
|
||
if in.Limit == 0 {
|
||
in.Limit = 20
|
||
}
|
||
if in.Limit < 1 || in.Limit > 100 || len(in.Cursor) > 1024 {
|
||
return in, fault.Invalid("invalid workflow history page")
|
||
}
|
||
return in, nil
|
||
}
|
||
func (s *Service) ListActivations(ctx context.Context, t auth.Token, id core.ID, key string, in service.HistoryPageInput) (ActivationPage, error) {
|
||
if id.Validate() != nil || !versionName(key) {
|
||
return ActivationPage{}, fault.Invalid("invalid workflow step identity")
|
||
}
|
||
in, err := historyPage(in)
|
||
if err != nil {
|
||
return ActivationPage{}, err
|
||
}
|
||
return s.store.ListActivations(ctx, t, id, key, in)
|
||
}
|
||
func (s *Service) GetActivation(ctx context.Context, t auth.Token, id core.ID, key string, activation core.ID) (ActivationDetail, error) {
|
||
if id.Validate() != nil || !versionName(key) || activation.Validate() != nil {
|
||
return ActivationDetail{}, fault.Invalid("invalid workflow activation identity")
|
||
}
|
||
return s.store.GetActivation(ctx, t, id, key, activation)
|
||
}
|
||
func (s *Service) ListPollRounds(ctx context.Context, t auth.Token, id core.ID, key string, activation core.ID, in service.HistoryPageInput) (PollRoundPage, error) {
|
||
if id.Validate() != nil || !versionName(key) || activation.Validate() != nil {
|
||
return PollRoundPage{}, fault.Invalid("invalid workflow activation identity")
|
||
}
|
||
in, err := historyPage(in)
|
||
if err != nil {
|
||
return PollRoundPage{}, err
|
||
}
|
||
return s.store.ListPollRounds(ctx, t, id, key, activation, in)
|
||
}
|