forked from coreos/fleet
/
unit_state.go
45 lines (37 loc) · 1.1 KB
/
unit_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
package registry
import (
"path"
"github.com/coreos/fleet/unit"
)
const (
statePrefix = "/state/"
)
// Get the current UnitState of the provided Job's Unit
func (r *EtcdRegistry) getUnitState(jobName string) *unit.UnitState {
key := path.Join(r.keyPrefix, statePrefix, jobName)
resp, err := r.etcd.Get(key, false, true)
// Assume the error was KeyNotFound and return an empty data structure
if err != nil {
return nil
}
var state unit.UnitState
//TODO: Handle the error generated by unmarshal
unmarshal(resp.Node.Value, &state)
return &state
}
// Persist the changes in a provided Machine's Job
func (r *EtcdRegistry) SaveUnitState(jobName string, unitState *unit.UnitState) {
key := path.Join(r.keyPrefix, statePrefix, jobName)
//TODO: Handle the error generated by marshal
json, _ := marshal(unitState)
r.etcd.Set(key, json, 0)
}
// Delete the state from the Registry for the given Job's Unit
func (r *EtcdRegistry) RemoveUnitState(jobName string) error {
key := path.Join(r.keyPrefix, statePrefix, jobName)
_, err := r.etcd.Delete(key, false)
if isKeyNotFound(err) {
err = nil
}
return err
}