feat(monitors,workergroups): implement deletion and stauts changes

This commit is contained in:
Tine 2024-02-28 16:40:24 +01:00
parent d9dafd766a
commit 65db9b3f72
Signed by: mentos1386
SSH key fingerprint: SHA256:MNtTsLbihYaWF8j1fkOHfkKNlnN1JQfxEU/rBU8nCGw
7 changed files with 110 additions and 4 deletions

View file

@ -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 { func (h *BaseHandler) SettingsMonitorsDescribePOST(c echo.Context) error {
ctx := context.Background() ctx := context.Background()
monitorSlug := c.Param("slug") monitorSlug := c.Param("slug")

View file

@ -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 { func (h *BaseHandler) SettingsWorkerGroupsCreateGET(c echo.Context) error {
cc := c.(AuthenticatedContext) cc := c.(AuthenticatedContext)

View file

@ -40,6 +40,20 @@ func GetMonitorStatus(ctx context.Context, temporal client.Client, slug string)
return MonitorStatusActive, nil 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 { func CreateMonitor(ctx context.Context, db *sqlx.DB, monitor *models.Monitor) error {
_, err := db.NamedExecContext(ctx, _, err := db.NamedExecContext(ctx,
"INSERT INTO monitors (slug, name, script, schedule) VALUES (:slug, :name, :script, :schedule)", "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 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 { func UpdateMonitorWorkerGroups(ctx context.Context, db *sqlx.DB, monitor *models.Monitor, workerGroups []*models.WorkerGroup) error {
tx, err := db.BeginTxx(ctx, nil) tx, err := db.BeginTxx(ctx, nil)
if err != nil { if err != nil {
@ -204,6 +226,11 @@ ORDER BY monitors.name
return maps.Values(monitors), err 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( func CreateOrUpdateMonitorSchedule(
ctx context.Context, ctx context.Context,
t client.Client, t client.Client,

View file

@ -32,6 +32,14 @@ func CreateWorkerGroup(ctx context.Context, db *sqlx.DB, workerGroup *models.Wor
return err 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) { func GetWorkerGroups(ctx context.Context, db *sqlx.DB) ([]*models.WorkerGroup, error) {
var workerGroups []*models.WorkerGroup var workerGroups []*models.WorkerGroup
err := db.SelectContext(ctx, &workerGroups, err := db.SelectContext(ctx, &workerGroups,

View file

@ -50,10 +50,15 @@ func Routes(
settings.POST("/monitors/create", h.SettingsMonitorsCreatePOST) settings.POST("/monitors/create", h.SettingsMonitorsCreatePOST)
settings.GET("/monitors/:slug", h.SettingsMonitorsDescribeGET) settings.GET("/monitors/:slug", h.SettingsMonitorsDescribeGET)
settings.POST("/monitors/:slug", h.SettingsMonitorsDescribePOST) 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", h.SettingsWorkerGroupsGET)
settings.GET("/worker-groups/create", h.SettingsWorkerGroupsCreateGET) settings.GET("/worker-groups/create", h.SettingsWorkerGroupsCreateGET)
settings.POST("/worker-groups/create", h.SettingsWorkerGroupsCreatePOST) settings.POST("/worker-groups/create", h.SettingsWorkerGroupsCreatePOST)
settings.GET("/worker-groups/:slug", h.SettingsWorkerGroupsDescribeGET) 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) settings.Match([]string{"GET", "HEAD", "POST", "PUT", "PATCH", "DELETE"}, "/temporal*", h.Temporal)
// OAuth2 // OAuth2

View file

@ -46,7 +46,7 @@ func (s *Server) Start() error {
s.worker = NewWorker(temporalClient, s.cfg) s.worker = NewWorker(temporalClient, s.cfg)
s.echo.Renderer = templates.NewTemplates() s.echo.Renderer = templates.NewTemplates()
//s.echo.Use(middleware.Logger()) s.echo.Use(middleware.Logger())
s.echo.Use(middleware.Recover()) s.echo.Use(middleware.Recover())
Routes(s.echo, db, temporalClient, s.cfg, s.logger) Routes(s.echo, db, temporalClient, s.cfg, s.logger)

View file

@ -40,9 +40,16 @@
</span> </span>
{{ end }} {{ end }}
</h2> </h2>
<p class="text-sm mb-2">Pausing the monitor will stop it from executing. This can be useful in cases of expected downtime.</p> <p class="text-sm mb-2">
<a class="block text-center py-2.5 px-5 me-2 mb-2 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded-lg border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-100" href="/settings/monitors/{{ .Monitor.Slug }}/enable">Pause</a> Pausing the monitor will stop it from executing.
<a class="block text-center py-2.5 px-5 me-2 mb-2 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded-lg border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-100" href="/settings/monitors/{{ .Monitor.Slug }}/disable">Resume</a> This can be useful in cases of expected downtime.
Or when the monitor is not needed anymore.
</p>
{{ if eq .Monitor.Status "ACTIVE" }}
<a class="block text-center py-2.5 px-5 me-2 mb-2 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded-lg border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-100" href="/settings/monitors/{{ .Monitor.Slug }}/disable">Pause</a>
{{ else if eq .Monitor.Status "PAUSED" }}
<a class="block text-center py-2.5 px-5 me-2 mb-2 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded-lg border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-100" href="/settings/monitors/{{ .Monitor.Slug }}/enable">Resume</a>
{{ end }}
</section> </section>
<section class="p-2 flex-1 border-4 border-red-300"> <section class="p-2 flex-1 border-4 border-red-300">