Change experiment signal to pre delete for stopping jobs
# -*- coding: utf-8 -*-
from __future__ import absolute_import, division, print_function
-from django.db.models.signals import post_save, post_delete
+from django.db.models.signals import post_save, post_delete, pre_delete
from django.dispatch import receiver
from experiments.models import Experiment, ExperimentJob, ExperimentJobStatus
+from projects.models import ExperimentGroup
+from spawner.scheduling import schedule_stop_experiment
from spawner.utils.constants import JobLifeCycle, ExperimentLifeCycle
-from experiments.tasks import check_experiment_status, build_experiment, stop_experiment
+from experiments.tasks import check_experiment_status, build_experiment
@receiver(post_save, sender=Experiment, dispatch_uid="experiment_saved")
@@ -25,11 +27,15 @@ def new_experiment(sender, **kwargs):
build_experiment.delay(experiment_id=instance.id)
-@receiver(post_delete, sender=Experiment, dispatch_uid="experiment_deleted")
+@receiver(pre_delete, sender=Experiment, dispatch_uid="experiment_deleted")
def experiment_deleted(sender, **kwargs):
instance = kwargs['instance']
- stop_experiment.delay(experiment_id=instance.id)
-
+ try:
+ _ = instance.experiment_group
+ schedule_stop_experiment(instance, is_delete=True)
+ except ExperimentGroup.DoesNotExist:
+ # The experiment was already stopped when the group was deleted
+ pass
@receiver(post_save, sender=ExperimentJob, dispatch_uid="experiment_job_saved")
def new_experiment_job(sender, **kwargs):
