def retry_task(modeladmin, request, queryset): """Admin Site Action that retries the selected tasks. """ task_ids = [] for task in queryset.all(): task_name = task.task task_args = serialization.loads(task.args) task_kwargs = serialization.loads(task.kwargs) result = current_app.send_task( task_name, args=task_args, kwargs=task_kwargs) task_ids.append(result.task_id) modeladmin.message_user(request, "Tasks sent: {0}".format(task_ids))
def retry_task(modeladmin, request, queryset): """Admin Site Action that retries the selected tasks. """ task_ids = [] for task in queryset.all(): task_name = task.task task_args = serialization.loads(task.args) task_kwargs = serialization.loads(task.kwargs) result = current_app.send_task(task_name, args=task_args, kwargs=task_kwargs) task_ids.append(result.task_id) modeladmin.message_user(request, "Tasks sent: {0}".format(task_ids))
def test_parameters_schedule_eta_ignore_result(self): with self.settings(DJANGO_CELERY_FULLDBRESULT_SCHEDULE_ETA=True, CELERY_IGNORE_RESULT=True, DJANGO_CELERY_FULLDBRESULT_MONKEY_PATCH_ASYNC=True): apply_async_monkey_patch() a_date = datetime(2080, 1, 1, tzinfo=utc) do_something.apply_async( kwargs={"param": "testing"}, eta=a_date) task = TaskResultMeta.objects.order_by("pk")[0] self.assertEqual( "test_app.tasks.do_something", task.task) # This is new and was never set in previous backend. It is only set # before the task is published. self.assertIsNotNone(task.date_submitted) self.assertEqual(SCHEDULED, task.status) # Attributes such as eta are preserved self.assertEqual(a_date, task.eta) kwargs = serialization.loads(task.kwargs) self.assertEqual(kwargs, {"param": "testing"}) unapply_async_monkey_patch()
def send_scheduled_task(): """Task that sends due scheduled tasks for execution. Each DB operation must be in its own commit so that even if send_scheduled_task is called multiple times concurrently, a task is ensured to only be sent **at most once**. If a crash occurs while sending a task, the task will stay indefinitely in the SCHEDULED state while having a schedule id. 1. Tasks due to be executed are marked with a schedule id. This prevents the task from being sent for execution twice. 2. Each task marked by the schedule id is sent for exeuction without an ETA. 3. We change the status from SCHEDULED to SCHEDULED SENT after a task is being sent for execution. **IMPORTANT: ** Never call this task inside an atomic block or you could end up sending tasks more than once. Always use autocommit (each SQL query executed in its own transaction). """ limit = now() schedule_id = uuid4().hex # Mark tasks ready to be scheduled TaskResultMeta.objects.filter( scheduled_id__isnull=True, eta__lt=limit, status=SCHEDULED).update( scheduled_id=schedule_id) # Fetch and apply by removing eta for task in TaskResultMeta.objects.filter( scheduled_id=schedule_id).all(): task_name = task.task task_args = serialization.loads(task.args) task_kwargs = serialization.loads(task.kwargs) result = current_app.send_task( task_name, args=task_args, kwargs=task_kwargs) task.status = SCHEDULED_SENT task.result = {"new_task_id": result.task_id} task.save()
def to_python(self, value): if use_json(): if value: try: value = serialization.loads(value) return value except Exception: if isinstance(value, StringType): # Badly formatted JSON! raise else: return value else: return None else: return super(PickledOrJSONObjectField, self).to_python(value)
def test_parameters(self): a_date = datetime(2080, 1, 1, tzinfo=utc) do_something.apply_async(kwargs={"param": "testing"}, eta=a_date) task = TaskResultMeta.objects.order_by("pk")[0] self.assertEqual("test_app.tasks.do_something", task.task) # This is new and was never set in previous backend. It is only set # before the task is published. self.assertIsNotNone(task.date_submitted) # Task is never executed because eager = false self.assertEqual(PENDING, task.status) # Attributes such as eta are preserved self.assertEqual(a_date, task.eta) kwargs = serialization.loads(task.kwargs) self.assertEqual(kwargs, {"param": "testing"})
def test_parameters_schedule_eta(self): with self.settings(DJANGO_CELERY_FULLDBRESULT_SCHEDULE_ETA=True, DJANGO_CELERY_FULLDBRESULT_MONKEY_PATCH_ASYNC=True): apply_async_monkey_patch() a_date = datetime(2080, 1, 1, tzinfo=utc) do_something.apply_async(kwargs={"param": "testing"}, eta=a_date) task = TaskResultMeta.objects.order_by("pk")[0] self.assertEqual("test_app.tasks.do_something", task.task) # This is new and was never set in previous backend. It is only set # before the task is published. self.assertIsNotNone(task.date_submitted) self.assertEqual(SCHEDULED, task.status) # Attributes such as eta are preserved self.assertEqual(a_date, task.eta) kwargs = serialization.loads(task.kwargs) self.assertEqual(kwargs, {"param": "testing"}) unapply_async_monkey_patch()
def test_parameters(self): a_date = datetime(2080, 1, 1, tzinfo=utc) do_something.apply_async( kwargs={"param": "testing"}, eta=a_date) task = TaskResultMeta.objects.order_by("pk")[0] self.assertEqual( "test_app.tasks.do_something", task.task) # This is new and was never set in previous backend. It is only set # before the task is published. self.assertIsNotNone(task.date_submitted) # Task is never executed because eager = false self.assertEqual(PENDING, task.status) # Attributes such as eta are preserved self.assertEqual(a_date, task.eta) kwargs = serialization.loads(task.kwargs) self.assertEqual(kwargs, {"param": "testing"})