/
life.go
123 lines (103 loc) · 2.85 KB
/
life.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// Copyright 2012, 2013 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package state
import (
"github.com/juju/errors"
"github.com/juju/mgo/v3/bson"
"github.com/juju/juju/core/life"
"github.com/juju/juju/mongo"
)
// Life represents the lifecycle state of the entities
// Relation, Unit, Application and Machine.
type Life int8
const (
Alive Life = iota
Dying
Dead
)
// String is deprecated, use Value.
func (l Life) String() string {
switch l {
case Alive:
return "alive"
case Dying:
return "dying"
case Dead:
return "dead"
default:
return "unknown"
}
}
// Value returns the core.life.Value type.
func (l Life) Value() life.Value {
switch l {
case Alive:
return life.Alive
case Dying:
return life.Dying
case Dead:
return life.Dead
default:
return life.Value("unknown")
}
}
// LifeFromValue converts a life.Value into it's corresponding
// state.Life
func LifeFromValue(value life.Value) Life {
return valueMap[value]
}
var (
isAliveDoc = bson.D{{"life", Alive}}
isDyingDoc = bson.D{{"life", Dying}}
isDeadDoc = bson.D{{"life", Dead}}
notDeadDoc = bson.D{{"life", bson.D{{"$ne", Dead}}}}
errDeadOrGone = errors.New("neither alive nor dying")
errAlreadyDying = errors.New("already dying")
errAlreadyRemoved = errors.New("already removed")
)
var valueMap = map[life.Value]Life{
life.Alive: Alive,
life.Dying: Dying,
life.Dead: Dead,
}
// Living describes state entities with a lifecycle.
type Living interface {
Life() Life
Destroy() error
Refresh() error
}
// AgentLiving describes state entities with a lifecycle and an agent that
// manages it.
type AgentLiving interface {
Living
EnsureDead() error
Remove() error
}
func isAlive(mb modelBackend, collName string, id interface{}) (bool, error) {
coll, closer := mb.db().GetCollection(collName)
defer closer()
return isAliveWithSession(coll, id)
}
func isAliveWithSession(coll mongo.Collection, id interface{}) (bool, error) {
return checkLifeWithSession(coll, id, bson.DocElem{"life", Alive})
}
func isNotDead(mb modelBackend, collName string, id interface{}) (bool, error) {
coll, closer := mb.db().GetCollection(collName)
defer closer()
return isNotDeadWithSession(coll, id)
}
func isNotDeadWithSession(coll mongo.Collection, id interface{}) (bool, error) {
return checkLifeWithSession(coll, id, bson.DocElem{"life", bson.D{{"$ne", Dead}}})
}
func isDying(mb modelBackend, collName string, id interface{}) (bool, error) {
coll, closer := mb.db().GetCollection(collName)
defer closer()
return isDyingWithSession(coll, id)
}
func isDyingWithSession(coll mongo.Collection, id interface{}) (bool, error) {
return checkLifeWithSession(coll, id, bson.DocElem{"life", Dying})
}
func checkLifeWithSession(coll mongo.Collection, id interface{}, sel bson.DocElem) (bool, error) {
n, err := coll.Find(bson.D{{"_id", id}, sel}).Count()
return n == 1, err
}