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
/
actor.go
110 lines (90 loc) · 2.24 KB
/
actor.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
package mock
import (
"fmt"
"time"
"github.com/Xe/flitter/lib/deployment"
)
// Struct Actor represents a single worker running a given
// image with given tags.
type Actor struct {
name string
tags []string
image string
status deployment.Status
}
// Struct ActorInfo is the serializable form of Actor.
type ActorInfo struct {
Name string `json:"name"`
Tags []string `json:"tags"`
Image string `json:"image"`
Status deployment.Status `json:"status"`
}
// Image returns the Actor's image as a string.
func (a *Actor) Image() string {
return a.image
}
// Name returns the Actor's name as a string.
func (a *Actor) Name() string {
return a.name
}
// Tags returns the Actor's metadata tags as a string slice.
func (a *Actor) Tags() []string {
return a.tags
}
// Status returns the Actor's status as a deployment.Status.
func (a *Actor) Status() deployment.Status {
return a.status
}
// Info returns the serializable form of the Actor.
func (a *Actor) Info() *ActorInfo {
return &ActorInfo{
Name: a.name,
Status: a.status,
Tags: a.tags,
Image: a.image,
}
}
// Start commands the Actor to start, returning an error
// if the start fails.
func (a *Actor) Start() error {
a.status = deployment.STATUS_LOADING
go func() {
time.Sleep(3 * time.Second)
a.status = deployment.STATUS_RUNNING
}()
return nil
}
// Stop commands the Actor to stop, returning an error if
// the stop fails.
func (a *Actor) Stop() error {
go func() {
time.Sleep(3 * time.Second)
a.status = deployment.STATUS_STOPPED
}()
return nil
}
// Restart calls Stop and then Start and returns an error if
// either fails, bailing out on the first failure.
func (a *Actor) Restart() error {
err := a.Stop()
if err != nil {
return err
}
// Wait for the actor to stop
for a.status != deployment.STATUS_STOPPED {
time.Sleep(1 * time.Second)
}
err = a.Start()
if err != nil {
return err
}
for a.status != deployment.STATUS_RUNNING && a.status == deployment.STATUS_LOADING {
time.Sleep(1 * time.Second)
}
return err
}
// String returns a representation of the Actor as a simple string.
func (a *Actor) String() string {
return fmt.Sprintf("%s running %s with tags %s and status %d",
a.name, a.image, a.tags, a.status)
}