generated from goexl/template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
job_once.go
57 lines (47 loc) · 1.02 KB
/
job_once.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
package schedule
import (
"github.com/goexl/gox"
"github.com/goexl/gox/field"
"github.com/goexl/log"
)
type jobOnce struct {
id *string
scheduler *Scheduler
worker worker
logger log.Logger
deleted bool
executed bool
}
func newOnceJob(id *string, scheduler *Scheduler, worker worker, logger log.Logger) *jobOnce {
return &jobOnce{
id: id,
scheduler: scheduler,
worker: worker,
logger: logger,
}
}
func (jo *jobOnce) Run() {
// 在任何情况下,确保任务被删除
defer jo.cleanup()
// 只能被执行一次
if jo.executed {
return
}
jo.executed = true
fields := gox.Fields[any]{
field.New("worker", jo.worker),
}
if err := jo.worker.Run(); nil != err {
jo.logger.Warn("任务执行出错", fields.Add(field.Error(err))...)
} else {
jo.logger.Debug("任务执行成功", fields...)
}
// 删除原来的任务,确保不会再被执行
jo.scheduler.remove(*jo.id)
jo.deleted = true
}
func (jo *jobOnce) cleanup() {
if !jo.deleted {
jo.scheduler.remove(*jo.id)
}
}