-
Notifications
You must be signed in to change notification settings - Fork 13
/
scheduler.go
169 lines (147 loc) · 4.15 KB
/
scheduler.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
package main
import (
"context"
"database/sql"
"log"
"time"
"github.com/robfig/cron/v3"
)
// Scheduler data structure
type Scheduler struct {
db *sql.DB
listeners Listeners
cron *cron.Cron
cronEntries map[string]cron.EntryID
}
// Listeners has attached event listeners
type Listeners map[string]ListenFunc
// ListenFunc function that listens to events
type ListenFunc func(string)
// Event structure
type Event struct {
ID uint
Name string
Payload string
Cron string
}
// NewScheduler creates a new scheduler
func NewScheduler(db *sql.DB, listeners Listeners) Scheduler {
return Scheduler{
db: db,
listeners: listeners,
cron: cron.New(),
cronEntries: map[string]cron.EntryID{},
}
}
// AddListener adds the listener function to Listeners
func (s Scheduler) AddListener(event string, listenFunc ListenFunc) {
s.listeners[event] = listenFunc
}
// callListeners calls the event listener of provided event
func (s Scheduler) callListeners(event Event) {
eventFn, ok := s.listeners[event.Name]
if ok {
go eventFn(event.Payload)
_, err := s.db.Exec(`DELETE FROM "public"."jobs" WHERE "id" = $1`, event.ID)
if err != nil {
log.Print("💀 error: ", err)
}
} else {
log.Print("💀 error: couldn't find event listeners attached to ", event.Name)
}
}
// CheckEventsInInterval checks the event in given interval
func (s Scheduler) CheckEventsInInterval(ctx context.Context, duration time.Duration) {
ticker := time.NewTicker(duration)
go func() {
for {
select {
case <-ctx.Done():
ticker.Stop()
return
case <-ticker.C:
log.Println("⏰ Ticks Received...")
events := s.checkDueEvents()
for _, e := range events {
s.callListeners(e)
}
}
}
}()
}
// checkDueEvents checks and returns due events
func (s Scheduler) checkDueEvents() []Event {
events := []Event{}
rows, err := s.db.Query(`SELECT "id", "name", "payload" FROM "public"."jobs" WHERE "runAt" < $1 AND "cron"='-'`, time.Now())
if err != nil {
log.Print("💀 error: ", err)
return nil
}
for rows.Next() {
evt := Event{}
rows.Scan(&evt.ID, &evt.Name, &evt.Payload)
events = append(events, evt)
}
return events
}
// Schedule sechedules the provided events
func (s Scheduler) Schedule(event string, payload string, runAt time.Time) {
log.Print("🚀 Scheduling event ", event, " to run at ", runAt)
_, err := s.db.Exec(`INSERT INTO "public"."jobs" ("name", "payload", "runAt") VALUES ($1, $2, $3)`, event, payload, runAt)
if err != nil {
log.Print("schedule insert error: ", err)
}
}
// ScheduleCron schedules a cron job
func (s Scheduler) ScheduleCron(event string, payload string, cron string) {
log.Print("🚀 Scheduling event ", event, " with cron string ", cron)
entryID, ok := s.cronEntries[event]
if ok {
s.cron.Remove(entryID)
_, err := s.db.Exec(`UPDATE "public"."jobs" SET "cron" = $1 , "payload" = $2 WHERE "name" = $3 AND "cron" != '-'`, cron, payload, event)
if err != nil {
log.Print("schedule cron update error: ", err)
}
} else {
_, err := s.db.Exec(`INSERT INTO "public"."jobs" ("name", "payload", "runAt", "cron") VALUES ($1, $2, $3, $4)`, event, payload, time.Now(), cron)
if err != nil {
log.Print("schedule cron insert error: ", err)
}
}
eventFn, ok := s.listeners[event]
if ok {
entryID, err := s.cron.AddFunc(cron, func() { eventFn(payload) })
s.cronEntries[event] = entryID
if err != nil {
log.Print("💀 error: ", err)
}
}
}
// attachCronJobs attaches cron jobs
func (s Scheduler) attachCronJobs() {
log.Printf("Attaching cron jobs")
rows, err := s.db.Query(`SELECT "id", "name", "payload", "cron" FROM "public"."jobs" WHERE "cron"!='-'`)
if err != nil {
log.Print("💀 error: ", err)
}
for rows.Next() {
evt := Event{}
rows.Scan(&evt.ID, &evt.Name, &evt.Payload, &evt.Cron)
eventFn, ok := s.listeners[evt.Name]
if ok {
entryID, err := s.cron.AddFunc(evt.Cron, func() { eventFn(evt.Payload) })
s.cronEntries[evt.Name] = entryID
if err != nil {
log.Print("💀 error: ", err)
}
}
}
}
// StartCron starts cron job
func (s Scheduler) StartCron() func() {
s.attachCronJobs()
s.cron.Start()
return func() {
s.cron.Stop()
}
}