11import datetime
22import random
33from logging import getLogger
4- from typing import Any , Optional
4+ from typing import Any , Optional , Iterable
55
66from taskiq import ScheduleSource
77from taskiq .abc .middleware import TaskiqMiddleware
@@ -35,6 +35,7 @@ def __init__(
3535 use_delay_exponent : bool = False ,
3636 max_delay_exponent : float = 60 ,
3737 schedule_source : Optional [ScheduleSource ] = None ,
38+ types_of_exceptions : Optional [Iterable [type [BaseException ]]] = None ,
3839 ) -> None :
3940 """
4041 Initialize retry middleware.
@@ -48,6 +49,7 @@ def __init__(
4849 :param max_delay_exponent: Maximum allowed delay when using backoff.
4950 :param schedule_source: Schedule source to use for scheduling.
5051 If None, the default broker will be used.
52+ :param types_of_exceptions: Types of exceptions to retry from.
5153 """
5254 super ().__init__ ()
5355 self .default_retry_count = default_retry_count
@@ -58,6 +60,7 @@ def __init__(
5860 self .use_delay_exponent = use_delay_exponent
5961 self .max_delay_exponent = max_delay_exponent
6062 self .schedule_source = schedule_source
63+ self .types_of_exceptions = types_of_exceptions
6164
6265 if not isinstance (schedule_source , (ScheduleSource , type (None ))):
6366 raise TypeError (
@@ -138,6 +141,11 @@ async def on_error(
138141 :param result: Execution result.
139142 :param exception: Caught exception.
140143 """
144+ if self .types_of_exceptions is not None and not isinstance (
145+ exception , tuple (self .types_of_exceptions )
146+ ):
147+ return
148+
141149 if isinstance (exception , NoResultError ):
142150 return
143151
0 commit comments