forked from getsentry/sentry-kubernetes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crons_monitor_data.go
97 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
97
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 syncrhonization
type CronsMetaData struct {
mutex *sync.RWMutex
cronsMonitorDataMap 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
}
func NewCronsMetaData() *CronsMetaData {
return &CronsMetaData{
mutex: &sync.RWMutex{},
cronsMonitorDataMap: make(map[string]*CronsMonitorData),
}
}