Skip to content

Commit c510fc5

Browse files
author
Matthew Fisher
committed
fix(controller): give processes unique counters
Previously, when a container was created through App.scale(), it would give the container a number based off the highest container number amongst all containers available for the application. This has been changed to give the container a number based off the highest container number amongst all containers with the same process type. fixes #769
1 parent e6e15d1 commit c510fc5

2 files changed

Lines changed: 5 additions & 3 deletions

File tree

controller/api/models.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from django.conf import settings
1616
from django.contrib.auth.models import User
1717
from django.db import models
18+
from django.db.models import Max
1819
from django.db.models.signals import post_delete
1920
from django.db.models.signals import post_save
2021
from django.utils.encoding import python_2_unicode_compatible
@@ -138,16 +139,16 @@ def scale(self, **kwargs):
138139
"""Scale containers up or down to match requested."""
139140
requested_containers = self.structure.copy()
140141
release = self.release_set.latest()
141-
# increment new container nums off the most recent container
142-
all_containers = self.container_set.all().order_by('-created')
143-
container_num = 1 if not all_containers else all_containers[0].num + 1
144142
msg = 'Containers scaled ' + ' '.join(
145143
"{}={}".format(k, v) for k, v in requested_containers.items())
146144
# iterate and scale by container type (web, worker, etc)
147145
changed = False
148146
to_add, to_remove = [], []
149147
for container_type in requested_containers.keys():
150148
containers = list(self.container_set.filter(type=container_type).order_by('created'))
149+
# increment new container nums off the most recent container
150+
results = self.container_set.filter(type=container_type).aggregate(Max('num'))
151+
container_num = results.get('num__max') or 0 + 1
151152
requested = requested_containers.pop(container_type)
152153
diff = requested - len(containers)
153154
if diff == 0:

controller/api/tests/test_container.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ def test_container_api(self):
151151
response = self.client.get(url)
152152
self.assertEqual(response.status_code, 200)
153153
self.assertEqual(len(response.data['results']), 3)
154+
self.assertEqual(max(c['num'] for c in response.data['results']), 2)
154155
url = "/api/apps/{app_id}".format(**locals())
155156
response = self.client.get(url)
156157
self.assertEqual(response.status_code, 200)

0 commit comments

Comments
 (0)