2024-02-16 12:07:29 +00:00
|
|
|
package services
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"log"
|
2024-02-22 16:29:17 +00:00
|
|
|
"time"
|
2024-02-16 12:07:29 +00:00
|
|
|
|
|
|
|
"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-23 21:08:17 +00:00
|
|
|
func getScheduleId(healthcheck *models.Healthcheck, group string) string {
|
|
|
|
return "healthcheck-" + healthcheck.Slug + "-" + group
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateHealthcheck(ctx context.Context, query *query.Query, healthcheck *models.Healthcheck) error {
|
|
|
|
return query.Healthcheck.WithContext(ctx).Create(healthcheck)
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateHealthcheck(ctx context.Context, q *query.Query, healthcheck *models.Healthcheck) error {
|
|
|
|
_, err := q.Healthcheck.WithContext(ctx).Where(
|
|
|
|
q.Healthcheck.Slug.Eq(healthcheck.Slug),
|
|
|
|
).Updates(healthcheck)
|
|
|
|
return err
|
2024-02-16 12:07:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
func GetHealthcheck(ctx context.Context, q *query.Query, slug string) (*models.Healthcheck, error) {
|
|
|
|
return q.Healthcheck.WithContext(ctx).Where(
|
|
|
|
q.Healthcheck.Slug.Eq(slug),
|
2024-02-22 16:29:17 +00:00
|
|
|
q.Healthcheck.DeletedAt.IsNull(),
|
2024-02-22 19:20:34 +00:00
|
|
|
).Preload(
|
|
|
|
q.Healthcheck.History,
|
2024-02-16 12:07:29 +00:00
|
|
|
).First()
|
|
|
|
}
|
|
|
|
|
2024-02-22 19:20:34 +00:00
|
|
|
func GetHealthchecks(ctx context.Context, q *query.Query) ([]*models.Healthcheck, error) {
|
2024-02-22 16:29:17 +00:00
|
|
|
return q.Healthcheck.WithContext(ctx).Preload(
|
|
|
|
q.Healthcheck.History,
|
|
|
|
).Where(
|
|
|
|
q.Healthcheck.DeletedAt.IsNull(),
|
|
|
|
).Find()
|
|
|
|
}
|
|
|
|
|
2024-02-23 21:08:17 +00:00
|
|
|
func CreateOrUpdateHealthcheckSchedule(ctx context.Context, t client.Client, healthcheck *models.Healthcheck) error {
|
|
|
|
log.Println("Creating or Updating Healthcheck Schedule")
|
2024-02-16 12:07:29 +00:00
|
|
|
|
|
|
|
args := make([]interface{}, 0)
|
2024-02-21 22:15:21 +00:00
|
|
|
args = append(args, workflows.HealthcheckWorkflowParam{Script: healthcheck.Script, Slug: healthcheck.Slug})
|
2024-02-16 12:07:29 +00:00
|
|
|
|
2024-02-21 09:06:54 +00:00
|
|
|
for _, group := range healthcheck.WorkerGroups {
|
2024-02-23 21:08:17 +00:00
|
|
|
options := client.ScheduleOptions{
|
|
|
|
ID: getScheduleId(healthcheck, group),
|
|
|
|
//SearchAttributes: map[string]interface{}{
|
|
|
|
// "worker-group": group,
|
|
|
|
// "healthcheck-slug": healthcheck.Slug,
|
|
|
|
//},
|
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-22 16:29:17 +00:00
|
|
|
Jitter: time.Second * 10,
|
2024-02-16 12:41:18 +00:00
|
|
|
},
|
2024-02-19 10:43:43 +00:00
|
|
|
Action: &client.ScheduleWorkflowAction{
|
2024-02-23 21:08:17 +00:00
|
|
|
ID: getScheduleId(healthcheck, group),
|
|
|
|
Workflow: workflows.NewWorkflows(nil).HealthcheckWorkflowDefinition,
|
2024-02-19 10:43:43 +00:00
|
|
|
Args: args,
|
|
|
|
TaskQueue: group,
|
|
|
|
RetryPolicy: &temporal.RetryPolicy{
|
|
|
|
MaximumAttempts: 3,
|
|
|
|
},
|
|
|
|
},
|
2024-02-23 21:08:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
schedule := t.ScheduleClient().GetHandle(ctx, getScheduleId(healthcheck, group))
|
|
|
|
|
|
|
|
// If exists, we update
|
|
|
|
_, err := schedule.Describe(ctx)
|
|
|
|
if err == nil {
|
|
|
|
err = schedule.Update(ctx, client.ScheduleUpdateOptions{
|
|
|
|
DoUpdate: func(input client.ScheduleUpdateInput) (*client.ScheduleUpdate, error) {
|
|
|
|
return &client.ScheduleUpdate{
|
|
|
|
Schedule: &client.Schedule{
|
|
|
|
Spec: &options.Spec,
|
|
|
|
Action: options.Action,
|
|
|
|
Policy: input.Description.Schedule.Policy,
|
|
|
|
State: input.Description.Schedule.State,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
schedule, err = t.ScheduleClient().Create(ctx, options)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = schedule.Trigger(ctx, client.ScheduleTriggerOptions{})
|
2024-02-19 10:43:43 +00:00
|
|
|
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
|
|
|
|
}
|