-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstatus.go
108 lines (95 loc) · 2.44 KB
/
status.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
package gojob
import (
"sync"
"sync/atomic"
"time"
)
// Status represents the status of the job.
type Status struct {
Timestamp string `json:"timestamp"`
NumFailed int64 `json:"num_failed"`
NumSucceed int64 `json:"num_succeed"`
NumFinished int64 `json:"num_done"`
NumTotal int64 `json:"num_total"`
}
type statusManager struct {
numFailed atomic.Int64
numSucceed atomic.Int64
numTotal atomic.Int64
mutex sync.Mutex
ticker *time.Ticker
statusChans []chan Status
}
func newStatusManager() *statusManager {
return &statusManager{
numFailed: atomic.Int64{},
numSucceed: atomic.Int64{},
numTotal: atomic.Int64{},
mutex: sync.Mutex{},
ticker: time.NewTicker(5 * time.Second),
statusChans: []chan Status{},
}
}
func (sm *statusManager) notify() {
status := sm.Snapshot()
sm.mutex.Lock()
for _, ch := range sm.statusChans {
ch <- status
}
sm.mutex.Unlock()
}
// Start starts the status manager.
// It will notify all the status channels every second.
func (sm *statusManager) Start() {
sm.notify()
for range sm.ticker.C {
sm.notify()
}
}
// Stop stops the status manager.
func (sm *statusManager) Stop() {
sm.notify()
sm.notify()
sm.ticker.Stop()
for _, ch := range sm.statusChans {
close(ch)
}
}
// IncFailed increments the number of failed jobs.
func (sm *statusManager) IncFailed() {
sm.numFailed.Add(1)
}
// IncSucceed increments the number of succeed jobs.
func (sm *statusManager) IncSucceed() {
sm.numSucceed.Add(1)
}
// SetTotal sets the total number of jobs.
// It should be called before the job starts.
func (sm *statusManager) SetTotal(total int64) {
sm.numTotal.Store(total)
}
// StatusChan returns a channel that will receive the status of the job.
// The status will be sent every second. It should be called before the job starts.
// You can call it multiple times to get multiple channels.
func (sm *statusManager) StatusChan() <-chan Status {
ch := make(chan Status)
sm.mutex.Lock()
sm.statusChans = append(sm.statusChans, ch)
sm.mutex.Unlock()
return ch
}
// Snapshot returns the current status of the job.
func (sm *statusManager) Snapshot() Status {
sm.mutex.Lock()
numFailed := sm.numFailed.Load()
numSucceed := sm.numSucceed.Load()
numTotal := sm.numTotal.Load()
sm.mutex.Unlock()
return Status{
Timestamp: time.Now().Format(time.RFC3339),
NumFailed: numFailed,
NumSucceed: numSucceed,
NumFinished: numFailed + numSucceed,
NumTotal: numTotal,
}
}