-
Notifications
You must be signed in to change notification settings - Fork 0
/
cron_job.go
60 lines (53 loc) · 1.22 KB
/
cron_job.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
// Copyright (c) 2019 Benjamin Borbe All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package cron
import (
"context"
"time"
"github.com/bborbe/run"
"github.com/golang/glog"
)
//go:generate go run -mod=vendor github.com/maxbrunsfeld/counterfeiter/v6 -o mocks/cron-job.go --fake-name CronJob . CronJob
type CronJob interface {
Run(ctx context.Context) error
}
func NewCronJob(
oneTime bool,
expression string,
wait time.Duration,
action run.Runnable,
) CronJob {
return &cronJob{
oneTime: oneTime,
expression: expression,
wait: wait,
action: action,
}
}
type cronJob struct {
oneTime bool
expression string
wait time.Duration
action run.Runnable
}
func (c *cronJob) Run(ctx context.Context) error {
var runner Cron
if c.oneTime {
glog.V(2).Infof("create one-time cron")
runner = NewOneTimeCron(c.action)
} else if len(c.expression) > 0 {
glog.V(2).Infof("create cron with expression %s", c.expression)
runner = NewExpressionCron(
c.expression,
c.action,
)
} else {
glog.V(2).Infof("create cron with wait %v", c.wait)
runner = NewWaitCron(
c.wait,
c.action,
)
}
return runner.Run(ctx)
}