2024-02-24 11:57:53 +00:00
|
|
|
package workflows
|
|
|
|
|
|
|
|
import (
|
2024-02-27 22:47:20 +00:00
|
|
|
"sort"
|
2024-02-24 11:57:53 +00:00
|
|
|
"time"
|
|
|
|
|
2024-02-27 11:04:05 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/database/models"
|
2024-02-24 11:57:53 +00:00
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/activities"
|
|
|
|
"go.temporal.io/sdk/workflow"
|
|
|
|
)
|
|
|
|
|
|
|
|
type MonitorWorkflowParam struct {
|
2024-02-29 22:42:56 +00:00
|
|
|
Script string
|
|
|
|
MonitorId string
|
|
|
|
WorkerGroupIds []string
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
|
|
|
|
2024-02-29 22:42:56 +00:00
|
|
|
func (w *Workflows) MonitorWorkflowDefinition(ctx workflow.Context, param MonitorWorkflowParam) (models.MonitorStatus, error) {
|
|
|
|
workerGroupIds := param.WorkerGroupIds
|
|
|
|
sort.Strings(workerGroupIds)
|
2024-02-27 22:47:20 +00:00
|
|
|
|
2024-02-29 22:42:56 +00:00
|
|
|
for _, workerGroupId := range workerGroupIds {
|
2024-02-27 22:47:20 +00:00
|
|
|
ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{
|
|
|
|
StartToCloseTimeout: 60 * time.Second,
|
2024-02-29 22:42:56 +00:00
|
|
|
TaskQueue: workerGroupId,
|
2024-02-27 22:47:20 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
heatlcheckParam := activities.HealtcheckParam{
|
|
|
|
Script: param.Script,
|
|
|
|
}
|
|
|
|
|
|
|
|
var monitorResult *activities.MonitorResult
|
|
|
|
err := workflow.ExecuteActivity(ctx, w.activities.Monitor, heatlcheckParam).Get(ctx, &monitorResult)
|
|
|
|
if err != nil {
|
2024-02-29 22:42:56 +00:00
|
|
|
return models.MonitorUnknown, err
|
2024-02-27 22:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
status := models.MonitorFailure
|
|
|
|
if monitorResult.Success {
|
|
|
|
status = models.MonitorSuccess
|
|
|
|
}
|
|
|
|
|
|
|
|
historyParam := activities.HealtcheckAddToHistoryParam{
|
2024-02-29 22:42:56 +00:00
|
|
|
MonitorId: param.MonitorId,
|
|
|
|
Status: status,
|
|
|
|
Note: monitorResult.Note,
|
|
|
|
WorkerGroupId: workerGroupId,
|
2024-02-27 22:47:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var historyResult *activities.MonitorAddToHistoryResult
|
|
|
|
err = workflow.ExecuteActivity(ctx, w.activities.MonitorAddToHistory, historyParam).Get(ctx, &historyResult)
|
|
|
|
if err != nil {
|
2024-02-29 22:42:56 +00:00
|
|
|
return models.MonitorUnknown, err
|
2024-02-27 22:47:20 +00:00
|
|
|
}
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|
|
|
|
|
2024-02-29 22:42:56 +00:00
|
|
|
return models.MonitorSuccess, nil
|
2024-02-24 11:57:53 +00:00
|
|
|
}
|