Comments (11)
Hi, @jonatasoli! There is no any error: you are trying to send the message to nowhere: .publish(message, queue)
requires a message body with the first argument.
So, in your case body is RabbitQueue('test', arguments={'m': username})
and you send the message with ""
routing key to default exchange.
If you try to send message like
publish(queue=RabbitQueue('test', arguments={'m': username})
You will get a message serialization error.
If you will try to send correct message
publish({"username": "john"}, queue=RabbitQueue('test', arguments={'m': username})
You will get the Exception('test')
So, all exceptions are printing correctly
from propan.
Found With set only queue
import logging
import sys
from propan.brokers.rabbit import RabbitQueue
from propan.fastapi import RabbitRouter
from pydantic import BaseModel
import sentry_sdk
from dynaconf import settings
from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from loguru import logger
from endpoints.v1.users import user
task_cart_router = RabbitRouter(settings.BROKER_URL)
class Incoming(BaseModel):
username: str
def call():
return True
@task_cart_router.event(RabbitQueue("test"))
async def hello(m: Incoming, d = Depends(call)):
logger.info(f" log test {m.username}")
return m.username
return { "response": f"Hello, {m.username}!" }
app = FastAPI(lifespan=task_cart_router.lifespan_context)
app.include_router(task_cart_router)
origins = [
'*',
]
app.add_middleware(
CORSMiddleware,
allow_origins=origins,
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
@user.post('/test/{username}')
async def test(username: str):
task_response = await task_cart_router.broker.publish({"username": username}, queue=RabbitQueue('test'))
return { "response": f"Hiiii, {username}! {str(task_response.body)}" }
app.include_router(user)
app.include_router(task_cart_router)
If you want I create a simple example with this example
https://github.com/Lancetnik/Propan/tree/main/examples/http_frameworks_integrations
from propan.
I can't understand your problem to be honest. You will have no any messages until you doesn't trigger your POST hook. So, what you expect in your console at startup if there no any messages yet?
When messages published, all works correct, I checked it with your code example.
from propan.
If you want to publish smth at startup, use router.after_startup
hook
from propan.
My example I try exec the event via swagger this example but, after the original code the message send to rabbitmq but, don't receive the message in event or dont show the logs.
from propan.
@jonatasoli the following example works fine for me
from propan.brokers.rabbit import RabbitQueue
from propan.fastapi import RabbitRouter
from pydantic import BaseModel
from fastapi import Depends, FastAPI
from fastapi.middleware.cors import CORSMiddleware
from loguru import logger
task_cart_router = RabbitRouter()
class Incoming(BaseModel):
username: str
def call():
return True
@task_cart_router.event(RabbitQueue("test"))
async def hello(m: Incoming, d = Depends(call)):
logger.info(f" log test {m.username}")
return m.username
app = FastAPI(lifespan=task_cart_router.lifespan_context)
app.add_middleware(
CORSMiddleware,
allow_origins=['*'],
allow_credentials=True,
allow_methods=['*'],
allow_headers=['*'],
)
@app.post('/test/{username}')
async def test(username: str):
task_response = await task_cart_router.broker.publish({"username": username}, queue=RabbitQueue('test'))
return { "response": f"Hiiii, {username}! {str(task_response)}" }
app.include_router(task_cart_router)
from propan.
But if you want to catch the event
return, you need to use callback=True
flag
from propan.
Yep this mode is found to me too, I think is great up this example with sending and receiving the fastapi example in repository is:
from contextlib import asynccontextmanager
from fastapi import FastAPI
from propan import RabbitBroker
broker = RabbitBroker("amqp://guest:guest@localhost:5672/")
app = FastAPI()
@asynccontextmanager
async def lifespan(app: FastAPI):
await broker.start()
yield
await broker.close()
@broker.handle("test")
async def base_handler(body):
print(body)
@app.get("/")
def read_root():
return {"Hello": "World"}
from propan.
I resolve my problem so I think close this issue, but I think a good ideia add a simple mode this example to examples galery, I send PR if you think is a good ideia also.
from propan.
@jonatasoli
There are two FastAPI examples already:
- raw: https://github.com/Lancetnik/Propan/blob/main/examples/http_frameworks_integrations/fastapi.py
- router: https://github.com/Lancetnik/Propan/blob/main/examples/http_frameworks_integrations/native_fastapi.py
Is it the same thing you want to add?
from propan.
Yes I think add one more with this example.
from propan.
Related Issues (20)
- RabbitQueue as a parameter for a router handle HOT 5
- Generator-based dependency functions fail with Propan HOT 4
- Split FastAPI tests
- weird package imports HOT 1
- Separate dev distributions to different requirements files
- Update CI to use required dependencies versions
- Fix all mkdocs inner links warnings
- Bug: propan run chokes on relative imports in the main script HOT 3
- Refactor project templates HOT 2
- Feature: Marshmallow support along with pydantic? HOT 3
- Feature/Question: consume a batch of messages HOT 3
- Bug: Pydantic v1 'RabbitQueue' object has no attribute 'routing_key' HOT 2
- Bug: SQSBroker not consuming when Queue is empty on startup. HOT 2
- BrokerRouter can't be called with `build_message` in tests
- 0.1.6.0 ZeroMQ (Brokerless Messaging)
- Implement an Exception to stop consuming from handler
- Scheduling Docs page
- Add `New feature` Issue template
- Relative imports and non-root import paths crash the app HOT 8
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from propan.