-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask.go
More file actions
55 lines (43 loc) · 776 Bytes
/
task.go
File metadata and controls
55 lines (43 loc) · 776 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
52
53
54
55
package pool
import (
"fmt"
"math/rand"
"time"
)
type Task interface {
Execute() error
}
var _ Task = PoolTask{}
type PoolTask struct {
Name string
Handle func() error
}
func (t PoolTask) Execute() error {
if t.Handle == nil {
return fmt.Errorf("PoolTask.Handle is nil")
}
return t.Handle()
}
func (t PoolTask) GetName() string {
return t.Name
}
var _ Task = (*nonTask)(nil)
type nonTask struct{}
func (nt *nonTask) Execute() error {
return nil
}
func GetNonTask() Task {
return &nonTask{}
}
// for test
type InvalidTask struct {
Name string
}
func (it *InvalidTask) Execute() error {
rn := float32(rand.Intn(4)) / 4
time.Sleep(time.Duration(rn*1000) * time.Millisecond)
if rn < 0.5 {
return fmt.Errorf("testHandle: %f", rn)
}
return nil
}