From 7a65cf7186202fe698899edd55e6df74f359e59c Mon Sep 17 00:00:00 2001 From: Tine Date: Mon, 19 Feb 2024 11:43:43 +0100 Subject: [PATCH] feat(healthcheck): abbility to select worker group --- cmd/zdravko/main.go | 2 +- internal/handlers/settingshealthchecks.go | 8 +++-- internal/models/models.go | 4 ++- .../models/query/healthcheck_https.gen.go | 6 +++- internal/models/query/healthcheck_tcps.gen.go | 6 +++- internal/services/healthcheck.go | 35 +++++++++++-------- .../pages/settings_healthchecks.tmpl | 8 +++++ .../pages/settings_healthchecks_create.tmpl | 4 +++ .../pages/settings_healthchecks_describe.tmpl | 1 + 9 files changed, 52 insertions(+), 22 deletions(-) diff --git a/cmd/zdravko/main.go b/cmd/zdravko/main.go index fa920b1..793cc30 100644 --- a/cmd/zdravko/main.go +++ b/cmd/zdravko/main.go @@ -85,7 +85,7 @@ func main() { defer wg.Done() err := srv.Start() if err != nil { - log.Fatalf("Unable to start server %s: %v", srv.Name(), err) + log.Fatalf("Unable to start %s: %v", srv.Name(), err) } }() } diff --git a/internal/handlers/settingshealthchecks.go b/internal/handlers/settingshealthchecks.go index 735d41e..e37a65d 100644 --- a/internal/handlers/settingshealthchecks.go +++ b/internal/handlers/settingshealthchecks.go @@ -4,6 +4,7 @@ import ( "context" "fmt" "net/http" + "strings" "text/template" "code.tjo.space/mentos1386/zdravko/internal/models" @@ -123,9 +124,10 @@ func (h *BaseHandler) SettingsHealthchecksCreatePOST(w http.ResponseWriter, r *h healthcheckHttp := &models.HealthcheckHttp{ Healthcheck: models.Healthcheck{ - Name: r.FormValue("name"), - Slug: slug.Make(r.FormValue("name")), - Schedule: r.FormValue("schedule"), + Name: r.FormValue("name"), + Slug: slug.Make(r.FormValue("name")), + Schedule: r.FormValue("schedule"), + WorkerGroups: strings.Split(r.FormValue("workergroups"), ","), }, Url: r.FormValue("url"), Method: r.FormValue("method"), diff --git a/internal/models/models.go b/internal/models/models.go index e68b967..7d50569 100644 --- a/internal/models/models.go +++ b/internal/models/models.go @@ -3,6 +3,7 @@ package models import ( "time" + "github.com/lib/pq" "gorm.io/gorm" ) @@ -25,7 +26,8 @@ type Healthcheck struct { Name string `gorm:"unique" validate:"required"` Status string // UP, DOWN UptimePercentage float64 - Schedule string `validate:"required,cron"` + Schedule string `validate:"required,cron"` + WorkerGroups pq.StringArray `gorm:"type:text[]"` } type HealthcheckHttp struct { diff --git a/internal/models/query/healthcheck_https.gen.go b/internal/models/query/healthcheck_https.gen.go index 227393f..f53f30d 100644 --- a/internal/models/query/healthcheck_https.gen.go +++ b/internal/models/query/healthcheck_https.gen.go @@ -36,6 +36,7 @@ func newHealthcheckHttp(db *gorm.DB, opts ...gen.DOOption) healthcheckHttp { _healthcheckHttp.Status = field.NewString(tableName, "status") _healthcheckHttp.UptimePercentage = field.NewFloat64(tableName, "uptime_percentage") _healthcheckHttp.Schedule = field.NewString(tableName, "schedule") + _healthcheckHttp.Groups = field.NewField(tableName, "groups") _healthcheckHttp.Url = field.NewString(tableName, "url") _healthcheckHttp.Method = field.NewString(tableName, "method") @@ -57,6 +58,7 @@ type healthcheckHttp struct { Status field.String UptimePercentage field.Float64 Schedule field.String + Groups field.Field Url field.String Method field.String @@ -84,6 +86,7 @@ func (h *healthcheckHttp) updateTableName(table string) *healthcheckHttp { h.Status = field.NewString(table, "status") h.UptimePercentage = field.NewFloat64(table, "uptime_percentage") h.Schedule = field.NewString(table, "schedule") + h.Groups = field.NewField(table, "groups") h.Url = field.NewString(table, "url") h.Method = field.NewString(table, "method") @@ -114,7 +117,7 @@ func (h *healthcheckHttp) GetFieldByName(fieldName string) (field.OrderExpr, boo } func (h *healthcheckHttp) fillFieldMap() { - h.fieldMap = make(map[string]field.Expr, 11) + h.fieldMap = make(map[string]field.Expr, 12) h.fieldMap["id"] = h.ID h.fieldMap["created_at"] = h.CreatedAt h.fieldMap["updated_at"] = h.UpdatedAt @@ -124,6 +127,7 @@ func (h *healthcheckHttp) fillFieldMap() { h.fieldMap["status"] = h.Status h.fieldMap["uptime_percentage"] = h.UptimePercentage h.fieldMap["schedule"] = h.Schedule + h.fieldMap["groups"] = h.Groups h.fieldMap["url"] = h.Url h.fieldMap["method"] = h.Method } diff --git a/internal/models/query/healthcheck_tcps.gen.go b/internal/models/query/healthcheck_tcps.gen.go index eb7b169..86993ac 100644 --- a/internal/models/query/healthcheck_tcps.gen.go +++ b/internal/models/query/healthcheck_tcps.gen.go @@ -36,6 +36,7 @@ func newHealthcheckTcp(db *gorm.DB, opts ...gen.DOOption) healthcheckTcp { _healthcheckTcp.Status = field.NewString(tableName, "status") _healthcheckTcp.UptimePercentage = field.NewFloat64(tableName, "uptime_percentage") _healthcheckTcp.Schedule = field.NewString(tableName, "schedule") + _healthcheckTcp.Groups = field.NewField(tableName, "groups") _healthcheckTcp.Hostname = field.NewString(tableName, "hostname") _healthcheckTcp.Port = field.NewInt(tableName, "port") @@ -57,6 +58,7 @@ type healthcheckTcp struct { Status field.String UptimePercentage field.Float64 Schedule field.String + Groups field.Field Hostname field.String Port field.Int @@ -84,6 +86,7 @@ func (h *healthcheckTcp) updateTableName(table string) *healthcheckTcp { h.Status = field.NewString(table, "status") h.UptimePercentage = field.NewFloat64(table, "uptime_percentage") h.Schedule = field.NewString(table, "schedule") + h.Groups = field.NewField(table, "groups") h.Hostname = field.NewString(table, "hostname") h.Port = field.NewInt(table, "port") @@ -114,7 +117,7 @@ func (h *healthcheckTcp) GetFieldByName(fieldName string) (field.OrderExpr, bool } func (h *healthcheckTcp) fillFieldMap() { - h.fieldMap = make(map[string]field.Expr, 11) + h.fieldMap = make(map[string]field.Expr, 12) h.fieldMap["id"] = h.ID h.fieldMap["created_at"] = h.CreatedAt h.fieldMap["updated_at"] = h.UpdatedAt @@ -124,6 +127,7 @@ func (h *healthcheckTcp) fillFieldMap() { h.fieldMap["status"] = h.Status h.fieldMap["uptime_percentage"] = h.UptimePercentage h.fieldMap["schedule"] = h.Schedule + h.fieldMap["groups"] = h.Groups h.fieldMap["hostname"] = h.Hostname h.fieldMap["port"] = h.Port } diff --git a/internal/services/healthcheck.go b/internal/services/healthcheck.go index 3bae49d..177e920 100644 --- a/internal/services/healthcheck.go +++ b/internal/services/healthcheck.go @@ -29,21 +29,26 @@ func StartHealthcheckHttp(ctx context.Context, t client.Client, healthcheckHttp args := make([]interface{}, 0) args = append(args, workflows.HealthcheckHttpWorkflowParam{Url: healthcheckHttp.Url, Method: healthcheckHttp.Method}) - _, err := t.ScheduleClient().Create(ctx, client.ScheduleOptions{ - ID: "healthcheck-http-" + healthcheckHttp.Slug, - Spec: client.ScheduleSpec{ - CronExpressions: []string{healthcheckHttp.Schedule}, - }, - Action: &client.ScheduleWorkflowAction{ - ID: "healthcheck-http-id-workflow", - Workflow: workflows.HealthcheckHttpWorkflowDefinition, - Args: args, - TaskQueue: "default", - RetryPolicy: &temporal.RetryPolicy{ - MaximumAttempts: 3, + for _, group := range healthcheckHttp.WorkerGroups { + _, err := t.ScheduleClient().Create(ctx, client.ScheduleOptions{ + ID: "healthcheck-http-" + healthcheckHttp.Slug, + Spec: client.ScheduleSpec{ + CronExpressions: []string{healthcheckHttp.Schedule}, }, - }, - }) + Action: &client.ScheduleWorkflowAction{ + ID: "healthcheck-http-" + healthcheckHttp.Slug, + Workflow: workflows.HealthcheckHttpWorkflowDefinition, + Args: args, + TaskQueue: group, + RetryPolicy: &temporal.RetryPolicy{ + MaximumAttempts: 3, + }, + }, + }) + if err != nil { + return err + } + } - return err + return nil } diff --git a/web/templates/pages/settings_healthchecks.tmpl b/web/templates/pages/settings_healthchecks.tmpl index 60668be..4ac3a79 100644 --- a/web/templates/pages/settings_healthchecks.tmpl +++ b/web/templates/pages/settings_healthchecks.tmpl @@ -39,6 +39,9 @@ Name + + Worker Groups + Type @@ -60,6 +63,11 @@ {{.Name}} + {{range .WorkerGroups}} + {{ . }} + {{end}} + + HTTP diff --git a/web/templates/pages/settings_healthchecks_create.tmpl b/web/templates/pages/settings_healthchecks_create.tmpl index 2eb4ebe..d88d4bc 100644 --- a/web/templates/pages/settings_healthchecks_create.tmpl +++ b/web/templates/pages/settings_healthchecks_create.tmpl @@ -8,6 +8,10 @@ +
+ + +
diff --git a/web/templates/pages/settings_healthchecks_describe.tmpl b/web/templates/pages/settings_healthchecks_describe.tmpl index 68a7fdd..b5e762d 100644 --- a/web/templates/pages/settings_healthchecks_describe.tmpl +++ b/web/templates/pages/settings_healthchecks_describe.tmpl @@ -7,5 +7,6 @@ {{ .Healthcheck.Slug }} {{ .Healthcheck.Url }} {{ .Healthcheck.Schedule }} + {{ .Healthcheck.WorkerGroups }} {{end}}