Skip to content

Commit 6fddbd5

Browse files
committed
Add new service.DRAINING status
1 parent ca2f4a0 commit 6fddbd5

File tree

5 files changed

+47
-12
lines changed

5 files changed

+47
-12
lines changed

healthy/healthy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ import (
1111
"time"
1212

1313
"github.com/Nitro/sidecar/service"
14-
log "github.com/sirupsen/logrus"
1514
"github.com/relistan/go-director"
15+
log "github.com/sirupsen/logrus"
1616
)
1717

1818
const (

receiver/http_test.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func Test_updateHandler(t *testing.T) {
105105
So(received, ShouldBeTrue)
106106
})
107107

108-
Convey("enqueus all updates if no Subscriptions are provided", func() {
108+
Convey("enqueues all updates if no Subscriptions are provided", func() {
109109
evtState := deepcopy.Copy(state).(*catalog.ServicesState)
110110
evtState.LastChanged = time.Now().UTC()
111111

@@ -223,5 +223,33 @@ func Test_updateHandler(t *testing.T) {
223223
So(lastReceivedState.LastChanged.Before(state.LastChanged), ShouldBeTrue)
224224
So(len(lastReceivedState.Servers["chaucer"].Services), ShouldEqual, 2)
225225
})
226+
227+
Convey("enqueues an update to mark a service as DRAINING", func() {
228+
evtState := deepcopy.Copy(state).(*catalog.ServicesState)
229+
evtState.LastChanged = time.Now().UTC()
230+
231+
change := catalog.StateChangedEvent{
232+
State: evtState,
233+
ChangeEvent: catalog.ChangeEvent{
234+
Service: service.Service{
235+
Name: "nobody-wants-this",
236+
ID: "10101010101",
237+
Updated: time.Now().UTC(),
238+
Created: time.Now().UTC(),
239+
Status: service.DRAINING,
240+
},
241+
PreviousStatus: service.ALIVE,
242+
},
243+
}
244+
245+
encoded, _ := json.Marshal(change)
246+
req := httptest.NewRequest("POST", "/update", bytes.NewBuffer(encoded))
247+
248+
UpdateHandler(recorder, req, rcvr)
249+
resp := recorder.Result()
250+
251+
So(resp.StatusCode, ShouldEqual, 200)
252+
So(len(rcvr.ReloadChan), ShouldEqual, 1)
253+
})
226254
})
227255
}

receiver/receiver.go

+2
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ func ShouldNotify(oldStatus int, newStatus int) bool {
5757
if oldStatus == service.ALIVE {
5858
return true
5959
}
60+
case service.DRAINING:
61+
return true
6062
default:
6163
log.Errorf("Got unknown service change status: %d", newStatus)
6264
return false

service/service.go

+3
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const (
1919
TOMBSTONE = iota
2020
UNHEALTHY = iota
2121
UNKNOWN = iota
22+
DRAINING = iota
2223
)
2324

2425
type Port struct {
@@ -162,6 +163,8 @@ func StatusString(status int) string {
162163
return "Unhealthy"
163164
case UNKNOWN:
164165
return "Unknown"
166+
case DRAINING:
167+
return "Draining"
165168
default:
166169
return "Tombstone"
167170
}

ui/app/services/services.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ angular.module('sidecar.services', ['ngRoute', 'ui.bootstrap'])
1212
.factory('stateService', function($http, $q) {
1313
function svcGetServices() {
1414
return $http({
15-
method: 'GET',
15+
method: 'GET',
1616
url: '/api/services.json',
1717
dataType: 'json',
1818
});
1919
};
2020

21-
var haproxyUrl = window.location.protocol +
21+
var haproxyUrl = window.location.protocol +
2222
'//' + window.location.hostname +
2323
':3212/;csv;norefresh';
2424

@@ -185,7 +185,7 @@ angular.module('sidecar.services', ['ngRoute', 'ui.bootstrap'])
185185
ports.push(port.Port.toString())
186186
}
187187
}
188-
188+
189189
return ports.join(", ")
190190
};
191191
})
@@ -199,6 +199,8 @@ angular.module('sidecar.services', ['ngRoute', 'ui.bootstrap'])
199199
return "Unhealthy"
200200
case 3:
201201
return "Unknown"
202+
case 4:
203+
return "Draining"
202204
default:
203205
return "Tombstone"
204206
}
@@ -218,9 +220,9 @@ angular.module('sidecar.services', ['ngRoute', 'ui.bootstrap'])
218220
if (seconds > 630720000) { // More than 20 years ago
219221
return "never";
220222
}
221-
223+
222224
var interval = Math.floor(seconds / 31536000);
223-
225+
224226
if (interval > 1) {
225227
return interval + " years ago";
226228
}
@@ -275,11 +277,11 @@ if ( ! Array.prototype.groupBy) {
275277
this.forEach(function(o) {
276278
var group = JSON.stringify(f(o));
277279
groups[group] = groups[group] || [];
278-
groups[group].push(o);
280+
groups[group].push(o);
279281
});
280-
282+
281283
return Object.keys(groups).map(function (group) {
282-
return groups[group];
283-
});
284-
};
284+
return groups[group];
285+
});
286+
};
285287
}

0 commit comments

Comments
 (0)