-
Notifications
You must be signed in to change notification settings - Fork 122
/
execution_manager.go
66 lines (57 loc) · 1.44 KB
/
execution_manager.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
package ib
import (
"fmt"
)
// ExecutionManager fetches execution reports from the past 24 hours.
type ExecutionManager struct {
AbstractManager
id int64
filter ExecutionFilter
values []ExecutionData
}
// NewExecutionManager .
func NewExecutionManager(e *Engine, filter ExecutionFilter) (*ExecutionManager, error) {
am, err := NewAbstractManager(e)
if err != nil {
return nil, err
}
em := &ExecutionManager{AbstractManager: *am,
id: UnmatchedReplyID,
filter: filter,
}
go em.startMainLoop(em.preLoop, em.receive, em.preDestroy)
return em, nil
}
func (e *ExecutionManager) preLoop() error {
e.id = e.eng.NextRequestID()
e.eng.Subscribe(e.rc, e.id)
req := &RequestExecutions{Filter: e.filter}
req.SetID(e.id)
return e.eng.Send(req)
}
func (e *ExecutionManager) receive(r Reply) (UpdateStatus, error) {
switch r.(type) {
case *ErrorMessage:
r := r.(*ErrorMessage)
if r.SeverityWarning() {
return UpdateFalse, nil
}
return UpdateFalse, r.Error()
case *ExecutionData:
t := r.(*ExecutionData)
e.values = append(e.values, *t)
return UpdateFalse, nil
case *ExecutionDataEnd:
return UpdateFinish, nil
}
return UpdateFalse, fmt.Errorf("Unexpected type %v", r)
}
func (e *ExecutionManager) preDestroy() {
e.eng.Unsubscribe(e.rc, e.id)
}
// Values returns the most recent snapshot of execution information.
func (e *ExecutionManager) Values() []ExecutionData {
e.rwm.RLock()
defer e.rwm.RUnlock()
return e.values
}