Skip to content

Commit e04b9fa

Browse files
committed
Merge pull request #3607 from mboersma/simpler-code
ref(controller): simplify thread and set comparison code
2 parents 20a3eeb + e0c98bd commit e04b9fa

1 file changed

Lines changed: 12 additions & 24 deletions

File tree

controller/api/models.py

Lines changed: 12 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
import re
1515
import subprocess
1616
import time
17-
import threading
17+
from threading import Thread
1818

1919
from django.conf import settings
2020
from django.contrib.auth import get_user_model
@@ -84,9 +84,8 @@ def validate_id_is_docker_compatible(value):
8484
def validate_app_structure(value):
8585
"""Error if the dict values aren't ints >= 0."""
8686
try:
87-
for k, v in value.iteritems():
88-
if int(v) < 0:
89-
raise ValueError("Must be greater than or equal to zero")
87+
if any(int(v) < 0 for v in value.itervalues()):
88+
raise ValueError("Must be greater than or equal to zero")
9089
except ValueError, err:
9190
raise ValidationError(err)
9291

@@ -271,17 +270,13 @@ def scale(self, user, structure): # noqa
271270

272271
def _start_containers(self, to_add):
273272
"""Creates and starts containers via the scheduler"""
274-
create_threads = []
275-
start_threads = []
276273
if not to_add:
277-
# do nothing if we didn't request any containers
278274
return
279-
for c in to_add:
280-
create_threads.append(threading.Thread(target=c.create))
281-
start_threads.append(threading.Thread(target=c.start))
275+
create_threads = [Thread(target=c.create) for c in to_add]
276+
start_threads = [Thread(target=c.start) for c in to_add]
282277
[t.start() for t in create_threads]
283278
[t.join() for t in create_threads]
284-
if set([c.state for c in to_add]) != set(['created']):
279+
if any(c.state != 'created' for c in to_add):
285280
err = 'aborting, failed to create some containers'
286281
log_event(self, err, logging.ERROR)
287282
raise RuntimeError(err)
@@ -293,37 +288,30 @@ def _start_containers(self, to_add):
293288

294289
def _restart_containers(self, to_restart):
295290
"""Restarts containers via the scheduler"""
296-
stop_threads = []
297-
start_threads = []
298291
if not to_restart:
299-
# do nothing if we didn't request any containers
300292
return
301-
for c in to_restart:
302-
stop_threads.append(threading.Thread(target=c.stop))
303-
start_threads.append(threading.Thread(target=c.start))
293+
stop_threads = [Thread(target=c.stop) for c in to_restart]
294+
start_threads = [Thread(target=c.start) for c in to_restart]
304295
[t.start() for t in stop_threads]
305296
[t.join() for t in stop_threads]
306-
if set([c.state for c in to_restart]) != set(['created']):
297+
if any(c.state != 'created' for c in to_restart):
307298
err = 'warning, some containers failed to stop'
308299
log_event(self, err, logging.WARNING)
309300
[t.start() for t in start_threads]
310301
[t.join() for t in start_threads]
311-
if set([c.state for c in to_restart]) != set(['up']):
302+
if any(c.state != 'up' for c in to_restart):
312303
err = 'warning, some containers failed to start'
313304
log_event(self, err, logging.WARNING)
314305

315306
def _destroy_containers(self, to_destroy):
316307
"""Destroys containers via the scheduler"""
317-
destroy_threads = []
318308
if not to_destroy:
319-
# do nothing if we didn't request any containers
320309
return
321-
for c in to_destroy:
322-
destroy_threads.append(threading.Thread(target=c.destroy))
310+
destroy_threads = [Thread(target=c.destroy) for c in to_destroy]
323311
[t.start() for t in destroy_threads]
324312
[t.join() for t in destroy_threads]
325313
[c.delete() for c in to_destroy if c.state == 'destroyed']
326-
if set([c.state for c in to_destroy]) != set(['destroyed']):
314+
if any(c.state != 'destroyed' for c in to_destroy):
327315
err = 'aborting, failed to destroy some containers'
328316
log_event(self, err, logging.ERROR)
329317
raise RuntimeError(err)

0 commit comments

Comments
 (0)