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/cron | |
| parent | f2b9094e301ac764930feaca93f1f0705f3e1a04 (diff) | |
refactor CRON
Diffstat (limited to 'internal/server/cron')
| -rw-r--r-- | internal/server/cron/cron.go | 25 |
1 files changed, 15 insertions, 10 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) +} |
