-
Notifications
You must be signed in to change notification settings - Fork 11
/
service.go
109 lines (96 loc) · 2.19 KB
/
service.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
package main
import (
"fmt"
"log"
"os"
"os/exec"
"sync"
"time"
)
type runState uint8
// Types of valid runState
const (
notStarted = runState(iota)
starting
started
stopped
errored
)
func (rs runState) String() string {
switch rs {
case notStarted:
return "not started"
case starting:
return "being started"
case started:
return "already started"
case stopped:
return "stopped"
case errored:
return "errored"
default:
return "in an invalid state"
}
}
type Service struct {
Name ServiceName
Startup Command
Shutdown Command
Provides []ServiceType
Needs []ServiceType
state runState
}
func StartServices(services map[ServiceType][]*Service) {
wg := sync.WaitGroup{}
startedTypes := make(map[ServiceType]bool)
for _, services := range services {
wg.Add(len(services))
for _, s := range services {
go func(s *Service) {
// TODO: This should ensure that Needs are satisfiable instead of getting into an
// infinite loop when they're not.
// (TODO(2): Prove N=NP in order to do the above efficiently.)
for satisfied, tries := false, 0; satisfied == false && tries < 60; tries++ {
satisfied = s.NeedsSatisfied(startedTypes)
time.Sleep(2 * time.Second)
}
if s.state == notStarted {
if err := s.Start(); err != nil {
log.Println(err)
}
}
// We don't use a mutex to handle startedTypes, because things only ever get set from false
// to true, so if there's a race it's a race to set the variable to the same thing.
for _, t := range s.Provides {
startedTypes[t] = true
}
wg.Done()
}(s)
}
}
wg.Wait()
}
// Starts the Service s.
func (s *Service) Start() error {
if s.state != notStarted {
return fmt.Errorf("Service %v is %v", s.Name, s.state.String())
}
s.state = starting
cmd := exec.Command("sh", "-c", s.Startup.String())
cmd.Stderr = os.Stderr
if err := cmd.Run(); err != nil {
s.state = errored
return err
}
s.state = started
return nil
}
// Checks if all of s's needs are satified by the passed list of provided types
func (s Service) NeedsSatisfied(started map[ServiceType]bool) bool {
for _, st := range s.Needs {
if !started[st] {
return false
}
}
return true
}