Skip to content

Commit

Permalink
fix(hatchery:swarm): clean service remove useless condition
Browse files Browse the repository at this point in the history
  • Loading branch information
richardlt committed Jan 17, 2020
1 parent b5de1c1 commit b87954a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions engine/hatchery/swarm/swarm.go
Original file line number Diff line number Diff line change
Expand Up @@ -626,7 +626,7 @@ func (h *HatcherySwarm) listAwolWorkers(dockerClientName string, containers []ty
//Checking workers
oldContainers := []types.Container{}
for _, c := range workers {
if !strings.Contains(c.Status, "Exited") && time.Now().Add(-1*time.Minute).Unix() < c.Created {
if !strings.Contains(c.Status, "Exited") && time.Now().Add(-3*time.Minute).Unix() < c.Created {
log.Debug("hatchery> swarm> listAwolWorkers> container %s(status=%s) is too young", c.Names[0], c.Status)
continue
}
Expand Down Expand Up @@ -681,12 +681,25 @@ func (h *HatcherySwarm) killAwolWorker(ctx context.Context) error {
}
}

// creating a map of containers names
mContainers := map[string]struct{}{}
for i := range containers {
name := strings.TrimPrefix(containers[i].Names[0], "/") // docker returns name prefixed by a /
mContainers[name] = struct{}{}
}

// Checking services
for _, c := range containers {
if c.Labels["service_worker"] == "" || c.Names[0] != c.Labels["service_worker"] {
// checks if the container is a service based on its labels
if c.Labels["service_worker"] == "" {
continue
}
if !strings.Contains(c.Status, "Exited") && time.Now().Add(-1*time.Minute).Unix() < c.Created {
// if the worker associated to this service is still alive do not kill the service
if _, workerStillAlive := mContainers[c.Labels["service_worker"]]; workerStillAlive {
continue
}

if !strings.Contains(c.Status, "Exited") && time.Now().Add(-3*time.Minute).Unix() < c.Created {
log.Debug("hatchery> swarm> killAwolWorker> container %s(status=%s) is too young - service associated to worker %s", c.Names[0], c.Status, c.Labels["service_worker"])
continue
}
Expand Down
2 changes: 1 addition & 1 deletion engine/hatchery/swarm/swarm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func TestHatcherySwarm_ListAwolWorker(t *testing.T) {
h.Config.Name = "swarmy"

now := time.Now()
d1h := now.Add(-2 * time.Minute)
d1h := now.Add(-5 * time.Minute)
containers := []types.Container{
{
ID: "swarmy-model1-w1",
Expand Down

0 comments on commit b87954a

Please sign in to comment.