forked from getsentry/sentry-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crons_monitor_data.go
96 lines (83 loc) · 2.46 KB
/
crons_monitor_data.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package main
import (
"sync"
"github.com/getsentry/sentry-go"
batchv1 "k8s.io/api/batch/v1"
)
// Struct associated with a job
type CronsJobData struct {
CheckinID sentry.EventID
}
// Constructor for cronsMonitorData
func NewCronsJobData(checkinId sentry.EventID) *CronsJobData {
return &CronsJobData{
CheckinID: checkinId,
}
}
func (j *CronsJobData) getCheckinID() sentry.EventID {
return j.CheckinID
}
// Struct associated with a cronJob
type CronsMonitorData struct {
mutex sync.RWMutex
MonitorSlug string
monitorConfig *sentry.MonitorConfig
JobDatas map[string]*CronsJobData
requiredCompletions int32
}
// Constructor for cronsMonitorData
func NewCronsMonitorData(monitorSlug string, schedule string, completions *int32) *CronsMonitorData {
// Get required number of pods to complete
var requiredCompletions int32
if completions == nil {
// If not set, any pod success is enough
requiredCompletions = 1
} else {
requiredCompletions = *completions
}
monitorSchedule := sentry.CrontabSchedule(schedule)
return &CronsMonitorData{
mutex: sync.RWMutex{},
MonitorSlug: monitorSlug,
monitorConfig: &sentry.MonitorConfig{
Schedule: monitorSchedule,
},
JobDatas: make(map[string]*CronsJobData),
requiredCompletions: requiredCompletions,
}
}
// 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)
return nil
}
// Wrapper struct over crons monitor map that
// handles synchronization
type CronsMetaData struct {
mutex *sync.RWMutex
cronsMonitorDataMap map[string]*CronsMonitorData
}
func NewCronsMetaData() *CronsMetaData {
return &CronsMetaData{
mutex: &sync.RWMutex{},
cronsMonitorDataMap: make(map[string]*CronsMonitorData),
}
}
func (c *CronsMetaData) addCronsMonitorData(cronjobName string, newCronsMonitorData *CronsMonitorData) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.cronsMonitorDataMap[cronjobName] = newCronsMonitorData
}
func (c *CronsMetaData) deleteCronsMonitorData(cronjobName string) {
c.mutex.Lock()
defer c.mutex.Unlock()
delete(c.cronsMonitorDataMap, cronjobName)
}
func (c *CronsMetaData) getCronsMonitorData(cronjobName string) (*CronsMonitorData, bool) {
c.mutex.RLock()
defer c.mutex.RUnlock()
cronsMonitorData, ok := c.cronsMonitorDataMap[cronjobName]
return cronsMonitorData, ok
}