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()
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)
}
}()
}

View file

@ -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"),

View file

@ -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 {

View file

@ -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
}

View file

@ -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
}

View file

@ -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
}

View file

@ -39,6 +39,9 @@
<th scope="col" class="px-6 py-3">
Name
</th>
<th scope="col" class="px-6 py-3">
Worker Groups
</th>
<th scope="col" class="px-6 py-3">
Type
</th>
@ -60,6 +63,11 @@
{{.Name}}
</th>
<td class="px-6 py-4">
{{range .WorkerGroups}}
{{ . }}
{{end}}
</td>
<td class="px-6 py-4">
HTTP
</td>
<td class="px-6 py-4">

View file

@ -8,6 +8,10 @@
<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"/>
</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">
<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"/>

View file

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