-
Notifications
You must be signed in to change notification settings - Fork 3
/
scheduler.go
128 lines (109 loc) · 3.05 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
package recurring
import (
"sync"
"time"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog/log"
"github.com/lloydmeta/tasques/internal/domain/task"
"github.com/lloydmeta/tasques/internal/domain/task/recurring"
"github.com/lloydmeta/tasques/internal/domain/tracing"
)
type schedulerImpl struct {
cron *cron.Cron
tasksService task.Service
tracer tracing.Tracer
idsToEntryIds map[task.RecurringTaskId]cron.EntryID
mu sync.Mutex
getUTC func() time.Time
}
// Returns the default implementation of a scheduler that delegates to
// the standard robfig/cron
func NewScheduler(tasksService task.Service, tracer tracing.Tracer) recurring.Scheduler {
return &schedulerImpl{
cron: cron.New(cron.WithLocation(time.UTC)),
tasksService: tasksService,
idsToEntryIds: make(map[task.RecurringTaskId]cron.EntryID),
mu: sync.Mutex{},
tracer: tracer,
getUTC: func() time.Time {
return time.Now().UTC()
},
}
}
func (i *schedulerImpl) Schedule(task recurring.Task) error {
i.mu.Lock()
defer i.mu.Unlock()
if log.Info().Enabled() {
log.Info().
Str("id", string(task.ID)).
Str("expression", string(task.ScheduleExpression)).
Msg("Scheduling recurring Task to Cron")
}
if entryId, ok := i.idsToEntryIds[task.ID]; ok {
i.cron.Remove(entryId)
delete(i.idsToEntryIds, task.ID)
}
entryId, err := i.cron.AddFunc(string(task.ScheduleExpression), func() {
if log.Debug().Enabled() {
log.Debug().
Str("id", string(task.ID)).
Str("expression", string(task.ScheduleExpression)).
Msg("Enqueuing Task")
}
tx := i.tracer.BackgroundTx("recurring-task-enqueue")
ctx := tx.Context()
_, err := i.tasksService.Create(ctx, i.taskDefToNewTask(task.ID, &task.TaskDefinition))
if err != nil {
log.Error().
Err(err).
Str("id", string(task.ID)).
Str("expression", string(task.ScheduleExpression)).
Msg("Failed to insert new Task at interval")
}
tx.End()
})
i.idsToEntryIds[task.ID] = entryId
return err
}
func (i *schedulerImpl) Unschedule(taskId task.RecurringTaskId) bool {
i.mu.Lock()
defer i.mu.Unlock()
if entryId, ok := i.idsToEntryIds[taskId]; ok {
if log.Info().Enabled() {
log.Info().
Str("id", string(taskId)).
Msg("Unscheduling recurring Task from Cron")
}
i.cron.Remove(entryId)
delete(i.idsToEntryIds, taskId)
return true
} else {
return false
}
}
func (i *schedulerImpl) Start() {
i.mu.Lock()
defer i.mu.Unlock()
i.cron.Start()
}
func (i *schedulerImpl) Stop() {
i.mu.Lock()
defer i.mu.Unlock()
i.cron.Stop()
}
func (i *schedulerImpl) Parse(spec string) (recurring.Schedule, error) {
return cron.ParseStandard(spec)
}
func (i *schedulerImpl) taskDefToNewTask(id task.RecurringTaskId, def *recurring.TaskDefinition) *task.NewTask {
return &task.NewTask{
Queue: def.Queue,
RetryTimes: def.RetryTimes,
Kind: def.Kind,
Priority: def.Priority,
RunAt: task.RunAt(i.getUTC()),
ProcessingTimeout: def.ProcessingTimeout,
Args: def.Args,
Context: def.Context,
RecurringTaskId: &id,
}
}