feat(healthcheck): abbility to select worker group

This commit is contained in:
Tine 2024-02-19 11:43:43 +01:00
parent 07f41f879d
commit 7a65cf7186
Signed by: mentos1386
SSH key fingerprint: SHA256:MNtTsLbihYaWF8j1fkOHfkKNlnN1JQfxEU/rBU8nCGw
9 changed files with 52 additions and 22 deletions

View file

@ -85,7 +85,7 @@ func main() {
defer wg.Done() defer wg.Done()
err := srv.Start() err := srv.Start()
if err != nil { if err != nil {
log.Fatalf("Unable to start server %s: %v", srv.Name(), err) log.Fatalf("Unable to start %s: %v", srv.Name(), err)
} }
}() }()
} }

View file

@ -4,6 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"net/http" "net/http"
"strings"
"text/template" "text/template"
"code.tjo.space/mentos1386/zdravko/internal/models" "code.tjo.space/mentos1386/zdravko/internal/models"
@ -126,6 +127,7 @@ func (h *BaseHandler) SettingsHealthchecksCreatePOST(w http.ResponseWriter, r *h
Name: r.FormValue("name"), Name: r.FormValue("name"),
Slug: slug.Make(r.FormValue("name")), Slug: slug.Make(r.FormValue("name")),
Schedule: r.FormValue("schedule"), Schedule: r.FormValue("schedule"),
WorkerGroups: strings.Split(r.FormValue("workergroups"), ","),
}, },
Url: r.FormValue("url"), Url: r.FormValue("url"),
Method: r.FormValue("method"), Method: r.FormValue("method"),

View file

@ -3,6 +3,7 @@ package models
import ( import (
"time" "time"
"github.com/lib/pq"
"gorm.io/gorm" "gorm.io/gorm"
) )
@ -26,6 +27,7 @@ type Healthcheck struct {
Status string // UP, DOWN Status string // UP, DOWN
UptimePercentage float64 UptimePercentage float64
Schedule string `validate:"required,cron"` Schedule string `validate:"required,cron"`
WorkerGroups pq.StringArray `gorm:"type:text[]"`
} }
type HealthcheckHttp struct { type HealthcheckHttp struct {

View file

@ -36,6 +36,7 @@ func newHealthcheckHttp(db *gorm.DB, opts ...gen.DOOption) healthcheckHttp {
_healthcheckHttp.Status = field.NewString(tableName, "status") _healthcheckHttp.Status = field.NewString(tableName, "status")
_healthcheckHttp.UptimePercentage = field.NewFloat64(tableName, "uptime_percentage") _healthcheckHttp.UptimePercentage = field.NewFloat64(tableName, "uptime_percentage")
_healthcheckHttp.Schedule = field.NewString(tableName, "schedule") _healthcheckHttp.Schedule = field.NewString(tableName, "schedule")
_healthcheckHttp.Groups = field.NewField(tableName, "groups")
_healthcheckHttp.Url = field.NewString(tableName, "url") _healthcheckHttp.Url = field.NewString(tableName, "url")
_healthcheckHttp.Method = field.NewString(tableName, "method") _healthcheckHttp.Method = field.NewString(tableName, "method")
@ -57,6 +58,7 @@ type healthcheckHttp struct {
Status field.String Status field.String
UptimePercentage field.Float64 UptimePercentage field.Float64
Schedule field.String Schedule field.String
Groups field.Field
Url field.String Url field.String
Method field.String Method field.String
@ -84,6 +86,7 @@ func (h *healthcheckHttp) updateTableName(table string) *healthcheckHttp {
h.Status = field.NewString(table, "status") h.Status = field.NewString(table, "status")
h.UptimePercentage = field.NewFloat64(table, "uptime_percentage") h.UptimePercentage = field.NewFloat64(table, "uptime_percentage")
h.Schedule = field.NewString(table, "schedule") h.Schedule = field.NewString(table, "schedule")
h.Groups = field.NewField(table, "groups")
h.Url = field.NewString(table, "url") h.Url = field.NewString(table, "url")
h.Method = field.NewString(table, "method") h.Method = field.NewString(table, "method")
@ -114,7 +117,7 @@ func (h *healthcheckHttp) GetFieldByName(fieldName string) (field.OrderExpr, boo
} }
func (h *healthcheckHttp) fillFieldMap() { 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["id"] = h.ID
h.fieldMap["created_at"] = h.CreatedAt h.fieldMap["created_at"] = h.CreatedAt
h.fieldMap["updated_at"] = h.UpdatedAt h.fieldMap["updated_at"] = h.UpdatedAt
@ -124,6 +127,7 @@ func (h *healthcheckHttp) fillFieldMap() {
h.fieldMap["status"] = h.Status h.fieldMap["status"] = h.Status
h.fieldMap["uptime_percentage"] = h.UptimePercentage h.fieldMap["uptime_percentage"] = h.UptimePercentage
h.fieldMap["schedule"] = h.Schedule h.fieldMap["schedule"] = h.Schedule
h.fieldMap["groups"] = h.Groups
h.fieldMap["url"] = h.Url h.fieldMap["url"] = h.Url
h.fieldMap["method"] = h.Method h.fieldMap["method"] = h.Method
} }

View file

@ -36,6 +36,7 @@ func newHealthcheckTcp(db *gorm.DB, opts ...gen.DOOption) healthcheckTcp {
_healthcheckTcp.Status = field.NewString(tableName, "status") _healthcheckTcp.Status = field.NewString(tableName, "status")
_healthcheckTcp.UptimePercentage = field.NewFloat64(tableName, "uptime_percentage") _healthcheckTcp.UptimePercentage = field.NewFloat64(tableName, "uptime_percentage")
_healthcheckTcp.Schedule = field.NewString(tableName, "schedule") _healthcheckTcp.Schedule = field.NewString(tableName, "schedule")
_healthcheckTcp.Groups = field.NewField(tableName, "groups")
_healthcheckTcp.Hostname = field.NewString(tableName, "hostname") _healthcheckTcp.Hostname = field.NewString(tableName, "hostname")
_healthcheckTcp.Port = field.NewInt(tableName, "port") _healthcheckTcp.Port = field.NewInt(tableName, "port")
@ -57,6 +58,7 @@ type healthcheckTcp struct {
Status field.String Status field.String
UptimePercentage field.Float64 UptimePercentage field.Float64
Schedule field.String Schedule field.String
Groups field.Field
Hostname field.String Hostname field.String
Port field.Int Port field.Int
@ -84,6 +86,7 @@ func (h *healthcheckTcp) updateTableName(table string) *healthcheckTcp {
h.Status = field.NewString(table, "status") h.Status = field.NewString(table, "status")
h.UptimePercentage = field.NewFloat64(table, "uptime_percentage") h.UptimePercentage = field.NewFloat64(table, "uptime_percentage")
h.Schedule = field.NewString(table, "schedule") h.Schedule = field.NewString(table, "schedule")
h.Groups = field.NewField(table, "groups")
h.Hostname = field.NewString(table, "hostname") h.Hostname = field.NewString(table, "hostname")
h.Port = field.NewInt(table, "port") h.Port = field.NewInt(table, "port")
@ -114,7 +117,7 @@ func (h *healthcheckTcp) GetFieldByName(fieldName string) (field.OrderExpr, bool
} }
func (h *healthcheckTcp) fillFieldMap() { 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["id"] = h.ID
h.fieldMap["created_at"] = h.CreatedAt h.fieldMap["created_at"] = h.CreatedAt
h.fieldMap["updated_at"] = h.UpdatedAt h.fieldMap["updated_at"] = h.UpdatedAt
@ -124,6 +127,7 @@ func (h *healthcheckTcp) fillFieldMap() {
h.fieldMap["status"] = h.Status h.fieldMap["status"] = h.Status
h.fieldMap["uptime_percentage"] = h.UptimePercentage h.fieldMap["uptime_percentage"] = h.UptimePercentage
h.fieldMap["schedule"] = h.Schedule h.fieldMap["schedule"] = h.Schedule
h.fieldMap["groups"] = h.Groups
h.fieldMap["hostname"] = h.Hostname h.fieldMap["hostname"] = h.Hostname
h.fieldMap["port"] = h.Port h.fieldMap["port"] = h.Port
} }

View file

@ -29,21 +29,26 @@ func StartHealthcheckHttp(ctx context.Context, t client.Client, healthcheckHttp
args := make([]interface{}, 0) args := make([]interface{}, 0)
args = append(args, workflows.HealthcheckHttpWorkflowParam{Url: healthcheckHttp.Url, Method: healthcheckHttp.Method}) args = append(args, workflows.HealthcheckHttpWorkflowParam{Url: healthcheckHttp.Url, Method: healthcheckHttp.Method})
for _, group := range healthcheckHttp.WorkerGroups {
_, err := t.ScheduleClient().Create(ctx, client.ScheduleOptions{ _, err := t.ScheduleClient().Create(ctx, client.ScheduleOptions{
ID: "healthcheck-http-" + healthcheckHttp.Slug, ID: "healthcheck-http-" + healthcheckHttp.Slug,
Spec: client.ScheduleSpec{ Spec: client.ScheduleSpec{
CronExpressions: []string{healthcheckHttp.Schedule}, CronExpressions: []string{healthcheckHttp.Schedule},
}, },
Action: &client.ScheduleWorkflowAction{ Action: &client.ScheduleWorkflowAction{
ID: "healthcheck-http-id-workflow", ID: "healthcheck-http-" + healthcheckHttp.Slug,
Workflow: workflows.HealthcheckHttpWorkflowDefinition, Workflow: workflows.HealthcheckHttpWorkflowDefinition,
Args: args, Args: args,
TaskQueue: "default", TaskQueue: group,
RetryPolicy: &temporal.RetryPolicy{ RetryPolicy: &temporal.RetryPolicy{
MaximumAttempts: 3, MaximumAttempts: 3,
}, },
}, },
}) })
if err != nil {
return err return err
} }
}
return nil
}

View file

@ -39,6 +39,9 @@
<th scope="col" class="px-6 py-3"> <th scope="col" class="px-6 py-3">
Name Name
</th> </th>
<th scope="col" class="px-6 py-3">
Worker Groups
</th>
<th scope="col" class="px-6 py-3"> <th scope="col" class="px-6 py-3">
Type Type
</th> </th>
@ -59,6 +62,11 @@
<th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap"> <th scope="row" class="px-6 py-4 font-medium text-gray-900 whitespace-nowrap">
{{.Name}} {{.Name}}
</th> </th>
<td class="px-6 py-4">
{{range .WorkerGroups}}
{{ . }}
{{end}}
</td>
<td class="px-6 py-4"> <td class="px-6 py-4">
HTTP HTTP
</td> </td>

View file

@ -8,6 +8,10 @@
<label for="name" class="block mb-2 text-sm font-medium text-gray-900">Name</label> <label for="name" class="block mb-2 text-sm font-medium text-gray-900">Name</label>
<input type="name" name="name" id="name" placeholder="Github.com" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5"/> <input type="name" name="name" id="name" placeholder="Github.com" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5"/>
</div> </div>
<div class="mb-5">
<label for="workergroups" class="block mb-2 text-sm font-medium text-gray-900">Worker Groups</label>
<input type="text" name="workergroups" id="workergroups" placeholder="europe,asia" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5"/>
</div>
<div class="mb-5"> <div class="mb-5">
<label for="schedule" class="block mb-2 text-sm font-medium text-gray-900">Schedule</label> <label for="schedule" class="block mb-2 text-sm font-medium text-gray-900">Schedule</label>
<input type="text" name="schedule" id="schedule" placeholder="* * * * *" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5"/> <input type="text" name="schedule" id="schedule" placeholder="* * * * *" class="bg-gray-50 border border-gray-300 text-gray-900 text-sm rounded-lg focus:ring-blue-500 focus:border-blue-500 block w-full p-2.5"/>

View file

@ -7,5 +7,6 @@
{{ .Healthcheck.Slug }} {{ .Healthcheck.Slug }}
{{ .Healthcheck.Url }} {{ .Healthcheck.Url }}
{{ .Healthcheck.Schedule }} {{ .Healthcheck.Schedule }}
{{ .Healthcheck.WorkerGroups }}
</section> </section>
{{end}} {{end}}