1
1
import datetime
2
2
import random
3
3
from logging import getLogger
4
- from typing import Any , Optional
4
+ from typing import Any , Optional , Iterable
5
5
6
6
from taskiq import ScheduleSource
7
7
from taskiq .abc .middleware import TaskiqMiddleware
@@ -35,6 +35,7 @@ def __init__(
35
35
use_delay_exponent : bool = False ,
36
36
max_delay_exponent : float = 60 ,
37
37
schedule_source : Optional [ScheduleSource ] = None ,
38
+ types_of_exceptions : Optional [Iterable [type [BaseException ]]] = None ,
38
39
) -> None :
39
40
"""
40
41
Initialize retry middleware.
@@ -48,6 +49,7 @@ def __init__(
48
49
:param max_delay_exponent: Maximum allowed delay when using backoff.
49
50
:param schedule_source: Schedule source to use for scheduling.
50
51
If None, the default broker will be used.
52
+ :param types_of_exceptions: Types of exceptions to retry from.
51
53
"""
52
54
super ().__init__ ()
53
55
self .default_retry_count = default_retry_count
@@ -58,6 +60,7 @@ def __init__(
58
60
self .use_delay_exponent = use_delay_exponent
59
61
self .max_delay_exponent = max_delay_exponent
60
62
self .schedule_source = schedule_source
63
+ self .types_of_exceptions = types_of_exceptions
61
64
62
65
if not isinstance (schedule_source , (ScheduleSource , type (None ))):
63
66
raise TypeError (
@@ -138,6 +141,11 @@ async def on_error(
138
141
:param result: Execution result.
139
142
:param exception: Caught exception.
140
143
"""
144
+ if self .types_of_exceptions is not None and not isinstance (
145
+ exception , tuple (self .types_of_exceptions )
146
+ ):
147
+ return
148
+
141
149
if isinstance (exception , NoResultError ):
142
150
return
143
151
0 commit comments