-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.go
153 lines (129 loc) · 4.34 KB
/
task.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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package schedule
import (
"context"
"errors"
"fmt"
"github.com/chanjarster/gears/simplelog"
"time"
)
type Task interface {
// Return the Task name
Name() string
// Start the Task, cannot start a Task twice or start a stopped Task
Start() error
// Start the Task, panic if error happened
MustStart()
// Stop the task, cannot stop a not started Task or stop a Task twice.
// This function only send the stop signal to the Task, does not wait for JobFunc to finish.
Stop() error
// Stop the Task, panic if error happened
MustStop()
}
const (
new = iota
started
stopped
)
// User defined job.
// It's recommended that JobFunc respond to cancel channel.
type JobFunc func(cancel <-chan struct{}) error
type taskCommon struct {
name string // name for the task
job JobFunc // user provided JobFunc
stopSig chan struct{} // internal stop signal channel
interval time.Duration // interval between two runs
timeout time.Duration // timeout for the job
jobQ chan struct{} //
no int64 // count how many time current task has been run
status int // task status, new, started, stopped
}
func (t *taskCommon) Name() string {
return t.name
}
// should be called guard by lock
func (t *taskCommon) markStart() error {
if t.status == started {
return errors.New(fmt.Sprintf("task[%s] start error, you cannot start a Task twice", t.name))
}
if t.status == stopped {
return errors.New(fmt.Sprintf("task[%s] start error, you cannot start a stopped Task", t.name))
}
t.status = started
return nil
}
// should be called guard by lock
func (t *taskCommon) markStopSendSignal() error {
if t.status == new {
return errors.New(fmt.Sprintf("task[%s] stop error, Task is not started", t.name))
}
if t.status == stopped {
return errors.New(fmt.Sprintf("task[%s] stop error, task is already stopped", t.name))
}
t.status = stopped
close(t.stopSig)
// we do not close jobQ because there will be race condition on sending and closing a channel
// https://golang.org/doc/articles/race_detector.html#Unsynchronized_send_and_close_operations
// close(t.jobQ)
return nil
}
func (t *taskCommon) runJob(nextNo int64) {
simplelog.StdLogger.Printf("task[%s] job[#%v] start\n", t.name, nextNo)
timeoutCtx, cancel := context.WithTimeout(context.Background(), t.timeout)
defer cancel()
fin := make(chan struct{})
go func() {
startAt := time.Now()
err := t.job(timeoutCtx.Done())
finishAt := time.Now()
elapse := finishAt.Sub(startAt)
close(fin)
if err != nil {
simplelog.ErrLogger.Printf("task[%s] job[#%v] error: %v\n", t.name, nextNo, err)
}
if ctxErr := timeoutCtx.Err(); ctxErr == context.Canceled {
simplelog.StdLogger.Printf("task[%s] job[#%v] canceled by stop signal, time elapse %v\n", t.name, nextNo, elapse)
} else if ctxErr == context.DeadlineExceeded {
simplelog.StdLogger.Printf("task[%s] job[#%v] canceled due to timeout(%v), time elapse %v\n", t.name, nextNo, t.timeout, elapse)
} else {
simplelog.StdLogger.Printf("task[%s] job[#%v] finished, time elapse %v\n", t.name, nextNo, elapse)
}
}()
select {
case <-timeoutCtx.Done():
<-fin
case <-t.stopSig:
cancel()
<-fin
case <-fin:
// job normally finished
}
}
// Append a JobFunc to jobQ.
// Return false if Task is stopped and no job appended, otherwise return true.
func (t *taskCommon) appendJobQueue() bool {
// because we do not close t.jobQ on stop, so we should check manually whether Task is stopped
select {
case t.jobQ <- struct{}{}:
return true
case <-t.stopSig:
// task is stopped
return false
}
return false
}