-
Notifications
You must be signed in to change notification settings - Fork 43
/
queue.go
136 lines (107 loc) · 2.72 KB
/
queue.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
package sync
import (
"container/list"
"encoding/json"
"fmt"
)
const QueueStoreKey = "TextileSyncTaskQueue"
type marshalledQueue struct {
QueueAsSlice []Task `json:"queueAsSlice"`
FileQueueAsSlice []Task `json:"fileQueueAsSlice"`
}
func (s *synchronizer) enqueueTask(task *Task, queue *list.List) {
if s.isTaskEnqueued(task) == false {
queue.PushBack(task)
s.queueHashMap[task.ID] = task
}
}
func (s *synchronizer) dequeueTask(queue *list.List) *Task {
queueItem := queue.Front()
s.taskQueue.Remove(queueItem)
task := queueItem.Value.(*Task)
delete(s.queueHashMap, task.ID)
return task
}
func (s *synchronizer) storeQueue() error {
// Store main queue
queueAsSlice := []Task{}
currEl := s.taskQueue.Front()
for currEl != nil {
queueAsSlice = append(queueAsSlice, *currEl.Value.(*Task))
currEl = currEl.Next()
}
// Store file pinning queue
fileQueueAsSlice := []Task{}
currEl = s.filePinningQueue.Front()
for currEl != nil {
fileQueueAsSlice = append(fileQueueAsSlice, *currEl.Value.(*Task))
currEl = currEl.Next()
}
objToMarshal := &marshalledQueue{
QueueAsSlice: queueAsSlice,
FileQueueAsSlice: fileQueueAsSlice,
}
marshalled, err := json.Marshal(objToMarshal)
if err != nil {
return err
}
err = s.st.Set([]byte(QueueStoreKey), marshalled)
if err != nil {
return err
}
return nil
}
func (s *synchronizer) restoreQueue() error {
queueMutex1 := s.queueMutexMap[s.taskQueue]
queueMutex2 := s.queueMutexMap[s.filePinningQueue]
queueMutex1.Lock()
queueMutex2.Lock()
defer queueMutex1.Unlock()
defer queueMutex2.Unlock()
data, err := s.st.Get([]byte(QueueStoreKey))
if err != nil {
return err
}
queue := &marshalledQueue{}
err = json.Unmarshal(data, queue)
if err != nil {
return err
}
for _, el := range queue.QueueAsSlice {
s.enqueueTask(&el, s.taskQueue)
}
for _, el := range queue.FileQueueAsSlice {
s.enqueueTask(&el, s.filePinningQueue)
}
return nil
}
func (s *synchronizer) isTaskEnqueued(task *Task) bool {
existingTask := s.queueHashMap[task.ID]
if existingTask == nil {
return false
}
isPending := existingTask.State == taskQueued || existingTask.State == taskPending
if isPending {
return true
}
return false
}
func (s *synchronizer) queueString(queue *list.List) string {
queueName := "buckets"
if queue == s.filePinningQueue {
queueName = "file pinning"
}
failed, queued, pending := 0, 0, 0
for curr := queue.Front(); curr != nil; curr = curr.Next() {
task := curr.Value.(*Task)
switch task.State {
case taskPending:
pending++
case taskFailed:
failed++
case taskQueued:
queued++
}
}
return fmt.Sprintf("Textile sync [%s]: Total: %d, Queued: %d, Pending: %d, Failed: %d", queueName, queue.Len(), queued, pending, failed)
}