diff --git a/tests/core/service_test.py b/tests/core/service_test.py index 37f3501ef..24d2a4afa 100644 --- a/tests/core/service_test.py +++ b/tests/core/service_test.py @@ -154,6 +154,7 @@ 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): @@ -161,6 +162,7 @@ def test_update_node_pool_disabled(self): 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 diff --git a/tests/core/serviceinstance_test.py b/tests/core/serviceinstance_test.py index 8b613805a..72ed3db1e 100644 --- a/tests/core/serviceinstance_test.py +++ b/tests/core/serviceinstance_test.py @@ -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() diff --git a/tron/core/service.py b/tron/core/service.py index ff678654c..58c010266 100644 --- a/tron/core/service.py +++ b/tron/core/service.py @@ -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() diff --git a/tron/core/serviceinstance.py b/tron/core/serviceinstance.py index c6468dfa3..e260973c5 100644 --- a/tron/core/serviceinstance.py +++ b/tron/core/serviceinstance.py @@ -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)