forked from juju/juju
/
state.go
64 lines (52 loc) · 1.59 KB
/
state.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
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package meterstatus
import (
"os"
"time"
"github.com/juju/errors"
"github.com/juju/utils"
)
// StateFile holds the meter status on disk.
type StateFile struct {
path string
}
// NewStateFile creates a new file for persistent storage of
// the meter status.
func NewStateFile(path string) *StateFile {
return &StateFile{path: path}
}
type state struct {
Code string `yaml:"status-code"`
Info string `yaml:"status-info"`
Disconnected *Disconnected `yaml:"disconnected,omitempty"`
}
// Disconnected stores the information relevant to the inactive meter status worker.
type Disconnected struct {
Disconnected int64 `yaml:"disconnected-at,omitempty"`
State WorkerState `yaml:"disconnected-state,omitempty"`
}
// When returns the time when the unit was disconnected.
func (d Disconnected) When() time.Time {
return time.Unix(d.Disconnected, 0)
}
// Read reads the current meter status information from disk.
func (f *StateFile) Read() (string, string, *Disconnected, error) {
var st state
if err := utils.ReadYaml(f.path, &st); err != nil {
if os.IsNotExist(err) {
return "", "", nil, nil
}
return "", "", nil, errors.Trace(err)
}
return st.Code, st.Info, st.Disconnected, nil
}
// Write stores the supplied status information to disk.
func (f *StateFile) Write(code, info string, disconnected *Disconnected) error {
st := state{
Code: code,
Info: info,
Disconnected: disconnected,
}
return errors.Trace(utils.WriteYaml(f.path, st))
}