@@ -434,18 +434,20 @@ def converge(self, databag):
434434 return databag
435435
436436 def destroy (self ):
437- node_tasks , layer_tasks , chef_tasks = [], [], []
438437 # create subtasks to terminate all nodes in parallel
439438 all_layers = self .layer_set .all ()
440- node_tasks .extend ([layer .destroy ()[0 ] for layer in all_layers ])
441- layer_tasks .extend ([layer .destroy ()[1 ] for layer in all_layers ])
442- # call a celery task to update the formation data bag
443- if settings .CHEF_ENABLED :
444- chef_tasks .extend ([controller .destroy_formation .s (self .id )]) # @UndefinedVariable
445- # block for tasks by group
439+ tasks = [ layer .destroy (async = True ) for layer in all_layers ]
440+ node_tasks , layer_tasks = [], []
441+ for n , l in tasks :
442+ node_tasks .append (n ), layer_tasks .append (l )
443+ # kill all the nodes in parallel
446444 group (* node_tasks ).apply_async ().join ()
445+ # kill all the layers in parallel
447446 group (* layer_tasks ).apply_async ().join ()
448- group (* chef_tasks ).apply_async ().join ()
447+ # call a celery task to update the formation data bag
448+ if settings .CHEF_ENABLED :
449+ controller .destroy_formation .delay (self .id ).wait () # @UndefinedVariable
450+ self .delete ()
449451
450452
451453@python_2_unicode_compatible
@@ -485,20 +487,25 @@ def build(self, *args, **kwargs):
485487 name = "{0}-{1}" .format (self .formation .id , self .id )
486488 args = (name , self .flavor .provider .creds .copy (),
487489 self .flavor .params .copy ())
488- return tasks .build_layer .subtask ( args )
490+ return tasks .build_layer .delay ( * args ). wait ( )
489491
490- def destroy (self ):
492+ def destroy (self , async = False ):
491493 tasks = import_tasks (self .flavor .provider .type )
492- subtasks = []
493494 # create subtasks to terminate all nodes in parallel
494- subtasks . extend ( [node .terminate () for node in self .node_set .all ()])
495- node_tasks = group (* subtasks )
495+ node_tasks = [node .terminate (async = True ) for node in self .node_set .all ()]
496+ node_tasks = group (* node_tasks )
496497 # purge other hosting provider infrastructure
497498 name = "{0}-{1}" .format (self .formation .id , self .id )
498499 args = (name , self .flavor .provider .creds .copy (),
499500 self .flavor .params .copy ())
500501 layer_tasks = group (* [tasks .destroy_layer .subtask (args )])
501- return node_tasks , layer_tasks
502+ if async :
503+ return node_tasks , layer_tasks
504+ # destroy nodes, then the layer
505+ node_tasks .apply_async ().join ()
506+ layer_tasks .apply_async ().join ()
507+ # delete the database record
508+ self .delete ()
502509
503510
504511@python_2_unicode_compatible
@@ -609,6 +616,12 @@ def _prepare_terminate_args(self):
609616 args = (self .uuid , creds , params , self .provider_id )
610617 return args
611618
619+ def destroy (self , async = False ):
620+ subtask = self .terminate ()
621+ if async :
622+ return subtask
623+ return subtask .apply_async ().wait ()
624+
612625
613626@python_2_unicode_compatible
614627class Container (UuidAuditedModel ):
0 commit comments