Kaynağa Gözat

Export a fatal error handled by the hook manager

gabrielseibel1 2 yıl önce
ebeveyn
işleme
3c6f6f9da2
1 değiştirilmiş dosya ile 13 ekleme ve 1 silme
  1. 13 1
      logic/timer.go

+ 13 - 1
logic/timer.go

@@ -2,7 +2,10 @@ package logic
 
 import (
 	"context"
+	"errors"
 	"fmt"
+	"golang.org/x/exp/slog"
+	"os"
 	"sync"
 	"time"
 
@@ -18,6 +21,9 @@ const timer_hours_between_runs = 24
 // HookManagerCh - channel to add any new hooks
 var HookManagerCh = make(chan models.HookDetails, 2)
 
+// HookManagerFatalError is an error to which is fatal to the hook manager (stops the program!)
+var HookManagerFatalError = errors.New("fatal periodic procedure")
+
 // == Public ==
 
 // TimerCheckpoint - Checks if 24 hours has passed since telemetry was last sent. If so, sends telemetry data to posthog
@@ -70,7 +76,13 @@ func addHookWithInterval(ctx context.Context, wg *sync.WaitGroup, hook func() er
 		case <-ctx.Done():
 			return
 		case <-ticker.C:
-			hook()
+			if err := hook(); err != nil {
+				// stop the server if there was a fatal error
+				if errors.Is(err, HookManagerFatalError) {
+					slog.Error(err.Error())
+					os.Exit(0)
+				}
+			}
 		}
 	}