# SPDX-FileCopyrightText: 2022 Hynek Schlawack <hs@ox.cx>## SPDX-License-Identifier: MITfrom__future__importannotationsfromtypingimportIterablefrom._dataimportRetryHook,RetryHookFactoryfrom._loggingimportLoggingOnRetryHookfrom._prometheusimportPrometheusOnRetryHookfrom._structlogimportStructlogOnRetryHookdefinit_hooks(maybe_delayed:tuple[RetryHook|RetryHookFactory,...],)->tuple[RetryHook,...]:""" Execute delayed hook factories and return a tuple of finalized hooks. """hooks=[]forhook_or_factoryinmaybe_delayed:ifisinstance(hook_or_factory,RetryHookFactory):hooks.append(hook_or_factory.hook_factory())else:hooks.append(hook_or_factory)returntuple(hooks)defget_default_hooks()->tuple[RetryHookFactory,...]:""" Return the default hooks according to availability. """hooks=[]try:importprometheus_client# noqa: F401hooks.append(PrometheusOnRetryHook)exceptImportError:passtry:importstructlog# noqa: F401hooks.append(StructlogOnRetryHook)exceptImportError:hooks.append(LoggingOnRetryHook)returntuple(hooks)
[docs]defset_on_retry_hooks(hooks:Iterable[RetryHook|RetryHookFactory]|None,)->None:""" Set hooks that are called after a retry has been scheduled. Args: hooks: Hooks to call after a retry has been scheduled. Passing None resets to default. To deactivate instrumentation, pass an empty iterable. .. versionadded:: 23.2.0 """from.._configimportCONFIGCONFIG.on_retry=tuple(hooks)ifhooksisnotNoneelsehooks# type: ignore[assignment,arg-type]
[docs]defget_on_retry_hooks()->tuple[RetryHook,...]:""" Get hooks that are called after a retry has been scheduled. Returns: Hooks that will run if a retry is scheduled. Factories are called if they haven't already. .. versionadded:: 23.2.0 """from.._configimportCONFIGreturnCONFIG.on_retry