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"
|
|
|
|
)
|
|
|
|
|
|
|
|
func CreateHealthcheckHttp(ctx context.Context, db *gorm.DB, healthcheck *models.HealthcheckHttp) error {
|
|
|
|
return db.WithContext(ctx).Create(healthcheck).Error
|
|
|
|
}
|
|
|
|
|
2024-02-16 12:41:18 +00:00
|
|
|
func GetHealthcheckHttp(ctx context.Context, q *query.Query, slug string) (*models.HealthcheckHttp, error) {
|
2024-02-16 12:07:29 +00:00
|
|
|
log.Println("GetHealthcheckHttp")
|
|
|
|
return q.HealthcheckHttp.WithContext(ctx).Where(
|
2024-02-16 12:41:18 +00:00
|
|
|
q.HealthcheckHttp.Slug.Eq(slug),
|
2024-02-16 12:07:29 +00:00
|
|
|
).First()
|
|
|
|
}
|
|
|
|
|
2024-02-16 12:41:18 +00:00
|
|
|
func StartHealthcheckHttp(ctx context.Context, t client.Client, healthcheckHttp *models.HealthcheckHttp) error {
|
2024-02-16 12:07:29 +00:00
|
|
|
log.Println("Starting HealthcheckHttp Workflow")
|
|
|
|
|
|
|
|
args := make([]interface{}, 0)
|
2024-02-16 12:41:18 +00:00
|
|
|
args = append(args, workflows.HealthcheckHttpWorkflowParam{Url: healthcheckHttp.Url, Method: healthcheckHttp.Method})
|
2024-02-16 12:07:29 +00:00
|
|
|
|
2024-02-20 10:24:04 +00:00
|
|
|
id := "healthcheck-http-" + healthcheckHttp.Slug
|
|
|
|
|
2024-02-19 10:43:43 +00:00
|
|
|
for _, group := range healthcheckHttp.WorkerGroups {
|
|
|
|
_, 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{
|
|
|
|
CronExpressions: []string{healthcheckHttp.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-19 10:43:43 +00:00
|
|
|
Workflow: workflows.HealthcheckHttpWorkflowDefinition,
|
|
|
|
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
|
|
|
|
|
|
|
func CreateHealthcheckHistory(ctx context.Context, db *gorm.DB, healthcheckHistory *models.HealthcheckHttpHistory) error {
|
|
|
|
return db.WithContext(ctx).Create(healthcheckHistory).Error
|
|
|
|
}
|