Skip to content

Commit

Permalink
fix: add mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
MuZhou233 committed Mar 11, 2024
1 parent 7c28bbe commit bb884c9
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions internal/lib/libcron/sentry_listener.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package libcron

import (
"math"
"sync"
"time"

"github.com/getsentry/sentry-go"
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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
}
Expand All @@ -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
}
Expand Down

0 comments on commit bb884c9

Please sign in to comment.