-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
308 lines (264 loc) · 8.32 KB
/
store.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package cron
import (
"context"
"database/sql"
"errors"
"fmt"
"sync"
"time"
)
type Store interface {
// Initialize the store
Initialize(ctx context.Context) error
// Lock the store from external read or write
Lock(ctx context.Context) error
// Unlock the store
Unlock(ctx context.Context) error
// GetEntries retrieve only active entries
GetEntries(ctx context.Context) ([]Entry, error)
// AddEntry to the store
AddEntry(ctx context.Context, entry Entry) error
// DeleteEntry from the store
DeleteEntry(ctx context.Context, entry Entry) error
//WriteEvent which is triggered cron entry
AddEvent(ctx context.Context, e Event) error
// GetEvents on [from, to)
GetEvents(ctx context.Context, from, to time.Time) ([]Event, error)
//DeleteEvents
DeleteEvents(ctx context.Context, until time.Time) error
}
type MemStore struct {
entries []Entry
events []Event
sync.Mutex
}
func (m *MemStore) Initialize(ctx context.Context) error {
return nil
}
func (m *MemStore) Lock(ctx context.Context) error {
m.Mutex.Lock()
return nil
}
func (m *MemStore) Unlock(ctx context.Context) error {
m.Mutex.Unlock()
return nil
}
func (m *MemStore) GetEntries(ctx context.Context) ([]Entry, error) {
return m.entries, nil
}
func (m *MemStore) AddEntry(ctx context.Context, entry Entry) error {
m.entries = append(m.entries, entry)
return nil
}
func (m *MemStore) DeleteEntry(ctx context.Context, entry Entry) error {
var new []Entry
for _, v := range m.entries {
if v.expression == entry.expression && v.Name == entry.Name {
continue
}
new = append(new, v)
}
m.entries = new
return nil
}
func (m *MemStore) AddEvent(ctx context.Context, e Event) error {
m.events = append(m.events, e)
return nil
}
func (m *MemStore) GetEvents(ctx context.Context, from, to time.Time) ([]Event, error) {
var ret []Event
for _, v := range m.events {
if (v.Time.Equal(from) || v.Time.After(from)) && v.Time.Before(to) {
ret = append(ret, v)
}
}
return ret, nil
}
func (m *MemStore) DeleteEvents(ctx context.Context, until time.Time) error {
var filtered []Event
for _, v := range m.events {
if v.Time.Equal(until) || v.Time.After(until) {
filtered = append(filtered, v)
}
}
m.events = filtered
return nil
}
var (
// EntriesTable in SQL table that store cron entries
EntriesTable = "_entries"
// EventsTable is SQL table that store executed entries
EventsTable = "_events"
)
type SqlStore struct {
db *sql.DB
tx *sql.Tx
locked bool
}
func NewSQLStore(db *sql.DB) (*SqlStore, error) {
store := &SqlStore{db: db}
return store, nil
}
// Initialize the sql tables if not present
func (s *SqlStore) Initialize(ctx context.Context) error {
// For now this is enough with assumption that this table is going to be stable.
// If in the future we need to migrate this we can introduce `_version` table for doing db migration
// right now, absence of that table marks that this is the initial version
// create entries table
query := fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (
expression varchar(255) NOT NULL,
location varchar(255) NOT NULL,
name varchar(255) NOT NULL,
meta blob DEFAULT NULL,
active tinyint(1) DEFAULT '1',
PRIMARY KEY (expression,location,name)
)
`, EntriesTable)
_, err := s.db.ExecContext(ctx, query)
if err != nil {
return fmt.Errorf("failed creating entries table: %v", err)
}
// create events table
query = fmt.Sprintf(`
CREATE TABLE IF NOT EXISTS %s (
expression varchar(255) NOT NULL,
location varchar(255) NOT NULL,
name varchar(255) NOT NULL,
meta varchar(1024) DEFAULT NULL,
triggered_at timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (expression,location,name,triggered_at)
)`, EventsTable)
_, err = s.db.ExecContext(ctx, query)
if err != nil {
return fmt.Errorf("failed creating events table: %v", err)
}
return nil
}
// Lock the table so that no other session can read or write Entries and Triggered table
func (s *SqlStore) Lock(ctx context.Context) error {
if s.locked || s.tx != nil {
return errors.New("already locked or transaction exists")
}
// we use transaction because it guaranteed to give the same connection from SQL pool
var err error
txOptions := &sql.TxOptions{
Isolation: sql.LevelSerializable, // make sure that none is reading and writing to the table we lock
}
s.tx, err = s.db.BeginTx(ctx, txOptions)
if err != nil {
return fmt.Errorf("failed to create transaction: %v", err)
}
_, err = s.tx.ExecContext(ctx, fmt.Sprintf("LOCK TABLE `%s` WRITE, `%s` WRITE", EntriesTable, EventsTable))
if err != nil {
return err
}
s.locked = true
return nil
}
func (s *SqlStore) Unlock(ctx context.Context) error {
if !s.locked || s.tx == nil {
return errors.New("not locked or transaction not exists")
}
_, err := s.tx.ExecContext(ctx, "UNLOCK TABLES")
if err != nil {
return err
}
s.locked = false
s.tx = nil
return nil
}
func (s *SqlStore) AddEntry(ctx context.Context, entry Entry) error {
if entry.expression == "" {
return errors.New("got empty expression")
}
query := "REPLACE INTO " + EntriesTable + " (expression, location, name, meta) VALUES (?, ?, ?, ?)"
_, err := s.tx.ExecContext(ctx, query, entry.expression, entry.Location.String(), entry.Name, entry.Meta)
if err != nil {
return fmt.Errorf("failed to execute query: %v", err)
}
return nil
}
func (s *SqlStore) GetEntries(ctx context.Context) ([]Entry, error) {
entries := make([]Entry, 0)
query := "SELECT expression, location, name, meta FROM " + EntriesTable + " WHERE active=1"
rows, err := s.tx.QueryContext(ctx, query)
if err != nil {
return nil, fmt.Errorf("failed to query entries from DB: %v", err)
}
for rows.Next() {
var expression, location, name string
var meta sql.NullString
if err := rows.Scan(&expression, &location, &name, &meta); err != nil {
return nil, fmt.Errorf("failed reading a row: %v", err)
}
loc, err := time.LoadLocation(location)
if err != nil {
return nil, fmt.Errorf("failed to load location %q: %v", location, err)
}
entry, err := Parse(expression, loc, name)
if err != nil {
return nil, fmt.Errorf("failed to parse expression:%q loc:%q name:%q: %v", expression, loc, name, err)
}
entry.Meta = meta.String
entries = append(entries, entry)
}
return entries, nil
}
func (s *SqlStore) DeleteEntry(ctx context.Context, entry Entry) error {
query := "DELETE FROM " + EntriesTable + " WHERE expression=? AND location=? AND name=?"
_, err := s.tx.ExecContext(ctx, query, entry.expression, entry.Location.String(), entry.Name)
if err != nil {
return fmt.Errorf("failed to execute query: %v", err)
}
return nil
}
func (s *SqlStore) AddEvent(ctx context.Context, e Event) error {
query := "REPLACE INTO " + EventsTable + " (expression, location, name, triggered_at, meta) VALUES (?, ?, ?, ?, ?)"
expression := e.Entry.expression
location := e.Entry.Location.String()
name := e.Entry.Name
_, err := s.tx.ExecContext(ctx, query, expression, location, name, e.Time, e.Entry.Meta)
if err != nil {
return fmt.Errorf("failed to execute query: %v", err)
}
return nil
}
func (s *SqlStore) GetEvents(ctx context.Context, from, to time.Time) ([]Event, error) {
query := `SELECT expression, location, name, meta, triggered_at from ` + EventsTable + ` WHERE triggered_at >= ? AND triggered_at < ?`
rows, err := s.tx.QueryContext(ctx, query, from, to)
if err != nil {
return nil, fmt.Errorf("failed querying database: %v", err)
}
var events []Event
for rows.Next() {
var ev Event
var expression, location, name string
var meta sql.NullString
var triggeredAt time.Time
if err := rows.Scan(&expression, &location, &name, &meta, &triggeredAt); err != nil {
return nil, fmt.Errorf("failed reading a row: %v", err)
}
loc, err := time.LoadLocation(location)
if err != nil {
return nil, fmt.Errorf("failed to load location %q: %v", location, err)
}
entry, err := Parse(expression, loc, name)
if err != nil {
return nil, fmt.Errorf("failed to load entry expression:%q loc:%q name:%q: %v", expression, loc, name, err)
}
entry.Meta = meta.String
ev.Entry = entry
ev.Time = triggeredAt.In(loc)
events = append(events, ev)
}
return events, nil
}
func (s *SqlStore) DeleteEvents(ctx context.Context, until time.Time) error {
query := "DELETE FROM " + EventsTable + " WHERE triggered_at < ?"
_, err := s.tx.ExecContext(ctx, query, until)
if err != nil {
return fmt.Errorf("failed to execute query: %v", err)
}
return nil
}