-
Notifications
You must be signed in to change notification settings - Fork 0
/
counter.go
49 lines (41 loc) · 1.12 KB
/
counter.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
package middleware
import (
"context"
"sync/atomic"
"github.com/hmoragrega/workers"
)
// Counter count how many jobs have started and finished.
// Keep in mind that it's not protected against overflows.
type Counter struct {
started uint64
finished uint64
failed uint64
}
// Wrap wraps the job adding counters.
func (c *Counter) Wrap(next workers.Job) workers.Job {
return workers.JobFunc(func(ctx context.Context) error {
atomic.AddUint64(&c.started, 1)
err := next.Do(ctx)
atomic.AddUint64(&c.finished, 1)
if err != nil {
atomic.AddUint64(&c.failed, 1)
}
return err
})
}
// Started returns the number of jobs that have been started.
func (c *Counter) Started() uint64 {
return atomic.LoadUint64(&c.started)
}
// Finished returns the number of jobs that have been finished.
func (c *Counter) Finished() uint64 {
return atomic.LoadUint64(&c.finished)
}
// Running returns the number of failed jobs.
func (c *Counter) Failed() uint64 {
return atomic.LoadUint64(&c.failed)
}
// Running returns the number of jobs that are running now.
func (c *Counter) Running() uint64 {
return c.Started() - c.Finished()
}