taskiq-python / taskiq

Distributed task queue with full async support
MIT License
872 stars 53 forks source link

Invert execution orders of post, on_error and shutdown middleware hooks #359

Open hawang-wish opened 3 months ago

hawang-wish commented 3 months ago

Hi,

I've further noticed that we could introduce a slight improvement to make middlewares follow the "onion" execution order. For example, if a message is wrapped with pre_send of multiple middlewares, it's generally better to run post_send in reversed order.

I've then inverted the order of post_execute, shutdown, on_error, and post_save so that all existing middleware hooks have an onion-ish invoke order.

A practical use case is that some of my middlewares do modifications to add a mark in pre_send and remove it in post_send. However, without this improvement I couldn't make the mark invisible to other middlewares.

The unit tests I added could fail because of other issues: https://github.com/taskiq-python/taskiq/pull/358 and https://github.com/taskiq-python/taskiq/pull/357

As always, let me know if you have suggestions or concerns, and thanks for the review in advance!

s3rius commented 3 weeks ago

Please fix lints and tests before the review.