-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsiq.go
186 lines (155 loc) · 3.27 KB
/
siq.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package siq
import (
"fmt"
"time"
"github.com/mrasu/Siq/surface"
"github.com/mrasu/Siq/workers"
)
// Siq is distributed job queue
type Siq struct {
topics map[string]chan *surface.TopicCommand
topicConsuming map[string]bool
wo *WorkerObserver
so *SiqObserver
backupData []byte
}
func newSiq(so *SiqObserver) *Siq {
s := &Siq{
topics: map[string]chan *surface.TopicCommand{},
so: so,
}
s.wo = newWorkerObserver(s)
return s
}
func (s *Siq) Start() {
go func() {
for true {
for n, _ := range s.topics {
s.StartConsume(n)
}
time.Sleep(1 * time.Second)
}
}()
}
func (s *Siq) Register(w workers.Worker) {
s.wo.Add(&w)
}
func (s *Siq) Unregister(w workers.Worker) {
s.wo.Delete(&w)
}
func (s *Siq) Publish(tName string, m string) *Siq {
topic := s.getTopicChannel(tName)
if topic == nil {
topicSiq := s.so.GetSiqByTopic(tName)
if topicSiq == nil {
topic = s.createTopicChannel(tName)
} else {
return topicSiq
}
}
surface.AddTopic(topic, m)
s.StartConsume(tName)
return nil
}
func (s *Siq) StartConsume(tName string) {
if _, exists := s.topicConsuming[tName]; exists {
return
}
go func() {
s.consumeAllAt(tName)
delete(s.topicConsuming, tName)
fmt.Printf("consumeAllAt(%s)\n", tName)
}()
}
func (s *Siq) ConsumeTopic(tName string) string {
t, ok := s.topics[tName]
if !ok {
return ""
}
return surface.DequeTopic(t)
}
func (s *Siq) getTopicChannel(n string) chan *surface.TopicCommand {
if c, ok := s.topics[n]; ok {
return c
}
return nil
}
func (s *Siq) createTopicChannel(n string) chan *surface.TopicCommand {
c := surface.NewTopic(n)
s.topics[n] = c
err := s.so.SetSiqTopic(n, s)
if err != nil {
panic(err)
}
return c
}
func (s *Siq) Show() {
fmt.Println("Show Topic!!")
for _, t := range s.topics {
surface.ShowTopic(t)
}
}
func (s *Siq) getTopics() []string{
siq_topics := make([]string, 0, len(s.topics))
for t := range(s.topics) {
siq_topics = append(siq_topics, t)
}
return siq_topics
}
func (s *Siq) getMessages(tName string, fromId int) []surface.Message {
t, ok := s.topics[tName]
if ok == false {
return []surface.Message{}
}
return surface.GetTopicFrom(t, fromId)
}
func (s *Siq) getBackupSiq(tName string) *Siq {
return s.so.GetBackupSiqByTopic(tName)
}
func (s *Siq) updateBackup(data []byte) error {
s.backupData = append(s.backupData, data...)
fmt.Printf("receive backup data %s\n", s.backupData)
return nil
}
func (s *Siq) consumeAllAt(tName string) {
t, ok := s.topics[tName]
if !ok {
return
}
if s.wo.Count() == 0 {
return
}
currentNumber := 0
for true {
msg := surface.DequeTopic(t)
if msg == "" {
break
}
currentNumber, w := s.wo.LockIdleWorker(currentNumber)
if w == nil {
fmt.Println("***Idle Not Found****")
surface.AddTopic(t, msg)
time.Sleep(1 * time.Second)
continue
}
go func() {
defer s.wo.Unlock(w)
do, err := (*w).SendMessage(tName, msg)
if err != nil {
fmt.Printf("Worker[%d] fail: %s\n", (*w).GetId(), err)
s.wo.NotifyFail(w)
}
if !do {
time.Sleep(1 * time.Second)
do, err = (*w).SendMessage(tName, msg)
//TODO: pushFirst
if !do {
surface.AddTopic(t, msg)
fmt.Printf("do nothing: %s\n", msg)
}
}
fmt.Printf("finish [%s]\n", msg)
}()
currentNumber += 1
}
}