zdravko/internal/server/workflows/check.go

78 lines
2.1 KiB
Go
Raw Normal View History

package workflows
import (
"sort"
"time"
"github.com/mentos1386/zdravko/internal/temporal"
"go.temporal.io/sdk/workflow"
)
2024-05-25 17:52:37 +00:00
func (w *Workflows) CheckWorkflowDefinition(ctx workflow.Context, param temporal.WorkflowCheckParam) (*temporal.WorkflowCheckResult, error) {
workerGroupIds := param.WorkerGroupIds
sort.Strings(workerGroupIds)
targetsFilterResult := temporal.ActivityTargetsFilterResult{}
2024-05-25 11:45:25 +00:00
err := workflow.ExecuteActivity(
workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 60 * time.Second,
TaskQueue: temporal.TEMPORAL_SERVER_QUEUE,
}),
temporal.ActivityTargetsFilterName,
temporal.ActivityTargetsFilterParam{
Filter: param.Filter,
},
).Get(ctx, &targetsFilterResult)
if err != nil {
2024-05-25 17:52:37 +00:00
return nil, err
}
for _, target := range targetsFilterResult.Targets {
for _, workerGroupId := range workerGroupIds {
var checkResult *temporal.ActivityCheckResult
2024-05-25 11:45:25 +00:00
err := workflow.ExecuteActivity(
workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 60 * time.Second,
TaskQueue: workerGroupId,
}),
temporal.ActivityCheckName,
temporal.ActivityCheckParam{
Script: param.Script,
Target: target,
},
).Get(ctx, &checkResult)
if err != nil {
2024-05-25 17:52:37 +00:00
return nil, err
}
2024-05-25 11:45:25 +00:00
status := temporal.AddTargetHistoryStatusFailure
if checkResult.Success {
2024-05-25 11:45:25 +00:00
status = temporal.AddTargetHistoryStatusSuccess
}
var addTargetHistoryResult *temporal.ActivityAddTargetHistoryResult
err = workflow.ExecuteActivity(
workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
StartToCloseTimeout: 60 * time.Second,
TaskQueue: temporal.TEMPORAL_SERVER_QUEUE,
}),
temporal.ActivityAddTargetHistoryName,
&temporal.ActivityAddTargetHistoryParam{
2024-05-25 17:52:37 +00:00
Target: target,
WorkerGroupId: workerGroupId,
CheckId: param.CheckId,
Status: status,
Note: checkResult.Note,
2024-05-25 11:45:25 +00:00
},
).Get(ctx, &addTargetHistoryResult)
if err != nil {
2024-05-25 17:52:37 +00:00
return nil, err
}
}
}
2024-05-25 17:52:37 +00:00
return &temporal.WorkflowCheckResult{
Note: "Check workflow completed",
}, nil
}