generated from goexl/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
schedule_duration.go
43 lines (36 loc) · 964 Bytes
/
schedule_duration.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
package schedule
import (
"time"
"github.com/goexl/gox"
"github.com/goexl/gox/field"
"github.com/goexl/gox/rand"
"github.com/goexl/log"
"github.com/robfig/cron/v3"
)
var _ cron.Schedule = (*scheduleDuration)(nil)
type scheduleDuration struct {
params *addParams
self *durationParams
logger log.Logger
}
func newScheduleDuration(params *addParams, self *durationParams, logger log.Logger) *scheduleDuration {
return &scheduleDuration{
params: params,
self: self,
logger: logger,
}
}
func (sd *scheduleDuration) Next(from time.Time) (next time.Time) {
diff := rand.New().Duration().Between(sd.self.from, sd.self.to).Build().Generate()
next = from.Add(diff)
fields := gox.Fields[any]{
field.New("type", "random.duration"),
field.New("diff", diff.Truncate(time.Second)),
field.New("next", next),
}
if "" != sd.params.name {
fields.Add(field.New("name", sd.params.name))
}
sd.logger.Debug("任务调度", fields...)
return
}