-
Notifications
You must be signed in to change notification settings - Fork 50
/
manager.go
180 lines (146 loc) · 3.9 KB
/
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
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
package status
import (
"context"
"sort"
"sync"
"github.com/justtrackio/gosoline/pkg/coffin"
"github.com/justtrackio/gosoline/pkg/log"
)
//go:generate mockery --name Manager
type Manager interface {
// initialize a new work item (e.g. working through the files of a day) with the given number of steps (e.g. days) to do
StartWork(key string, steps int) WorkItem
// print the report to the logger
PrintReport(logger log.Logger)
// monitor execution of a method and set the correct result status upon return
Monitor(key string, f func() error) func() error
// like Monitor, but pass along a context
MonitorWithContext(key string, f func(ctx context.Context) error) func(ctx context.Context) error
}
//go:generate mockery --name WorkItem
type WorkItem interface {
// monitor execution of a method and set the correct result status upon return
Monitor(f func() error) func() error
// update the progress of a work item with the current step and the progress (in %) of that step (e.g. how many files of that day are done?)
ReportProgress(step int, progress float64)
// utility to report 100% progress on all steps
ReportDone()
// report that a work item failed and the reason for that
ReportError(err error)
}
type manager struct {
lck sync.Mutex
work map[string]*workItem
}
type workItemHandle struct {
key string
manager *manager
}
type workItem struct {
step int
totalSteps int
progress float64
err error
}
var managerContainer = struct {
sync.Mutex
instance Manager
}{}
func ProvideManager() Manager {
managerContainer.Lock()
defer managerContainer.Unlock()
if managerContainer.instance != nil {
return managerContainer.instance
}
managerContainer.instance = NewManager()
return managerContainer.instance
}
func NewManager() Manager {
return &manager{
lck: sync.Mutex{},
work: make(map[string]*workItem),
}
}
func (m *manager) StartWork(key string, steps int) WorkItem {
m.lck.Lock()
defer m.lck.Unlock()
m.work[key] = &workItem{
step: 0,
totalSteps: steps,
progress: 0,
err: nil,
}
return &workItemHandle{
key: key,
manager: m,
}
}
func (h *workItemHandle) ReportProgress(step int, progress float64) {
h.manager.lck.Lock()
defer h.manager.lck.Unlock()
if item, ok := h.manager.work[h.key]; ok {
item.step = step
item.progress = progress
}
}
func (h *workItemHandle) ReportDone() {
h.manager.lck.Lock()
defer h.manager.lck.Unlock()
if item, ok := h.manager.work[h.key]; ok {
item.step = item.totalSteps
item.progress = 100
}
}
func (h *workItemHandle) ReportError(err error) {
h.manager.lck.Lock()
defer h.manager.lck.Unlock()
if item, ok := h.manager.work[h.key]; ok {
item.err = err
}
}
func (h *workItemHandle) Monitor(f func() error) func() error {
return func() (err error) {
defer func() {
panicErr := coffin.ResolveRecovery(recover())
if panicErr != nil {
err = panicErr
}
if err != nil {
h.ReportError(err)
} else {
h.ReportDone()
}
}()
return f()
}
}
func (m *manager) PrintReport(logger log.Logger) {
m.lck.Lock()
defer m.lck.Unlock()
keys := make([]string, 0, len(m.work))
for key := range m.work {
keys = append(keys, key)
}
sort.Strings(keys)
for _, key := range keys {
work := m.work[key]
if work.err != nil {
logger.Info("Work item %s: failed with error %s", key, work.err.Error())
} else if work.step < work.totalSteps || work.progress < 100 {
logger.Info("Work item %s: step %d / %d (%.2f %%)", key, work.step, work.totalSteps, work.progress)
} else {
logger.Info("Work item %s: done", key)
}
}
}
func (m *manager) Monitor(key string, f func() error) func() error {
return m.StartWork(key, 1).Monitor(f)
}
func (m *manager) MonitorWithContext(key string, f func(ctx context.Context) error) func(ctx context.Context) error {
h := m.StartWork(key, 1)
return func(ctx context.Context) error {
return h.Monitor(func() error {
return f(ctx)
})()
}
}