/
plan.go
145 lines (122 loc) · 2.7 KB
/
plan.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
package main
import (
"fmt"
"sync"
"gopnik"
)
const (
PENDING = 0
INPROGRESS = 1
DONE = 2
FAILED = 3
)
type plan struct {
bboxes []gopnik.TileCoord
status []uint8
cursor int
attempts []uint8
mu sync.Mutex
cond *sync.Cond
condMu sync.Mutex
}
var max_attemps = (uint8)(10)
func newPlan(bboxes []gopnik.TileCoord) *plan {
self := &plan{
bboxes: bboxes,
status: make([]uint8, len(bboxes)),
attempts: make([]uint8, len(bboxes)),
}
self.cond = sync.NewCond(&self.condMu)
return self
}
func (self *plan) TotalTasks() int {
return len(self.bboxes)
}
func (self *plan) DoneTasks() int {
self.mu.Lock()
defer self.mu.Unlock()
return self.countTasks(DONE)
}
func (self *plan) FailedTasks() int {
self.mu.Lock()
defer self.mu.Unlock()
return self.countTasks(FAILED)
}
func (self *plan) ProgressTasksCoord() (coordInProg []gopnik.TileCoord) {
self.mu.Lock()
defer self.mu.Unlock()
for i, v := range self.status {
if v == INPROGRESS {
coordInProg = append(coordInProg, self.bboxes[i])
}
}
return
}
func (self *plan) countTasks(status uint8) int {
result := 0
for _, s := range self.status {
if s == status {
result++
}
}
return result
}
func (self *plan) GetTask() *gopnik.TileCoord {
self.mu.Lock()
defer self.mu.Unlock()
oldCursor := self.cursor
allDone := true
for {
self.cursor++
if self.cursor == len(self.bboxes) {
self.cursor = 0
}
if self.status[self.cursor] == PENDING {
if self.attempts[self.cursor] < max_attemps {
bb := self.bboxes[self.cursor]
self.status[self.cursor] = INPROGRESS
self.attempts[self.cursor]++
return &bb
}
log.Warning("Failed to process %v task after %v attempts", self.bboxes[self.cursor], max_attemps)
self.status[self.cursor] = FAILED
}
if self.status[self.cursor] != DONE && self.status[self.cursor] != FAILED {
allDone = false
}
// On start point again
if self.cursor == oldCursor {
// Is plan complete?
if allDone {
return nil
}
// Waiting...
log.Debug("Waiting for %v tasks...", self.countTasks(INPROGRESS))
self.condMu.Lock()
self.mu.Unlock()
self.cond.Wait()
self.condMu.Unlock()
self.mu.Lock()
oldCursor = self.cursor
allDone = true
}
}
}
func (self *plan) setStatus(coord gopnik.TileCoord, status uint8) error {
self.mu.Lock()
defer self.mu.Unlock()
for i, c := range self.bboxes {
if coord.Equals(&c) {
self.status[i] = status
self.cond.Broadcast()
return nil
}
}
return fmt.Errorf("Can't find task %v", coord)
}
func (self *plan) FailTask(coord gopnik.TileCoord) error {
return self.setStatus(coord, PENDING)
}
func (self *plan) DoneTask(coord gopnik.TileCoord) error {
return self.setStatus(coord, DONE)
}