2024-05-16 20:15:14 +00:00
|
|
|
package workflows
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"code.tjo.space/mentos1386/zdravko/database/models"
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/activities"
|
|
|
|
"go.temporal.io/sdk/workflow"
|
|
|
|
)
|
|
|
|
|
|
|
|
type CheckWorkflowParam struct {
|
|
|
|
Script string
|
2024-05-18 20:18:28 +00:00
|
|
|
CheckId string
|
2024-05-16 20:15:14 +00:00
|
|
|
WorkerGroupIds []string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (w *Workflows) CheckWorkflowDefinition(ctx workflow.Context, param CheckWorkflowParam) (models.CheckStatus, error) {
|
|
|
|
workerGroupIds := param.WorkerGroupIds
|
|
|
|
sort.Strings(workerGroupIds)
|
|
|
|
|
|
|
|
for _, workerGroupId := range workerGroupIds {
|
|
|
|
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
|
|
|
|
StartToCloseTimeout: 60 * time.Second,
|
|
|
|
TaskQueue: workerGroupId,
|
|
|
|
})
|
|
|
|
|
|
|
|
heatlcheckParam := activities.HealtcheckParam{
|
|
|
|
Script: param.Script,
|
|
|
|
}
|
|
|
|
|
|
|
|
var checkResult *activities.CheckResult
|
|
|
|
err := workflow.ExecuteActivity(ctx, w.activities.Check, heatlcheckParam).Get(ctx, &checkResult)
|
|
|
|
if err != nil {
|
2024-05-18 20:18:28 +00:00
|
|
|
return models.CheckStatusUnknown, err
|
2024-05-16 20:15:14 +00:00
|
|
|
}
|
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
status := models.CheckStatusFailure
|
2024-05-16 20:15:14 +00:00
|
|
|
if checkResult.Success {
|
2024-05-18 20:18:28 +00:00
|
|
|
status = models.CheckStatusSuccess
|
2024-05-16 20:15:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
historyParam := activities.HealtcheckAddToHistoryParam{
|
2024-05-18 20:18:28 +00:00
|
|
|
CheckId: param.CheckId,
|
2024-05-16 20:15:14 +00:00
|
|
|
Status: status,
|
|
|
|
Note: checkResult.Note,
|
|
|
|
WorkerGroupId: workerGroupId,
|
|
|
|
}
|
|
|
|
|
|
|
|
var historyResult *activities.CheckAddToHistoryResult
|
|
|
|
err = workflow.ExecuteActivity(ctx, w.activities.CheckAddToHistory, historyParam).Get(ctx, &historyResult)
|
|
|
|
if err != nil {
|
2024-05-18 20:18:28 +00:00
|
|
|
return models.CheckStatusUnknown, err
|
2024-05-16 20:15:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-18 20:18:28 +00:00
|
|
|
return models.CheckStatusSuccess, nil
|
2024-05-16 20:15:14 +00:00
|
|
|
}
|