From 31ee14150ef8cd30f1708fcef9636785ef4693ab Mon Sep 17 00:00:00 2001 From: inigogr Date: Thu, 1 Feb 2024 10:07:01 +0100 Subject: [PATCH 1/2] fix retry format with tuple values --- queue_job/models/queue_job_function.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/queue_job/models/queue_job_function.py b/queue_job/models/queue_job_function.py index f593b6520a..14a674ab72 100644 --- a/queue_job/models/queue_job_function.py +++ b/queue_job/models/queue_job_function.py @@ -149,10 +149,12 @@ def _parse_retry_pattern(self): try: # as json can't have integers as keys and the field is stored # as json, convert back to int - retry_pattern = { - int(try_count): postpone_seconds - for try_count, postpone_seconds in self.retry_pattern.items() - } + retry_pattern = {} + for try_count, postpone_value in self.retry_pattern.items(): + if isinstance(postpone_value, int): + retry_pattern[try_count] = postpone_value + else: + retry_pattern[try_count] = tuple(postpone_value) except ValueError: _logger.error( "Invalid retry pattern for job function %s," @@ -181,8 +183,9 @@ def job_config(self, name): def _retry_pattern_format_error_message(self): return _( "Unexpected format of Retry Pattern for {}.\n" - "Example of valid format:\n" - "{{1: 300, 5: 600, 10: 1200, 15: 3000}}" + "Example of valid formats:\n" + "{{1: 300, 5: 600, 10: 1200, 15: 3000}}\n" + "{{1: (1, 10), 5: (11, 20), 10: (21, 30), 15: (100, 300)}}" ).format(self.name) @api.constrains("retry_pattern") @@ -195,11 +198,18 @@ def _check_retry_pattern(self): all_values = list(retry_pattern) + list(retry_pattern.values()) for value in all_values: try: - int(value) + self._retry_value_type_check(value) except ValueError: raise exceptions.UserError( record._retry_pattern_format_error_message() ) + def _retry_value_type_check(self, value): + if isinstance(value, (tuple, list)): + if len(value) != 2: + raise ValueError + [self._retry_value_type_check(element) for element in value] + return + int(value) def _related_action_format_error_message(self): return _( From 6a1aa5e4d97c6a4e371517b48a2189a7e3f18d2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?I=C3=B1igo?= Date: Thu, 1 Feb 2024 11:20:02 +0100 Subject: [PATCH 2/2] added missing int cast --- queue_job/models/queue_job_function.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/queue_job/models/queue_job_function.py b/queue_job/models/queue_job_function.py index 14a674ab72..d839708dcf 100644 --- a/queue_job/models/queue_job_function.py +++ b/queue_job/models/queue_job_function.py @@ -152,9 +152,9 @@ def _parse_retry_pattern(self): retry_pattern = {} for try_count, postpone_value in self.retry_pattern.items(): if isinstance(postpone_value, int): - retry_pattern[try_count] = postpone_value + retry_pattern[int(try_count)] = postpone_value else: - retry_pattern[try_count] = tuple(postpone_value) + retry_pattern[int(try_count)] = tuple(postpone_value) except ValueError: _logger.error( "Invalid retry pattern for job function %s," @@ -203,6 +203,7 @@ def _check_retry_pattern(self): raise exceptions.UserError( record._retry_pattern_format_error_message() ) + def _retry_value_type_check(self, value): if isinstance(value, (tuple, list)): if len(value) != 2: