From bb884c955c61dbe333858300eb972f265d5536e7 Mon Sep 17 00:00:00 2001 From: MuZhou233 Date: Mon, 11 Mar 2024 00:41:28 +0000 Subject: [PATCH] fix: add mutex --- internal/lib/libcron/sentry_listener.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/internal/lib/libcron/sentry_listener.go b/internal/lib/libcron/sentry_listener.go index 5529c7b..2f6d943 100644 --- a/internal/lib/libcron/sentry_listener.go +++ b/internal/lib/libcron/sentry_listener.go @@ -2,6 +2,7 @@ package libcron import ( "math" + "sync" "time" "github.com/getsentry/sentry-go" @@ -12,16 +13,20 @@ import ( type sentryListener struct { idMap map[uuid.UUID]*sentry.EventID configMap map[string]*sentry.MonitorConfig + mu sync.Mutex } func newSentryListener() *sentryListener { return &sentryListener{ idMap: make(map[uuid.UUID]*sentry.EventID), configMap: make(map[string]*sentry.MonitorConfig), + mu: sync.Mutex{}, } } func (s *sentryListener) NewDurationJob(name string, duration time.Duration) { + s.mu.Lock() + defer s.mu.Unlock() s.configMap[name] = &sentry.MonitorConfig{ //nolint:exhaustruct // no need Schedule: sentry.IntervalSchedule(int64(math.Ceil(duration.Minutes())), sentry.MonitorScheduleUnitMinute), CheckInMargin: 1, @@ -31,6 +36,8 @@ func (s *sentryListener) NewDurationJob(name string, duration time.Duration) { func (s *sentryListener) EventListeners() []gocron.EventListener { return []gocron.EventListener{ gocron.BeforeJobRuns(func(jobID uuid.UUID, jobName string) { + s.mu.Lock() + defer s.mu.Unlock() s.idMap[jobID] = sentry.CaptureCheckIn( &sentry.CheckIn{ //nolint:exhaustruct // no need MonitorSlug: jobName, @@ -40,6 +47,8 @@ func (s *sentryListener) EventListeners() []gocron.EventListener { ) }), gocron.AfterJobRuns(func(jobID uuid.UUID, jobName string) { + s.mu.Lock() + defer s.mu.Unlock() if s.idMap[jobID] == nil { return } @@ -53,6 +62,8 @@ func (s *sentryListener) EventListeners() []gocron.EventListener { ) }), gocron.AfterJobRunsWithError(func(jobID uuid.UUID, jobName string, err error) { + s.mu.Lock() + defer s.mu.Unlock() if s.idMap[jobID] == nil { return }