-
Notifications
You must be signed in to change notification settings - Fork 4
/
scheduler.go
175 lines (157 loc) · 4.64 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
170
171
172
173
174
175
package scheduler
import (
"context"
"database/sql"
"time"
"github.com/benbjohnson/clock"
"github.com/jackc/pgtype"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
commonv1 "github.com/nanzhong/tstr/api/common/v1"
"github.com/nanzhong/tstr/db"
"github.com/robfig/cron/v3"
"github.com/rs/zerolog/log"
)
const defaultRunTimeout = 5 * time.Minute
type Scheduler struct {
pgxPool *pgxpool.Pool
dbQuerier db.Querier
cronParser cron.Parser
clock clock.Clock
nextSchedule time.Time
retryDelay time.Duration
doneCh chan struct{}
stopCh chan struct{}
stopCancelFn context.CancelFunc
}
func New(pgxPool *pgxpool.Pool) *Scheduler {
return &Scheduler{
pgxPool: pgxPool,
dbQuerier: db.New(),
cronParser: cron.NewParser(cron.Minute | cron.Hour | cron.Dom | cron.Month | cron.Dow | cron.Descriptor),
clock: clock.New(),
doneCh: make(chan struct{}),
stopCh: make(chan struct{}),
}
}
func (s *Scheduler) Start() error {
defer close(s.doneCh)
var ctx context.Context
ctx, s.stopCancelFn = context.WithCancel(context.Background())
s.nextSchedule = s.clock.Now()
for {
select {
case <-s.stopCh:
return nil
case <-ctx.Done():
return ctx.Err()
case <-s.clock.After(s.clock.Until(s.nextSchedule)):
log.Debug().Msg("starting schedule pass")
// First, reset orphaned runs.
orphanThreshold := s.clock.Now().Add(-5 * time.Second)
if err := s.dbQuerier.ResetOrphanedRuns(ctx, s.pgxPool, orphanThreshold); err != nil {
log.Error().Err(err).Msg("failed to reset orphaned runs")
}
// Next, mark timeout tests as having errored.
var timeoutLog pgtype.JSONB
if err := timeoutLog.Set(db.RunLog{
Type: commonv1.Run_Log_TSTR.String(),
Time: s.clock.Now().Format(time.RFC3339Nano),
Data: []byte("run timed out"),
}); err != nil {
log.Error().
Err(err).
Msg("failed to format timeout error log for run")
} else {
if err := s.dbQuerier.TimeoutRuns(ctx, s.pgxPool, db.TimeoutRunsParams{
TimeoutLog: timeoutLog,
DefaultTimeout: int32(defaultRunTimeout.Seconds()),
}); err != nil {
log.Error().Err(err).Msg("failed to time out runs")
}
}
// Next, schedule new runs.
minNextRunAt := s.clock.Now().Add(time.Minute)
err := s.pgxPool.BeginFunc(ctx, func(tx pgx.Tx) error {
tests, err := s.dbQuerier.ListTestsToSchedule(ctx, tx)
if err != nil {
log.Error().Err(err).Msg("failed to list tests to schedule")
return err
}
for _, test := range tests {
runParams, err := RunsForTest(test)
if err != nil {
log.Error().Err(err).Stringer("test_id", test.ID).Msg("generating runs to schedule")
return err
}
for _, runParam := range runParams {
run, err := s.dbQuerier.ScheduleRun(ctx, tx, runParam)
if err != nil {
log.Error().
Err(err).
Stringer("test_id", test.ID).
Stringer("test_matrix_id", runParam.TestMatrixID.UUID).
Msg("failed to schedule run for test")
return err
}
log.Info().
Stringer("test_id", runParam.TestID).
Stringer("run_id", run.ID).
Stringer("test_matrix_id", runParam.TestMatrixID.UUID).
Msg("scheduled run for test")
}
schedule, err := s.cronParser.Parse(test.CronSchedule.String)
if err != nil {
log.Error().
Err(err).
Stringer("test_id", test.ID).
Str("test_cron_schedule", test.CronSchedule.String).
Msg("failed to parse test cron schedule to calculate next run at")
return err
}
nextRunAt := schedule.Next(s.clock.Now())
if nextRunAt.Before(minNextRunAt) {
minNextRunAt = nextRunAt
}
_, err = s.dbQuerier.UpdateTest(ctx, tx, db.UpdateTestParams{
ID: test.ID,
Name: test.Name,
Labels: test.Labels,
Matrix: test.Matrix,
RunConfig: test.RunConfig,
CronSchedule: test.CronSchedule,
NextRunAt: sql.NullTime{Valid: true, Time: nextRunAt},
Namespace: test.Namespace,
})
if err != nil {
log.Error().
Err(err).
Stringer("test_id", test.ID).
Msg("failed to update test with next run at after scheduling")
return err
}
}
return nil
})
if err != nil {
log.Error().
Err(err).
Msg("failed to schedule runs, retrying next pass")
s.nextSchedule = s.clock.Now().Add(s.retryDelay)
} else {
log.Debug().Msg("finished schedule pass")
s.nextSchedule = minNextRunAt
}
}
}
}
func (s *Scheduler) Stop(ctx context.Context) {
close(s.stopCh)
select {
case <-s.doneCh:
case <-ctx.Done():
if s.stopCancelFn != nil {
s.stopCancelFn()
}
}
}