-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathactuator.go
127 lines (108 loc) · 2.83 KB
/
actuator.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
package worker
import (
"fmt"
"github.com/fujiahui/talnet-challenge-payman/common"
"github.com/fujiahui/talnet-challenge-payman/logger"
"github.com/fujiahui/talnet-challenge-payman/worker/util"
"strings"
"time"
)
// Actuator Task执行管理器
type Actuator struct {
currTimestamp common.TimestampType
jobs map[common.JobIDType]*util.Job
capacity common.PointType
executingPoint common.PointType
}
func NewActuator(startTimestamp common.TimestampType, capacity common.PointType) *Actuator {
return &Actuator{
currTimestamp: startTimestamp,
jobs: make(map[common.JobIDType]*util.Job),
capacity: capacity,
executingPoint: 0,
}
}
func (c *Actuator) Capacity() common.PointType {
return c.capacity
}
func (c *Actuator) ExecutingPoint() common.PointType {
return c.executingPoint
}
func (c *Actuator) CurrTimestamp() common.TimestampType {
return c.currTimestamp
}
func (c *Actuator) FreePoint() common.PointType {
return c.capacity - c.executingPoint
}
// Ticking 滴答滴答向前一步步大胆的滴答
func (c *Actuator) Ticking(tick int) []*util.Job {
time.Sleep(time.Duration(tick) * time.Millisecond)
c.currTimestamp++
ids := make([]common.JobIDType, 0, 16)
jobs := make([]*util.Job, 0, 16)
for id, job := range c.jobs {
c.executingPoint -= 1
t := job.CurrTask()
t.Ticking()
if t.Finished() {
ids = append(ids, id)
job.NextTask(c.currTimestamp)
if !job.Finished() {
jobs = append(jobs, job)
}
}
}
for _, id := range ids {
delete(c.jobs, id)
}
return jobs
}
// Execute 执行一个Job
func (c *Actuator) Execute(job *util.Job) {
if _, ok := c.jobs[job.ID()]; ok {
// job的tasks存在非顺序执行
logger.Panicf("job not sequentially executes job tasks")
}
c.jobs[job.ID()] = job
job.SetRunning()
t := job.CurrTask()
t.SetRunning()
c.executingPoint += t.TaskPoint()
if c.executingPoint > c.capacity {
// 容量超限
logger.Panicf("Actuator's executingPoint %d more than capacity %d ", c.executingPoint, c.capacity)
}
return
}
func (c *Actuator) String() string {
jj := make([]string, 0, 16)
for _, job := range c.jobs {
jj = append(jj, job.String())
}
tt := make([]string, 3, 4)
currTimestamp := int(c.currTimestamp)
for i := 2; i >= 0; i-- {
tt[i] = fmt.Sprintf("%.2d", currTimestamp%60)
currTimestamp /= 60
}
return fmt.Sprintf("%s | %s | %d",
strings.Join(tt, ":"),
strings.Join(jj, ","),
c.executingPoint)
}
func (c *Actuator) StringWithPriority() string {
ss := make([]string, 0, 16)
for _, job := range c.jobs {
ss = append(ss, job.StringWithPriority())
}
tt := make([]string, 3, 4)
currTimestamp := int(c.currTimestamp)
for i := 2; i >= 0; i-- {
tt[i] = fmt.Sprintf("%.2d", currTimestamp%60)
currTimestamp /= 60
}
return fmt.Sprintf("%s | %s | %d",
strings.Join(tt, ":"),
strings.Join(ss, ","),
c.executingPoint)
}