diff options
| author | Paul Buetow <paul@buetow.org> | 2024-08-22 10:45:05 +0300 |
|---|---|---|
| committer | Paul Buetow <paul@buetow.org> | 2024-08-22 10:45:05 +0300 |
| commit | 81a3437a14eadde755e5f2b8948d88981cd7daa6 (patch) | |
| tree | 2d445cf85029314b961dd20b53013647b83a8c8b /internal/server | |
| parent | f2b9094e301ac764930feaca93f1f0705f3e1a04 (diff) | |
refactor CRON
Diffstat (limited to 'internal/server')
| -rw-r--r-- | internal/server/cron/cron.go | 25 | ||||
| -rw-r--r-- | internal/server/health/health.go | 6 |
2 files changed, 18 insertions, 13 deletions
diff --git a/internal/server/cron/cron.go b/internal/server/cron/cron.go index b31b48f..af9bdad 100644 --- a/internal/server/cron/cron.go +++ b/internal/server/cron/cron.go @@ -2,7 +2,6 @@ package cron import ( "context" - "fmt" "log" "time" @@ -22,17 +21,23 @@ func Run(ctx context.Context, conf config.ServerConfig, status health.Status) { case <-ctx.Done(): return case <-helloTicker.C: - log.Println("CRON hello ticker ticked") + run(ctx, "cron.Hello", status, func(ctx context.Context) error { + log.Println("hello world") + return nil + }) case <-mergeTicker.C: - log.Println("CRON ticker initiating remote merge operation") - if err := repository.Instance(conf).MergeRemotely(ctx); err != nil { - status.Set(health.Critical, "cron", fmt.Errorf("unable to merge remote repository: %w", err)) - } + run(ctx, "cron.Repository.Merge", status, repository.Instance(conf).MergeRemotely) case <-scheduleTicker.C: - log.Println("CRON ticker initiating schedule operation") - if err := scheduler.Run(ctx); err != nil { - status.Set(health.Critical, "cron", fmt.Errorf("unable to schedule post(s): %w", err)) - } + run(ctx, "cron.Scheduler.Run", status, scheduler.Run) } } } + +func run(ctx context.Context, handlerName string, status health.Status, cb func(ctx context.Context) error) { + log.Println("CRON ticker initiating", handlerName) + if err := cb(ctx); err != nil { + status.Set(health.Critical, handlerName, err) + return + } + status.Clear(handlerName) +} diff --git a/internal/server/health/health.go b/internal/server/health/health.go index a75c475..11f0b84 100644 --- a/internal/server/health/health.go +++ b/internal/server/health/health.go @@ -52,14 +52,14 @@ func NewStatus() Status { } } -func (hs Status) Set(s Severity, what string, info any) { +func (hs Status) Set(s Severity, handlerName string, info any) { hs.mu.Lock() defer hs.mu.Unlock() text := fmt.Sprintf("%v", info) - log.Println("alerting", what, "to", text, "with severity", s) + log.Println("alerting", handlerName, "to", text, "with severity", s) - hs.alerts[what] = alert{ + hs.alerts[handlerName] = alert{ text: text, severity: s, } |
