-
Notifications
You must be signed in to change notification settings - Fork 1
/
queue.go
74 lines (63 loc) · 1.94 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
package none
import (
"errors"
"fmt"
"time"
"github.com/ScoreTrak/ScoreTrak/pkg/queue/queueing"
"github.com/ScoreTrak/ScoreTrak/pkg/servicegroup"
)
type None struct{}
var ErrUnknownPanic = errors.New("unknown panic")
var ErrPanic = errors.New("panic")
func (n None) Send(sds []*queueing.ScoringData) ([]*queueing.QCheck, error, error) {
ret := make([]*queueing.QCheck, len(sds))
cq := make(chan queueing.IndexedQueue, 1)
for i, sd := range sds {
go func(sd *queueing.ScoringData, i int) {
defer func() {
if x := recover(); x != nil {
var err error
switch x := x.(type) {
case string:
err = fmt.Errorf("%w: %s", ErrPanic, x)
case error:
err = x
default:
err = ErrUnknownPanic
}
cq <- queueing.IndexedQueue{Q: &queueing.QCheck{Service: sd.Service, Passed: false, Log: "Encountered an unexpected error during the check.", Err: err.Error(), RoundID: sd.RoundID}, I: i}
return
}
}()
qc := queueing.CommonExecute(sd, sd.Deadline.Add(-2*time.Second))
cq <- queueing.IndexedQueue{Q: &qc, I: i}
}(sd, i)
}
counter := len(sds)
for {
select {
case res := <-cq:
ret[res.I] = res.Q
counter--
if counter == 0 {
return ret, nil, nil
}
case <-time.After(time.Until(sds[0].Deadline)):
return nil, nil, ErrRoundTookTooLongToScore
}
}
}
var ErrRoundTookTooLongToScore = errors.New("round took too long to score. this might be due to many reasons like a worker going down, or the number of rounds being too big for one master")
var ErrMethodNotSupportedForNoneQueue = errors.New("method not supported when queue is none")
func (n None) Receive() {
panic(ErrMethodNotSupportedForNoneQueue)
}
func (n None) Acknowledge(q queueing.QCheck) {
panic(ErrMethodNotSupportedForNoneQueue)
}
func (n None) Ping(group *servicegroup.ServiceGroup) error {
return ErrMethodNotSupportedForNoneQueue
}
func NewNoneQueue() (*None, error) {
return &None{}, nil // Use global Variable
}