From 65db9b3f7253172d068c0ef72942099498032010 Mon Sep 17 00:00:00 2001 From: Tine Date: Wed, 28 Feb 2024 16:40:24 +0100 Subject: [PATCH] feat(monitors,workergroups): implement deletion and stauts changes --- internal/handlers/settingsmonitors.go | 48 +++++++++++++++++++ internal/handlers/settingsworkergroups.go | 11 +++++ internal/services/monitor.go | 27 +++++++++++ internal/services/worker_group.go | 8 ++++ pkg/server/routes.go | 5 ++ pkg/server/server.go | 2 +- .../pages/settings_monitors_describe.tmpl | 13 +++-- 7 files changed, 110 insertions(+), 4 deletions(-) diff --git a/internal/handlers/settingsmonitors.go b/internal/handlers/settingsmonitors.go index 028c19a..985e5b4 100644 --- a/internal/handlers/settingsmonitors.go +++ b/internal/handlers/settingsmonitors.go @@ -123,6 +123,54 @@ func (h *BaseHandler) SettingsMonitorsDescribeGET(c echo.Context) error { }) } +func (h *BaseHandler) SettingsMonitorsDescribeDELETE(c echo.Context) error { + slug := c.Param("slug") + + err := services.DeleteMonitor(context.Background(), h.db, slug) + if err != nil { + return err + } + + err = services.DeleteMonitorSchedule(context.Background(), h.temporal, slug) + if err != nil { + return err + } + + return c.Redirect(http.StatusSeeOther, "/settings/monitors") +} + +func (h *BaseHandler) SettingsMonitorsDisableGET(c echo.Context) error { + slug := c.Param("slug") + + monitor, err := services.GetMonitor(context.Background(), h.db, slug) + if err != nil { + return err + } + + err = services.SetMonitorStatus(context.Background(), h.temporal, monitor.Slug, services.MonitorStatusPaused) + if err != nil { + return err + } + + return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/monitors/%s", slug)) +} + +func (h *BaseHandler) SettingsMonitorsEnableGET(c echo.Context) error { + slug := c.Param("slug") + + monitor, err := services.GetMonitor(context.Background(), h.db, slug) + if err != nil { + return err + } + + err = services.SetMonitorStatus(context.Background(), h.temporal, monitor.Slug, services.MonitorStatusActive) + if err != nil { + return err + } + + return c.Redirect(http.StatusSeeOther, fmt.Sprintf("/settings/monitors/%s", slug)) +} + func (h *BaseHandler) SettingsMonitorsDescribePOST(c echo.Context) error { ctx := context.Background() monitorSlug := c.Param("slug") diff --git a/internal/handlers/settingsworkergroups.go b/internal/handlers/settingsworkergroups.go index f026fe7..118cad8 100644 --- a/internal/handlers/settingsworkergroups.go +++ b/internal/handlers/settingsworkergroups.go @@ -108,6 +108,17 @@ func (h *BaseHandler) SettingsWorkerGroupsDescribeGET(c echo.Context) error { }) } +func (h *BaseHandler) SettingsWorkerGroupsDescribeDELETE(c echo.Context) error { + slug := c.Param("slug") + + err := services.DeleteWorkerGroup(context.Background(), h.db, slug) + if err != nil { + return err + } + + return c.Redirect(http.StatusSeeOther, "/settings/worker-groups") +} + func (h *BaseHandler) SettingsWorkerGroupsCreateGET(c echo.Context) error { cc := c.(AuthenticatedContext) diff --git a/internal/services/monitor.go b/internal/services/monitor.go index b17856c..8ee7fef 100644 --- a/internal/services/monitor.go +++ b/internal/services/monitor.go @@ -40,6 +40,20 @@ func GetMonitorStatus(ctx context.Context, temporal client.Client, slug string) return MonitorStatusActive, nil } +func SetMonitorStatus(ctx context.Context, temporal client.Client, slug string, status MonitorStatus) error { + schedule := temporal.ScheduleClient().GetHandle(ctx, getScheduleId(slug)) + + if status == MonitorStatusActive { + return schedule.Unpause(ctx, client.ScheduleUnpauseOptions{Note: "Unpaused by user"}) + } + + if status == MonitorStatusPaused { + return schedule.Pause(ctx, client.SchedulePauseOptions{Note: "Paused by user"}) + } + + return nil +} + func CreateMonitor(ctx context.Context, db *sqlx.DB, monitor *models.Monitor) error { _, err := db.NamedExecContext(ctx, "INSERT INTO monitors (slug, name, script, schedule) VALUES (:slug, :name, :script, :schedule)", @@ -56,6 +70,14 @@ func UpdateMonitor(ctx context.Context, db *sqlx.DB, monitor *models.Monitor) er return err } +func DeleteMonitor(ctx context.Context, db *sqlx.DB, slug string) error { + _, err := db.ExecContext(ctx, + "UPDATE monitors SET deleted_at = datetime('now') WHERE slug=$1", + slug, + ) + return err +} + func UpdateMonitorWorkerGroups(ctx context.Context, db *sqlx.DB, monitor *models.Monitor, workerGroups []*models.WorkerGroup) error { tx, err := db.BeginTxx(ctx, nil) if err != nil { @@ -204,6 +226,11 @@ ORDER BY monitors.name return maps.Values(monitors), err } +func DeleteMonitorSchedule(ctx context.Context, t client.Client, slug string) error { + schedule := t.ScheduleClient().GetHandle(ctx, getScheduleId(slug)) + return schedule.Delete(ctx) +} + func CreateOrUpdateMonitorSchedule( ctx context.Context, t client.Client, diff --git a/internal/services/worker_group.go b/internal/services/worker_group.go index 38fd1cb..fc1508a 100644 --- a/internal/services/worker_group.go +++ b/internal/services/worker_group.go @@ -32,6 +32,14 @@ func CreateWorkerGroup(ctx context.Context, db *sqlx.DB, workerGroup *models.Wor return err } +func DeleteWorkerGroup(ctx context.Context, db *sqlx.DB, slug string) error { + _, err := db.ExecContext(ctx, + "UPDATE worker_groups SET deleted_at = datetime('now') WHERE slug = $1", + slug, + ) + return err +} + func GetWorkerGroups(ctx context.Context, db *sqlx.DB) ([]*models.WorkerGroup, error) { var workerGroups []*models.WorkerGroup err := db.SelectContext(ctx, &workerGroups, diff --git a/pkg/server/routes.go b/pkg/server/routes.go index 55ba3dc..e069d99 100644 --- a/pkg/server/routes.go +++ b/pkg/server/routes.go @@ -50,10 +50,15 @@ func Routes( settings.POST("/monitors/create", h.SettingsMonitorsCreatePOST) settings.GET("/monitors/:slug", h.SettingsMonitorsDescribeGET) settings.POST("/monitors/:slug", h.SettingsMonitorsDescribePOST) + settings.GET("/monitors/:slug/delete", h.SettingsMonitorsDescribeDELETE) + settings.GET("/monitors/:slug/disable", h.SettingsMonitorsDisableGET) + settings.GET("/monitors/:slug/enable", h.SettingsMonitorsEnableGET) settings.GET("/worker-groups", h.SettingsWorkerGroupsGET) settings.GET("/worker-groups/create", h.SettingsWorkerGroupsCreateGET) settings.POST("/worker-groups/create", h.SettingsWorkerGroupsCreatePOST) settings.GET("/worker-groups/:slug", h.SettingsWorkerGroupsDescribeGET) + settings.GET("/worker-groups/:slug/delete", h.SettingsWorkerGroupsDescribeDELETE) + settings.Match([]string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"}, "/temporal*", h.Temporal) // OAuth2 diff --git a/pkg/server/server.go b/pkg/server/server.go index 28c8ed4..f3081fa 100644 --- a/pkg/server/server.go +++ b/pkg/server/server.go @@ -46,7 +46,7 @@ func (s *Server) Start() error { s.worker = NewWorker(temporalClient, s.cfg) s.echo.Renderer = templates.NewTemplates() - //s.echo.Use(middleware.Logger()) + s.echo.Use(middleware.Logger()) s.echo.Use(middleware.Recover()) Routes(s.echo, db, temporalClient, s.cfg, s.logger) diff --git a/web/templates/pages/settings_monitors_describe.tmpl b/web/templates/pages/settings_monitors_describe.tmpl index 4c5a826..a1dea8e 100644 --- a/web/templates/pages/settings_monitors_describe.tmpl +++ b/web/templates/pages/settings_monitors_describe.tmpl @@ -40,9 +40,16 @@ {{ end }} -

Pausing the monitor will stop it from executing. This can be useful in cases of expected downtime.

- Pause - Resume +

+ Pausing the monitor will stop it from executing. + This can be useful in cases of expected downtime. + Or when the monitor is not needed anymore. +

+ {{ if eq .Monitor.Status "ACTIVE" }} + Pause + {{ else if eq .Monitor.Status "PAUSED" }} + Resume + {{ end }}