def create_task(**kwargs): task = Task() default_function_name = 'tests.fixtures.do_nothing' task.function_name = kwargs.get('function_name', default_function_name) task.function_args = kwargs.get('function_args', '{}') task.due_at = kwargs.get('due_at', now()) if 'name' in kwargs: task.name = kwargs['name'] if 'status' in kwargs: task.status = kwargs['status'] if 'retries' in kwargs: task.retries = kwargs['retries'] if 'max_retries' in kwargs: task.max_retries = kwargs['max_retries'] if 'retry_delay' in kwargs: task.retry_delay = kwargs['retry_delay'] if 'retry_backoff' in kwargs: task.retry_backoff = kwargs['retry_backoff'] if 'retry_backoff_factor' in kwargs: task.retry_backoff_factor = kwargs['retry_backoff_factor'] task.save() return task
def create_task(**kwargs): task = Task() default_function_name = 'tests.fixtures.do_nothing' task.function_name = kwargs.get('function_name', default_function_name) task.function_args = kwargs.get('function_args', '{}') task.due_at = kwargs.get('due_at', now()) if 'name' in kwargs: task.name = kwargs['name'] if 'status' in kwargs: task.status = kwargs['status'] if 'retries' in kwargs: task.retries = kwargs['retries'] if 'max_retries' in kwargs: task.max_retries = kwargs['max_retries'] if 'retry_delay' in kwargs: task.retry_delay = parse_timedelta(kwargs['retry_delay']) if 'retry_backoff' in kwargs: task.retry_backoff = kwargs['retry_backoff'] if 'retry_backoff_factor' in kwargs: task.retry_backoff_factor = kwargs['retry_backoff_factor'] if 'timeout' in kwargs: task.timeout = parse_timedelta(kwargs['timeout'], nullable=True) task.save() return task
def test_tasks_update_due_at_with_custom_backoff_factor(self): """The task updates its due_at with a delay growing according to retry_backoff_factor set. """ base_due_at = datetime.datetime(2018, 11, 28, hour=20, tzinfo=timezone.utc) task = Task() task.due_at = base_due_at task.retry_delay = datetime.timedelta(seconds=10) task.retry_backoff = True task.retry_backoff_factor = 4 task.function_name = "tests.fixtures.do_nothing" task.save() self.assertEqual(task.due_at, base_due_at) previous_due_at = task.due_at for i in range(1, 4): task.retries = i before = timezone.now() task.update_due_at_after_failure() after = timezone.now() delta = datetime.timedelta(seconds=10 * (4 ** (i - 1))) self.assertGreaterEqual(task.due_at, before + delta) self.assertLessEqual(task.due_at, after + delta) self.assertGreater(task.due_at, previous_due_at) previous_due_at = task.due_at
def test_tasks_update_due_at_ignores_backoff_factor_if_backoff_false(self): """The task updates its due_at without taking into account its retry_backoff_factor if retry_backoff is False. """ base_due_at = datetime.datetime(2018, 11, 28, hour=20, tzinfo=timezone.utc) task = Task() task.due_at = base_due_at task.retry_delay = datetime.timedelta(seconds=10) task.retry_backoff = False task.retry_backoff_factor = 4 task.function_name = "tests.fixtures.do_nothing" task.save() self.assertEqual(task.due_at, base_due_at) for i in range(1, 4): task.retries = i before = timezone.now() task.update_due_at_after_failure() after = timezone.now() delta = datetime.timedelta(seconds=10) self.assertGreaterEqual(task.due_at, before + delta) self.assertLessEqual(task.due_at, after + delta)