about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-05-15 21:09:02 +0200
committerAlan Pearce2024-05-15 21:09:02 +0200
commitfa97ca832d6dc301dabb5d9c76730630f99534e0 (patch)
treee34b587ba42c78df184f136198fd0093d916131e
parentd65d80e1133eebf11cc1adb3438a1cc53635185f (diff)
downloadsearchix-fa97ca832d6dc301dabb5d9c76730630f99534e0.tar.lz
searchix-fa97ca832d6dc301dabb5d9c76730630f99534e0.tar.zst
searchix-fa97ca832d6dc301dabb5d9c76730630f99534e0.zip
feat: monitor import jobs via sentry
-rw-r--r--internal/server/mux.go13
-rw-r--r--searchix.go75
2 files changed, 60 insertions, 28 deletions
diff --git a/internal/server/mux.go b/internal/server/mux.go
index e15535f..f49767b 100644
--- a/internal/server/mux.go
+++ b/internal/server/mux.go
@@ -20,7 +20,6 @@ import (
 	"searchix/internal/options"
 
 	"github.com/blevesearch/bleve/v2"
-	"github.com/getsentry/sentry-go"
 	sentryhttp "github.com/getsentry/sentry-go/http"
 	"github.com/osdevisnot/sorvor/pkg/livereload"
 	"github.com/pkg/errors"
@@ -83,20 +82,10 @@ func NewMux(
 	index *search.ReadIndex,
 	liveReload bool,
 ) (*http.ServeMux, error) {
-	err := sentry.Init(sentry.ClientOptions{
-		EnableTracing:    true,
-		TracesSampleRate: 1.0,
-		Dsn:              cfg.Web.SentryDSN,
-		Environment:      cfg.Web.Environment,
-	})
-	if err != nil {
-		return nil, errors.WithMessage(err, "could not set up sentry")
-	}
-	defer sentry.Flush(2 * time.Second)
+	var err error
 	sentryHandler := sentryhttp.New(sentryhttp.Options{
 		Repanic: true,
 	})
-
 	templates, err = loadTemplates()
 	if err != nil {
 		log.Panicf("could not load templates: %v", err)
diff --git a/searchix.go b/searchix.go
index ce1f5e1..9917969 100644
--- a/searchix.go
+++ b/searchix.go
@@ -15,6 +15,7 @@ import (
 	"searchix/internal/index"
 	"searchix/internal/server"
 
+	"github.com/getsentry/sentry-go"
 	"github.com/pelletier/go-toml/v2"
 )
 
@@ -65,6 +66,16 @@ func main() {
 		log.SetFlags(log.LstdFlags)
 	}
 
+	err = sentry.Init(sentry.ClientOptions{
+		EnableTracing:    true,
+		TracesSampleRate: 1.0,
+		Dsn:              cfg.Web.SentryDSN,
+		Environment:      cfg.Web.Environment,
+	})
+	if err != nil {
+		slog.Warn("could not initialise sentry", "error", err)
+	}
+
 	read, write, exists, err := index.OpenOrCreate(cfg.DataPath, *replace)
 	if err != nil {
 		log.Fatalf("Failed to open or create index: %v", err)
@@ -94,23 +105,54 @@ func main() {
 		slog.Debug("server stopped")
 	}()
 
-	go func() {
-		nextRun := nextOccurrenceOfLocalTime(cfg.Importer.UpdateAt)
-		for {
-			slog.Debug("scheduling next run", "next-run", nextRun)
-			<-time.After(time.Until(nextRun))
-			wg.Add(1)
-			slog.Info("updating index")
-			err = importer.Start(cfg, write, false)
-			wg.Done()
-			if err != nil {
-				slog.Warn("error updating index", "error", err)
-			} else {
-				slog.Info("update complete")
+	go func(localHub *sentry.Hub) {
+		const monitorSlug = "import"
+		localHub.WithScope(func(scope *sentry.Scope) {
+			scope.SetContext("monitor", sentry.Context{"slug": monitorSlug})
+			monitorConfig := &sentry.MonitorConfig{
+				Schedule: sentry.IntervalSchedule(1, sentry.MonitorScheduleUnitDay),
+				// minutes
+				MaxRuntime:    10,
+				CheckInMargin: 5,
+				Timezone:      time.Local.String(),
 			}
-			nextRun = nextRun.AddDate(0, 0, 1)
-		}
-	}()
+
+			nextRun := nextOccurrenceOfLocalTime(cfg.Importer.UpdateAt)
+			for {
+				slog.Debug("scheduling next run", "next-run", nextRun)
+				<-time.After(time.Until(nextRun))
+				wg.Add(1)
+				slog.Info("updating index")
+
+				eventID := localHub.CaptureCheckIn(&sentry.CheckIn{
+					MonitorSlug: monitorSlug,
+					Status:      sentry.CheckInStatusInProgress,
+				}, monitorConfig)
+
+				err = importer.Start(cfg, write, false)
+				wg.Done()
+				if err != nil {
+					slog.Warn("error updating index", "error", err)
+
+					localHub.CaptureException(err)
+					localHub.CaptureCheckIn(&sentry.CheckIn{
+						ID:          *eventID,
+						MonitorSlug: monitorSlug,
+						Status:      sentry.CheckInStatusError,
+					}, monitorConfig)
+				} else {
+					slog.Info("update complete")
+
+					localHub.CaptureCheckIn(&sentry.CheckIn{
+						ID:          *eventID,
+						MonitorSlug: monitorSlug,
+						Status:      sentry.CheckInStatusOK,
+					}, monitorConfig)
+				}
+				nextRun = nextRun.AddDate(0, 0, 1)
+			}
+		})
+	}(sentry.CurrentHub().Clone())
 
 	sErr := make(chan error)
 	wg.Add(1)
@@ -128,5 +170,6 @@ func main() {
 		// Error starting or closing listener:
 		log.Fatalf("error: %v", err)
 	}
+	sentry.Flush(2 * time.Second)
 	wg.Wait()
 }