Skip to content

Commit 1b32c98

Browse files
committed
Prepare 1.5.0
2 parents d465b5d + 5d6c63d commit 1b32c98

File tree

3 files changed

+28
-14
lines changed

3 files changed

+28
-14
lines changed

api/server.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ import (
55
"net/http"
66
)
77

8-
var mux = http.NewServeMux()
9-
108
// API defines socket on which we listen for commands
119
type API struct {
1210
HTTP *http.Server
@@ -38,14 +36,18 @@ func (a *API) RegisterHandle(e string, h func(w http.ResponseWriter, r *http.Req
3836
pattern := "/" + e;
3937

4038
if _, exist := a.registeredHandles[pattern]; !exist {
41-
mux.HandleFunc(pattern, h)
4239
a.registeredHandles[pattern] = h
4340
}
4441
}
4542

4643
// Start api server
4744
func (a *API) Start() {
4845
if !IsAPIRunning() {
46+
mux := http.NewServeMux()
47+
for pattern, handler := range a.registeredHandles {
48+
mux.HandleFunc(pattern, handler)
49+
}
50+
4951
a.HTTP.Handler = mux
5052
if err := a.HTTP.ListenAndServe(); err == http.ErrServerClosed {
5153
a.HTTP = &http.Server{

daemon/main.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"path/filepath"
88
"strconv"
99
"strings"
10+
"sync"
1011

1112
"github.com/getsentry/raven-go"
1213
"github.com/kilgaloon/leprechaun/api"
@@ -28,6 +29,7 @@ type Daemon struct {
2829
Debug bool
2930
API *api.API
3031
shutdownChan chan bool
32+
*sync.RWMutex
3133
}
3234

3335
// GetPID gets current PID of client
@@ -57,6 +59,9 @@ func (d *Daemon) GetPidPath() string {
5759

5860
// AddService push agent as a service to list of services
5961
func (d *Daemon) AddService(s Service) {
62+
d.Lock()
63+
defer d.Unlock()
64+
6065
name := s.GetName()
6166
cfg := d.Configs.New(name, d.GetConfigPath())
6267
a := s.New(name, cfg, d.Debug)
@@ -66,8 +71,18 @@ func (d *Daemon) AddService(s Service) {
6671
d.services[name] = a
6772
}
6873

74+
// SetCommand sets command for daemon to be executed
75+
func (d *Daemon) SetCommand(cmd api.Cmd) {
76+
d.Lock()
77+
d.Cmd = cmd
78+
d.Unlock()
79+
}
80+
6981
// Run starts daemon and long living process
7082
func (d *Daemon) Run(cb func()) {
83+
d.Lock()
84+
defer d.Unlock()
85+
7186
if api.IsAPIRunning() {
7287
// more commands can/will be used here
7388
if d.Cmd.Agent() == "daemon" {
@@ -205,6 +220,7 @@ func Init() *Daemon {
205220
d.Cmd = api.Cmd(*cmd)
206221
d.API = api.New()
207222
d.shutdownChan = make(chan bool, 1)
223+
d.RWMutex = new(sync.RWMutex)
208224

209225
cfg := d.Configs.New("daemon", d.ConfigPath)
210226
if cfg.GetErrorReporting() && os.Getenv("RUN_MODE") != "test" {

daemon/main_test.go

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,16 @@ func TestMain(t *testing.T) {
6767

6868
func TestAddService(t *testing.T) {
6969
d := Init()
70-
7170
d.Cmd = "run fake_service"
7271
d.AddService(&fakeService{})
7372

7473
go d.Run(nil)
7574
}
7675

7776
func TestRun(t *testing.T) {
78-
p := Init()
79-
go p.Run(func() {
80-
if p.GetPID() != os.Getpid() {
77+
d := Init()
78+
go d.Run(func() {
79+
if d.GetPID() != os.Getpid() {
8180
t.Fatal("PID NOT MATCHED")
8281
}
8382

@@ -88,7 +87,7 @@ func TestRun(t *testing.T) {
8887

8988
rr := httptest.NewRecorder()
9089

91-
p.daemonInfo(rr, req)
90+
d.daemonInfo(rr, req)
9291

9392
for i := 0; i < 5; i++ {
9493
_, err := http.Get("http://localhost:11401")
@@ -98,15 +97,14 @@ func TestRun(t *testing.T) {
9897
continue
9998
}
10099

101-
p.GetInfo()
102-
p.renderInfo()
100+
d.GetInfo()
101+
d.renderInfo()
103102
}
104103
})
105104
}
106105

107106
func TestRunningDaemonInfo(t *testing.T) {
108107
d := Init()
109-
110108
d.Cmd = "daemon info"
111109
d.Run(func() {
112110
for i := 0; i < 5; i++ {
@@ -125,7 +123,6 @@ func TestRunningDaemonInfo(t *testing.T) {
125123

126124
func TestRunningDaemonServices(t *testing.T) {
127125
d := Init()
128-
129126
d.Cmd = "daemon services"
130127
d.Run(func() {
131128
for i := 0; i < 5; i++ {
@@ -145,7 +142,6 @@ func TestRunningDaemonServices(t *testing.T) {
145142

146143
func TestRunningDaemonKill(t *testing.T) {
147144
d := Init()
148-
149145
d.Cmd = "daemon kill"
150146
d.Run(func() {
151147
for i := 0; i < 5; i++ {
@@ -169,7 +165,7 @@ func TestAPIRunning(t *testing.T) {
169165
d := Init()
170166

171167
d.Cmd = ""
172-
d.Run(nil)
168+
go d.Run(nil)
173169

174170
d = Init()
175171

0 commit comments

Comments
 (0)