|
|
@@ -11,6 +11,7 @@ from django.contrib.contenttypes.models import ContentType
|
|
|
from django.core.exceptions import ObjectDoesNotExist
|
|
|
from django.core.management import call_command
|
|
|
from django.db import IntegrityError, connection
|
|
|
+from django.db.models.signals import pre_save
|
|
|
from django.test import TestCase, override_settings, tag
|
|
|
from django.test.utils import CaptureQueriesContext
|
|
|
from django.urls import reverse
|
|
|
@@ -394,6 +395,94 @@ class JobTestCase(TestCase):
|
|
|
**kwargs,
|
|
|
)
|
|
|
|
|
|
+ def test_enqueue_with_parent(self):
|
|
|
+ """
|
|
|
+ A job enqueued with a parent records it, and the parent is not passed to the job's callable.
|
|
|
+ """
|
|
|
+ received = {}
|
|
|
+
|
|
|
+ def func(**kwargs):
|
|
|
+ received.update(kwargs)
|
|
|
+
|
|
|
+ parent = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER)
|
|
|
+ job = Job.enqueue(func, name='Child Job', immediate=True, parent=parent, foo='bar')
|
|
|
+
|
|
|
+ # The parent instance is not cached on the job, and so not pickled into its RQ payload
|
|
|
+ self.assertFalse(Job.parent.is_cached(job))
|
|
|
+ job.refresh_from_db()
|
|
|
+ self.assertEqual(job.parent, parent)
|
|
|
+ self.assertEqual(received['foo'], 'bar')
|
|
|
+ self.assertNotIn('parent', received)
|
|
|
+
|
|
|
+ @patch('core.models.jobs.django_rq.get_queue')
|
|
|
+ def test_delete_parent_retains_children(self, mock_get_queue):
|
|
|
+ """
|
|
|
+ Deleting a job leaves its children in place, without a parent.
|
|
|
+ """
|
|
|
+ parent = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER)
|
|
|
+ child = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER, parent=parent)
|
|
|
+
|
|
|
+ parent.delete()
|
|
|
+
|
|
|
+ child.refresh_from_db()
|
|
|
+ self.assertIsNone(child.parent)
|
|
|
+ # Only the parent's RQ job is canceled
|
|
|
+ mock_get_queue.return_value.fetch_job.assert_called_once_with(str(parent.job_id))
|
|
|
+
|
|
|
+ @patch('core.models.jobs.django_rq.get_queue')
|
|
|
+ def test_terminate_after_parent_deleted(self, mock_get_queue):
|
|
|
+ """
|
|
|
+ A copy of a job loaded before its parent was deleted does not restore the deleted parent when saved.
|
|
|
+ """
|
|
|
+ parent = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER)
|
|
|
+ child = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER, parent=parent)
|
|
|
+ stale_child = Job.objects.get(pk=child.pk)
|
|
|
+
|
|
|
+ parent.delete()
|
|
|
+ stale_child.terminate()
|
|
|
+
|
|
|
+ child.refresh_from_db()
|
|
|
+ self.assertIsNone(child.parent_id)
|
|
|
+ self.assertEqual(child.status, JobStatusChoices.STATUS_COMPLETED)
|
|
|
+
|
|
|
+ @patch('core.models.jobs.django_rq.get_queue')
|
|
|
+ def test_terminate_while_parent_deleted(self, mock_get_queue):
|
|
|
+ """
|
|
|
+ A job's parent being deleted during the save which terminates it does not fail the save.
|
|
|
+ """
|
|
|
+ parent = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER)
|
|
|
+ child = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER, parent=parent)
|
|
|
+
|
|
|
+ def delete_parent(sender, instance, **kwargs):
|
|
|
+ if instance.pk == child.pk:
|
|
|
+ Job.objects.filter(pk=parent.pk).delete()
|
|
|
+
|
|
|
+ pre_save.connect(delete_parent, sender=Job)
|
|
|
+ try:
|
|
|
+ child.terminate()
|
|
|
+ finally:
|
|
|
+ pre_save.disconnect(delete_parent, sender=Job)
|
|
|
+
|
|
|
+ # Foreign keys are checked at commit, which the test transaction never reaches
|
|
|
+ connection.check_constraints(table_names=[Job._meta.db_table])
|
|
|
+ child.refresh_from_db()
|
|
|
+ self.assertIsNone(child.parent_id)
|
|
|
+ self.assertEqual(child.status, JobStatusChoices.STATUS_COMPLETED)
|
|
|
+
|
|
|
+ def test_save_does_not_query_parent(self):
|
|
|
+ """
|
|
|
+ Saving an existing job issues only its update, which leaves the parent unwritten.
|
|
|
+ """
|
|
|
+ parent = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER)
|
|
|
+ child = self._make_job(None, JobNotificationChoices.NOTIFICATION_NEVER, parent=parent)
|
|
|
+
|
|
|
+ with CaptureQueriesContext(connection) as ctx:
|
|
|
+ child.save()
|
|
|
+ child.save(update_fields=['status'])
|
|
|
+ job_queries = [q['sql'] for q in ctx.captured_queries if '"core_job"' in q['sql']]
|
|
|
+ self.assertEqual(len(job_queries), 2)
|
|
|
+ self.assertTrue(all(sql.startswith('UPDATE') and '"parent_id"' not in sql for sql in job_queries))
|
|
|
+
|
|
|
@patch('core.models.jobs.django_rq.get_queue')
|
|
|
def test_delete_cancels_job_from_correct_queue(self, mock_get_queue):
|
|
|
"""
|