|
15 | 15 | from django.conf import settings |
16 | 16 | from django.contrib.auth.models import User |
17 | 17 | from django.db import models |
| 18 | +from django.db.models import Max |
18 | 19 | from django.db.models.signals import post_delete |
19 | 20 | from django.db.models.signals import post_save |
20 | 21 | from django.utils.encoding import python_2_unicode_compatible |
@@ -141,16 +142,16 @@ def scale(self, **kwargs): |
141 | 142 | """Scale containers up or down to match requested.""" |
142 | 143 | requested_containers = self.structure.copy() |
143 | 144 | release = self.release_set.latest() |
144 | | - # increment new container nums off the most recent container |
145 | | - all_containers = self.container_set.all().order_by('-created') |
146 | | - container_num = 1 if not all_containers else all_containers[0].num + 1 |
147 | 145 | msg = 'Containers scaled ' + ' '.join( |
148 | 146 | "{}={}".format(k, v) for k, v in requested_containers.items()) |
149 | 147 | # iterate and scale by container type (web, worker, etc) |
150 | 148 | changed = False |
151 | 149 | to_add, to_remove = [], [] |
152 | 150 | for container_type in requested_containers.keys(): |
153 | 151 | containers = list(self.container_set.filter(type=container_type).order_by('created')) |
| 152 | + # increment new container nums off the most recent container |
| 153 | + results = self.container_set.filter(type=container_type).aggregate(Max('num')) |
| 154 | + container_num = results.get('num__max') or 0 + 1 |
154 | 155 | requested = requested_containers.pop(container_type) |
155 | 156 | diff = requested - len(containers) |
156 | 157 | if diff == 0: |
|
0 commit comments