2024-02-16 12:07:29 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/models"
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/models/query"
|
|
|
|
"code.tjo.space/mentos1386/zdravko/internal/workflows"
|
|
|
|
"go.temporal.io/sdk/client"
|
2024-02-16 12:41:18 +00:00
|
|
|
"go.temporal.io/sdk/temporal"
|
2024-02-16 12:07:29 +00:00
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
func CreateHealthcheck(ctx context.Context, db *gorm.DB, healthcheck *models.Healthcheck) error {
|
2024-02-16 12:07:29 +00:00
|
|
|
return db.WithContext(ctx).Create(healthcheck).Error
|
|
|
|
}
|
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
func GetHealthcheck(ctx context.Context, q *query.Query, slug string) (*models.Healthcheck, error) {
|
|
|
|
log.Println("GetHealthcheck")
|
|
|
|
return q.Healthcheck.WithContext(ctx).Where(
|
|
|
|
q.Healthcheck.Slug.Eq(slug),
|
2024-02-16 12:07:29 +00:00
|
|
|
).First()
|
|
|
|
}
|
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
func StartHealthcheck(ctx context.Context, t client.Client, healthcheck *models.Healthcheck) error {
|
|
|
|
log.Println("Starting Healthcheck Workflow")
|
2024-02-16 12:07:29 +00:00
|
|
|
|
|
|
|
args := make([]interface{}, 0)
|
2024-02-21 09:06:54 +00:00
|
|
|
args = append(args, workflows.HealthcheckWorkflowParam{Script: healthcheck.Script})
|
2024-02-16 12:07:29 +00:00
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
id := "healthcheck-" + healthcheck.Slug
|
2024-02-20 10:24:04 +00:00
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
for _, group := range healthcheck.WorkerGroups {
|
2024-02-19 10:43:43 +00:00
|
|
|
_, err := t.ScheduleClient().Create(ctx, client.ScheduleOptions{
|
2024-02-20 10:24:04 +00:00
|
|
|
ID: id + "-" + group,
|
2024-02-19 10:43:43 +00:00
|
|
|
Spec: client.ScheduleSpec{
|
2024-02-21 09:06:54 +00:00
|
|
|
CronExpressions: []string{healthcheck.Schedule},
|
2024-02-16 12:41:18 +00:00
|
|
|
},
|
2024-02-19 10:43:43 +00:00
|
|
|
Action: &client.ScheduleWorkflowAction{
|
2024-02-20 10:24:04 +00:00
|
|
|
ID: id + "-" + group,
|
2024-02-21 09:06:54 +00:00
|
|
|
Workflow: workflows.HealthcheckWorkflowDefinition,
|
2024-02-19 10:43:43 +00:00
|
|
|
Args: args,
|
|
|
|
TaskQueue: group,
|
|
|
|
RetryPolicy: &temporal.RetryPolicy{
|
|
|
|
MaximumAttempts: 3,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-02-16 12:07:29 +00:00
|
|
|
|
2024-02-19 10:43:43 +00:00
|
|
|
return nil
|
2024-02-16 12:07:29 +00:00
|
|
|
}
|
2024-02-20 10:24:04 +00:00
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
func CreateHealthcheckHistory(ctx context.Context, db *gorm.DB, healthcheckHistory *models.HealthcheckHistory) error {
|
2024-02-20 10:24:04 +00:00
|
|
|
return db.WithContext(ctx).Create(healthcheckHistory).Error
|
|
|
|
}
|