This repository has been archived by the owner on Sep 13, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
backend.go
96 lines (77 loc) · 2.15 KB
/
backend.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
package mock
import "errors"
// Struct Backend is a mock backend for scheduling and starting Actors.
type Backend struct {
actors []*Actor
name string
}
// Struct BackendInfo is the serializable version of a Backend.
type BackendInfo struct {
Name string `json:"name"`
ActorCount int `json:"count"`
}
// Info returns the serializable information about a mock Backend.
func (b *Backend) Info() (i *BackendInfo) {
i = &BackendInfo{
Name: "Mock backend",
ActorCount: len(b.actors),
}
return
}
// Deploy introduces a new Actor to the Backend if the Actor is unique
// to the Backend.
func (b *Backend) Deploy(a *Actor) error {
for _, actor := range b.actors {
if a.Name() == actor.Name() {
return errors.New("Duplicate actor")
}
}
b.actors = append(b.actors, a)
a.Start()
return nil
}
// ListDeploys returns a list of the Actors in the Backend.
func (b *Backend) ListDeploys(pattern string) (res []*Actor, err error) {
return b.actors, nil
}
// GetActorer looks for an Actor by name pattern and returns it or
// an error describing the failure.
func (b *Backend) GetActorer(pattern string) (*Actor, error) {
for _, actor := range b.actors {
if actor.Name() == pattern {
return actor, nil
}
}
return nil, errors.New("No such Actor by name " + pattern)
}
// Stop asks the Actor to stop and returns its output.
func (b *Backend) Stop(a *Actor) error {
return a.Stop()
}
// Start asks the Actor to start and returns its output.
func (b *Backend) Start(a *Actor) error {
return a.Start()
}
// Restart asks the Actor to restart and returns its output.
func (b *Backend) Restart(a *Actor) error {
return a.Restart()
}
// Destroy destroys the Actor from the list of active Actors and
// returns an error if the given actor doesn't exist.
func (b *Backend) Destroy(a *Actor) error {
for i, actor := range b.actors {
if actor.Name() == a.Name() {
temp := b.actors[:i]
temp2 := b.actors[i+1:]
b.actors = nil
for _, myactor := range temp {
b.actors = append(b.actors, myactor)
}
for _, myactor := range temp2 {
b.actors = append(b.actors, myactor)
}
return nil
}
}
return errors.New("No such actor loaded")
}