-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdelay.go
More file actions
51 lines (44 loc) · 793 Bytes
/
delay.go
File metadata and controls
51 lines (44 loc) · 793 Bytes
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
package schedule
import (
"time"
)
// RunOnceTimeAfter 在一定时间后执行
//
// # Note
//
// 这是一个一次性任务,只会执行一次,不会重复执行
func (j *taskJob) RunOnceTimeAfter(delay time.Duration) (cancel CancelFunc) {
timer := time.After(delay)
cancel = j.cancelFunc
j.wrappedTaskFunc = func() {
j.running = true
defer func() {
j.running = false
}()
if !j.withoutOverlapping {
j.task()
return
}
if lockDriver.tryLock(j.lockerKey) {
defer func() {
lockDriver.unlock(j.lockerKey)
j.lockedByMe = false
}()
j.lockedByMe = true
j.task()
}
}
go func() {
LOOP:
for {
select {
case <-timer:
go j.wrappedTaskFunc()
break LOOP
case <-j.cancelTaskChan:
break LOOP
}
}
}()
return cancel
}