Skip to content

Commit

Permalink
use defer for unlocking
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiahui-Zhang-20 committed Nov 28, 2023
1 parent 4021fb1 commit 0b37149
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 15 deletions.
8 changes: 4 additions & 4 deletions crons_monitor_data.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func NewCronsMonitorData(monitorSlug string, schedule string, maxRunTime int64,
// Add a job to the crons monitor
func (c *CronsMonitorData) addJob(job *batchv1.Job, checkinId sentry.EventID) error {
c.mutex.Lock()
defer c.mutex.Unlock()
c.JobDatas[job.Name] = NewCronsJobData(checkinId)
c.mutex.Unlock()
return nil
}

Expand All @@ -74,20 +74,20 @@ type CronsMetaData struct {

func (c *CronsMetaData) addCronsMonitorData(cronjobName string, newCronsMonitorData *CronsMonitorData) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.cronsMonitorDataMap[cronjobName] = newCronsMonitorData
c.mutex.Unlock()
}

func (c *CronsMetaData) deleteCronsMonitorData(cronjobName string) {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.cronsMonitorDataMap, cronjobName)
c.mutex.Unlock()
}

func (c *CronsMetaData) getCronsMonitorData(cronjobName string) (*CronsMonitorData, bool) {
c.mutex.RLock()
defer c.mutex.RUnlock()
cronsMonitorData, ok := c.cronsMonitorDataMap[cronjobName]
c.mutex.RUnlock()
return cronsMonitorData, ok
}

Expand Down
12 changes: 1 addition & 11 deletions watcher_pods.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,6 @@ func handlePodWatchEvent(ctx context.Context, event *watch.Event) {

eventObjectRaw := event.Object

// err := runSentryCronsCheckin(ctx, event)
// if err != nil {
// return
// }

if event.Type != watch.Modified {
logger.Debug().Msgf("Skipping a pod watch event of type %s", event.Type)
return
Expand Down Expand Up @@ -188,18 +183,13 @@ func watchPodsInNamespaceForever(ctx context.Context, config *rest.Config, names

ctx = setClientsetOnContext(ctx, clientset)

// create the informers to integrate with sentry crons
// Create the informers to integrate with sentry crons
if isTruthy(os.Getenv("SENTRY_K8S_MONITOR_CRONJOBS")) {
// cronsInformerData := make(map[string]CronsMonitorData)
// ctx := context.WithValue(ctx, CronsInformerDataKey{}, &cronsInformerData)
logger.Info().Msgf("Enabling CronJob monitoring")

go startCronsInformers(ctx, namespace)

} else {
logger.Info().Msgf("CronJob monitoring is disabled")
}

for {
if err := watchPodsInNamespace(ctx, namespace); err != nil {
logger.Error().Msgf("Error while watching pods %s: %s", where, err)
Expand Down

0 comments on commit 0b37149

Please sign in to comment.