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-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) {
|
|
|
|
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-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 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
|
|
|
id := "healthcheck-" + healthcheck.Slug
|
2024-02-20 10:24:04 +00:00
|
|
|
|
2024-02-21 22:15:21 +00:00
|
|
|
fakeWorkflows := workflows.NewWorkflows(nil)
|
|
|
|
|
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-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-20 10:24:04 +00:00
|
|
|
ID: id + "-" + group,
|
2024-02-21 22:15:21 +00:00
|
|
|
Workflow: fakeWorkflows.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
|
|
|
|
}
|