/
status.go
66 lines (61 loc) · 1.56 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
package sweeper
import (
"github.com/skynetlabs/pinner/lib"
"github.com/skynetlabs/pinner/logger"
"sync"
"time"
)
type (
// Status represents the status of a sweep.
// All times are UTC-based in order to simplify handling and comparison.
Status struct {
InProgress bool
Error error
StartTime time.Time
EndTime time.Time
}
// status is the internal status type that allows thread-safe updates.
status struct {
mu sync.Mutex
staticLogger logger.Logger
status Status
}
)
// Start marks the start of a new process, unless one is already in progress.
// If there is a process in progress then Start returns without any action.
func (st *status) Start() {
st.mu.Lock()
// Double-check for parallel sweeps.
if st.status.InProgress {
st.mu.Unlock()
st.staticLogger.Debug("Attempted to start a sweep while another one was already ongoing.")
return
}
// Initialise the status to "a sweep is running".
st.status.InProgress = true
st.status.Error = nil
st.status.StartTime = lib.Now()
st.status.EndTime = time.Time{}
st.mu.Unlock()
st.staticLogger.Info("Started a sweep.")
}
// Status returns a copy of the current status.
func (st *status) Status() Status {
st.mu.Lock()
s := st.status
st.mu.Unlock()
return s
}
// Finalize marks a run as completed with the given error.
func (st *status) Finalize(err error) {
st.mu.Lock()
if !st.status.InProgress {
st.mu.Unlock()
return
}
st.status.InProgress = false
st.status.EndTime = lib.Now()
st.status.Error = err
st.mu.Unlock()
st.staticLogger.Info("Finalized a sweep.")
}