-
Notifications
You must be signed in to change notification settings - Fork 0
/
repository_gorm.go
244 lines (227 loc) · 7.55 KB
/
repository_gorm.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package smallben
import (
"gorm.io/gorm"
)
// DefaultCronID is the CronID of a job
// that has not been inserted yet.
const DefaultCronID = int64(0)
// RepositoryGorm implements the Repository
// interface by the means of GORM.
type RepositoryGorm struct {
db *gorm.DB
}
// ErrorTypeIfMismatchCount returns the error returned
// during operations where the number of involved jobs
// is different than the number of expected jobs that
// should have been involved.
func (r *RepositoryGorm) ErrorTypeIfMismatchCount() error {
return gorm.ErrRecordNotFound
}
// RepositoryGormConfig regulates the internal working of the scheduler.
type RepositoryGormConfig struct {
// Dialector is the dialector to use to connect to the database
Dialector gorm.Dialector
// Config is the configuration to use to connect to the database.
Config gorm.Config
}
// NewRepositoryGorm returns an instance of the repository connecting to the given database.
func NewRepositoryGorm(config *RepositoryGormConfig) (*RepositoryGorm, error) {
db, err := gorm.Open(config.Dialector, &config.Config)
if err != nil {
return nil, err
}
return &RepositoryGorm{db: db}, nil
}
// AddJobs adds `jobs` to the database. This operation can fail
// if the job serialized fails, or for database errors.
func (r *RepositoryGorm) AddJobs(jobs []JobWithSchedule) error {
rawJobs := make([]RawJob, len(jobs))
for i, job := range jobs {
rawJob, err := job.BuildJob()
if err != nil {
return err
}
rawJobs[i] = rawJob
}
return r.db.Create(&rawJobs).Error
}
// GetJob returns the JobWithSchedule whose id is `jobID`.
// In case the job is not found, an error of type gorm.ErrRecordNotFound is returned.
func (r *RepositoryGorm) GetJob(jobID int64) (JobWithSchedule, error) {
var rawJob RawJob
if err := r.db.First(&rawJob, "id = ?", jobID).Error; err != nil {
return JobWithSchedule{}, err
}
// now, convert it to a JobWithSchedule
job, err := rawJob.ToJobWithSchedule()
return job, err
}
// PauseJobs pause jobs whose id are in `jobs`.
// It returns an error `gorm.ErrRecordNotFound` in case
// the number of updated rows is different than the length of jobsToAdd.
func (r *RepositoryGorm) PauseJobs(jobs []RawJob) error {
return r.updatePausedField(jobs, true)
}
// PauseJobs resume jobs whose id are in `jobs`.
// It returns an error `gorm.ErrRecordNotFound` in case
// the number of updated rows is different then the length of jobsToAdd.
func (r *RepositoryGorm) ResumeJobs(jobs []JobWithSchedule) error {
result := r.db.Table("jobs").Where("id in ?", getIdsFromJobsWithScheduleList(jobs)).Updates(map[string]interface{}{"paused": false})
if result.Error != nil {
return result.Error
}
if result.RowsAffected != int64(len(jobs)) {
return gorm.ErrRecordNotFound
}
return nil
}
// GetAllJobsToExecute returns all the jobs whose `paused` field is set to `false`.
func (r *RepositoryGorm) GetAllJobsToExecute() ([]JobWithSchedule, error) {
paused := false
// build the struct to make the list query
// and make the query
rawJobs, err := r.ListJobs(&ListJobsOptions{
Paused: &paused,
})
if err != nil {
return nil, err
}
// now, convert the raw jobs to instances of JobWithSchedule.
jobs := make([]JobWithSchedule, len(rawJobs))
for i, rawJob := range rawJobs {
job, err := rawJob.ToJobWithSchedule()
if err != nil {
return nil, err
}
jobs[i] = job
}
return jobs, nil
}
// GetJobsByIds returns all the jobsToAdd whose ids are in `jobsID`.
// Returns an error of type `gorm.ErrRecordNotFound` in case
// there are less jobs than the requested ones.
func (r *RepositoryGorm) GetJobsByIds(jobsID []int64) ([]JobWithSchedule, error) {
// built the struct to do the list query
// and execute it
rawJobs, err := r.ListJobs(&ListJobsOptions{
JobIDs: jobsID,
})
if err != nil {
return nil, err
}
jobs := make([]JobWithSchedule, len(rawJobs))
for i, rawJob := range rawJobs {
job, err := rawJob.ToJobWithSchedule()
if err != nil {
return nil, err
}
jobs[i] = job
}
return jobs, nil
}
// DeleteJobsByIds delete jobs whose ids are 'jobsID`, returning an error
// of type gorm.ErrRecordNotFound if the number of deleted jobs is less
// than the length of `jobsID`.
func (r *RepositoryGorm) DeleteJobsByIds(jobsID []int64) error {
result := r.db.Delete(&RawJob{}, jobsID)
if result.Error != nil {
return result.Error
}
if result.RowsAffected != int64(len(jobsID)) {
return gorm.ErrRecordNotFound
}
return nil
}
// SetCronId updates the cron_id field of `jobs`.
func (r *RepositoryGorm) SetCronId(jobs []JobWithSchedule) error {
err := r.db.Transaction(func(tx *gorm.DB) error {
for _, job := range jobs {
result := tx.Model(&job.rawJob).Update("cron_id", job.rawJob.CronID)
if result.Error != nil {
return result.Error
}
if result.RowsAffected != int64(1) {
return gorm.ErrRecordNotFound
}
}
return nil
})
return err
}
// SetCronIdAndChangeScheduleAndJobInput updates the fields `cron_id`, `cron_expression`
// and `serialized_job_input` of jobs.
//
// In particular, the job input must have been set internally, since
// this call will encode the job input.
func (r *RepositoryGorm) SetCronIdAndChangeScheduleAndJobInput(jobs []JobWithSchedule) error {
err := r.db.Transaction(func(tx *gorm.DB) error {
for _, job := range jobs {
// build the input of the job.
if err := job.encodeJobInput(); err != nil {
// if it fails, the transaction is aborted.
return err
}
// now, do the update.
result := tx.Model(&job.rawJob).Updates(map[string]interface{}{
"cron_id": job.rawJob.CronID,
"cron_expression": job.rawJob.CronExpression,
"serialized_job_input": job.rawJob.SerializedJobInput,
})
if result.Error != nil {
return result.Error
}
if result.RowsAffected != int64(1) {
return gorm.ErrRecordNotFound
}
}
return nil
})
return err
}
// ListJobs list all jobs using options. If nil, no options will
// be used, thus returning all the jobs.
func (r *RepositoryGorm) ListJobs(options ToListOptions) ([]RawJob, error) {
var jobs []RawJob
var query = r.db.Session(&gorm.Session{WithConditions: true})
if options != nil {
convertedOptions := options.toListOptions()
if convertedOptions.Paused != nil {
if *convertedOptions.Paused == true {
query = query.Where("paused = ?", true)
} else if *convertedOptions.Paused == false {
query = query.Where("paused = ?", false)
}
}
if convertedOptions.JobIDs != nil && len(convertedOptions.JobIDs) > 0 {
query = query.Where("id in (?)", convertedOptions.JobIDs)
}
if convertedOptions.GroupIDs != nil && len(convertedOptions.GroupIDs) > 0 {
query = query.Where("group_id in (?)", convertedOptions.GroupIDs)
}
if convertedOptions.SuperGroupIDs != nil && len(convertedOptions.SuperGroupIDs) > 0 {
query = query.Where("super_group_id in (?)", convertedOptions.SuperGroupIDs)
}
}
err := query.Find(&jobs).Error
// a check for gorm.ErrRecordNotFound if we require only the job id
if options != nil {
convertedOptions := options.toListOptions()
if convertedOptions.JobIDs != nil && convertedOptions.SuperGroupIDs == nil &&
convertedOptions.GroupIDs == nil && convertedOptions.Paused == nil {
if len(jobs) != len(convertedOptions.JobIDs) {
err = gorm.ErrRecordNotFound
}
}
}
return jobs, err
}
func (r *RepositoryGorm) updatePausedField(jobs []RawJob, paused bool) error {
result := r.db.Table("jobs").Where("id in ?", getIdsFromJobRawList(jobs)).Updates(map[string]interface{}{"paused": paused, "cron_id": 0})
if result.Error != nil {
return result.Error
}
if result.RowsAffected != int64(len(jobs)) {
return gorm.ErrRecordNotFound
}
return nil
}