Skip to content

Commit

Permalink
added clear_extra for removing instances when config.count decreases
Browse files Browse the repository at this point in the history
  • Loading branch information
James McGuinness committed Aug 2, 2013
1 parent b9e88c3 commit 8b9bc0a
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 0 deletions.
2 changes: 2 additions & 0 deletions tests/core/service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,13 +154,15 @@ def test_update_node_pool_enabled(self):
self.service.enabled = True
self.service.update_node_pool()
self.service.instances.update_node_pool.assert_called_once_with()
self.service.instances.clear_extra.assert_called_once_with()
self.service.repair.assert_called_once_with()

def test_update_node_pool_disabled(self):
autospec_method(self.service.repair)
self.service.enabled = False
self.service.update_node_pool()
self.service.instances.update_node_pool.assert_called_once_with()
self.service.instances.clear_extra.assert_called_once_with()
assert not self.service.repair.called


Expand Down
10 changes: 10 additions & 0 deletions tests/core/serviceinstance_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,16 @@ def test_update_node_pool_diff_everything(self):
assert all([instance.stop.called for instance in self.collection.instances])
assert_equal(self.collection.instances, [])

def test_clear_extra(self):
instance_a = mock.Mock()
instance_b = mock.Mock()
instance_c = mock.Mock()
self.collection.instances = [instance_a, instance_b, instance_c]
self.collection.config.count = 2
self.collection.clear_extra()
assert_equal(self.collection.instances, [instance_a, instance_b])
instance_c.stop.assert_called_once_with()


if __name__ == "__main__":
run()
1 change: 1 addition & 0 deletions tron/core/service.py
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ def restore_state(self, state_data):

def update_node_pool(self):
self.instances.update_node_pool()
self.instances.clear_extra()
if self.enabled:
self.repair()

Expand Down
6 changes: 6 additions & 0 deletions tron/core/serviceinstance.py
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,12 @@ def update_node_pool(self):
self.instances = [i for i in self.instances if
i not in needs_new_node]

def clear_extra(self):
"""Clear out instances if too many exist."""
for i in range(0, self.missing, -1):
instance = self.instances.pop()
instance.stop()

@property
def missing(self):
return self.config.count - len(self.instances)
Expand Down

0 comments on commit 8b9bc0a

Please sign in to comment.