~juju/juju-core/1.10

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
package state

import (
	"labix.org/v2/mgo"
)

// Life represents the lifecycle state of the entities
// Relation, Unit, Service and Machine.
type Life int8

const (
	Alive Life = iota
	Dying
	Dead
	nLife
)

var lifeStrings = [nLife]string{
	Alive: "alive",
	Dying: "dying",
	Dead:  "dead",
}

func (l Life) String() string {
	return lifeStrings[l]
}

var isAliveDoc = D{{"life", Alive}}
var isDeadDoc = D{{"life", Dead}}
var notDeadDoc = D{{"life", D{{"$ne", Dead}}}}

// Living describes state entities with a lifecycle.
type Living interface {
	Life() Life
	Destroy() error
	EnsureDead() error
	Refresh() error
}

func isAlive(coll *mgo.Collection, id interface{}) (bool, error) {
	n, err := coll.Find(D{{"_id", id}, {"life", Alive}}).Count()
	return n == 1, err
}

func isNotDead(coll *mgo.Collection, id interface{}) (bool, error) {
	n, err := coll.Find(D{{"_id", id}, {"life", D{{"$ne", Dead}}}}).Count()
	return n == 1, err
}