File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -77,4 +77,5 @@ of those changes to CLEARTYPE SRL.
7777| [ @ksoviero-zengrc ] ( https://github.com/ksoviero-zengrc ) | Kevin Soviero |
7878| [ @mikeroll ] ( https://github.com/mikeroll ) | Mikhail Bulash |
7979| [ @janek-cosmose ] ( https://github.com/janek-cosmose ) | Jan Szejko |
80- | [ @ABolouk ] ( https://github.com/ABolouk ) | Amirhossein Bolouk Asli |
80+ | [ @ABolouk ] ( https://github.com/ABolouk ) | Amirhossein Bolouk Asli|
81+ | [ @albcunha ] ( https://github.com/albcunha ) | Alberto Cunha |
Original file line number Diff line number Diff line change @@ -447,6 +447,46 @@ The result expiration can be also set per an actor:
447447 def add (x , y ):
448448 return x + y
449449
450+
451+ Custom Middleware
452+ -----------------
453+
454+ Writing a Custom Middleware Class
455+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
456+
457+ You can write a custom middleware class to customize or add functionality to many parts of Dramatiq.
458+ For the full list of hooks available, see the |Middleware | class.
459+
460+ For example, you could write a middleware that sets the message time limit dynamically based
461+ on the message arguments,
462+ using the :meth: `Middleware.before_process_message<dramatiq.Middleware.before_process_message> ` hook.
463+
464+ .. code-block :: python
465+
466+ import dramatiq
467+
468+ class DynamicTimeLimitMiddleware (dramatiq .Middleware ):
469+ def before_process_message (self , broker , message ):
470+ """ Sets a dynamic time limit on messages based on their arguments."""
471+ message.options[" time_limit" ] = calculate_time_limit(
472+ message.args, message.kwargs
473+ )
474+
475+ Finally, instantiate and add it to your broker:
476+
477+ .. code-block :: python
478+
479+ import dramatiq.middleware
480+
481+ broker.add_middleware(
482+ DynamicTimeLimitMiddleware(),
483+ before = dramatiq.middleware.TimeLimit,
484+ )
485+
486+ Note that in this case, the custom middleware must be inserted before the |TimeLimit | middleware
487+ so the dynamic time limit is applied to messaged before it is used by the |TimeLimit | middleware.
488+
489+
450490Scheduling
451491----------
452492
You can’t perform that action at this time.
0 commit comments