Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: ability to declare queue/exchange binding #2011

Merged
merged 10 commits into from
Dec 30, 2024
Merged

Conversation

MagicAbdel
Copy link
Contributor

Description

Hi,

I was implementing dead lettering in RabbitMQ using faststream and I had to bind an queue/exchange. It resulted in the following:

if channel := rabbit_router.broker._channel:
    queue = await channel.get_queue(QUEUE.name)
    await queue.bind(
        DEAD_LETTER_EXCHANGE.name, routing_key=QUEUE.name
    )

With this; It can now can be written in a single line as (without accessing the "private" _channel):

await rabbit_router.broker.declarer.declare_binding(QUEUE, DEAD_LETTER_EXCHANGE)

Included a unit test scenario where we declare a queue/exchange binding, making sure that single call is made to the declare_queue/declare_exchange.

There might be some improvements to this code but I believe they should be in the RabbitQueue and not in the declarer. Let me know if there is anything to change.

NOTE: while running scripts/test-cov.sh I got the following (As I didn't touch any of these I just skipped it):

FAILED tests/prometheus/confluent/test_confluent.py::TestConsume::test_concurrent_consume - assert False
FAILED tests/a_docs/nats/js/test_object.py::test_basic - AssertionError: Expected 'mock' to be called once. Called 2 times.

Fixes #NOT_APPLICABLE

Type of change

Please delete options that are not relevant.

  • Documentation (typos, code examples, or any documentation updates)
  • New feature (a non-breaking change that adds functionality)
  • This change requires a documentation update

Checklist

  • My code adheres to the style guidelines of this project (scripts/lint.sh shows no errors)
  • I have conducted a self-review of my own code
  • I have made the necessary changes to the documentation
  • My changes do not generate any new warnings
  • I have added tests to validate the effectiveness of my fix or the functionality of my new feature
  • Both new and existing unit tests pass successfully on my local environment by running scripts/test-cov.sh
  • I have ensured that static analysis tests are passing by running scripts/static-analysis.sh
  • I have included code examples to illustrate the modifications

@Lancetnik
Copy link
Member

Sorry, but I don't think, that the new method required

async def declare_binding(
    self,
    queue: "RabbitQueue",
    exchange: "RabbitExchange",
    routing_key: Optional[str] = None,
) -> None:
    """Declare a binding between a queue and an exchange."""
    q = await self.declare_queue(queue)
    exch = await self.declare_exchange(exchange)

    await q.bind(
        exchange=exch,
        routing_key=routing_key,
    )

Inner code looks simple enough to provide it to user as it. Probably, we can just add such example to the documentation?

Copy link
Member

@Lancetnik Lancetnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, we can just add the example to the doc as it

@MagicAbdel MagicAbdel changed the title feat: ability to declare queue/exchange binding docs: ability to declare queue/exchange binding Dec 29, 2024
@MagicAbdel
Copy link
Contributor Author

You're right, now that I think about it it's just syntactic sugar. I reverted changes and added the example to the doc

@Lancetnik
Copy link
Member

@MagicAbdel thank you for the suggestion and the PR itself!

Copy link
Member

@Lancetnik Lancetnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We are using includable python files to be sure, that all code examples are runnable and correct. This reason we have a special tests for such files in tests/docs/...

It will be kind, if you add any test for you code example. I think, it should use monkeypathcing to check, that inner aio-pika method was called correctly

@MagicAbdel
Copy link
Contributor Author

That was my bad I didn't notice there were tests for doc code snippets, that is really nice btw! I just added relevant test

Lancetnik
Lancetnik previously approved these changes Dec 30, 2024
@Lancetnik
Copy link
Member

Finall, I just want to clone the repo and take a look at compiled documentation. Then I'll merge it. Thank your for the work!

Lancetnik
Lancetnik previously approved these changes Dec 30, 2024
@Lancetnik Lancetnik added documentation Improvements or additions to documentation RabbitMQ Issues related to `faststream.rabbit` module and RabbitMQ broker features labels Dec 30, 2024
@Lancetnik Lancetnik added this pull request to the merge queue Dec 30, 2024
Merged via the queue into airtai:main with commit 4a5c491 Dec 30, 2024
30 checks passed
doublehomixide pushed a commit to doublehomixide/faststream that referenced this pull request Jan 3, 2025
* feat: declare queue/exchange binding

* docs: generate API References

* docs: reverting the changes and adding queue/exchange binding to the documentation

* docs: reverting the changes and adding queue/exchange binding to the documentation

* docs: reverting the changes and adding queue/exchange binding to the documentation

* docs: generate API References

* test: added some testing for the doc code snippet

* chore: polish docs

* tests: fix RMQ bind test

---------

Co-authored-by: MagicAbdel <[email protected]>
Co-authored-by: Nikita Pastukhov <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation RabbitMQ Issues related to `faststream.rabbit` module and RabbitMQ broker features
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants