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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

馃帹 Add explicit async methods for fastapi app lifespan #79

Merged
merged 2 commits into from
Nov 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
MQTT is a lightweight publish/subscribe messaging protocol designed for M2M (machine to machine) telemetry in low bandwidth environments.
Fastapi-mqtt is the client for working with MQTT.

For more information about MQQT, please refer to here: [MQTT](mqtt.md)
For more information about MQTT, please refer to here: [MQTT](mqtt.md)

Fatapi-mqtt wraps around [gmqtt](https://github.com/wialon/gmqtt) module. Gmqtt Python async client for MQTT client implementation.
The module has the support of MQTT version 5.0 protocol
Expand Down
16 changes: 12 additions & 4 deletions fastapi_mqtt/fastmqtt.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import asyncio
import uuid
from functools import partial
from itertools import zip_longest
from typing import Any, Callable, Dict, List, Optional, Tuple

Expand Down Expand Up @@ -213,21 +212,30 @@ def unsubscribe(self, topic: str, **kwargs):

topic: topic name
"""
partial(self.client.unsubscribe, topic, **kwargs)
log_info.debug("unsubscribe")
if topic in self.subscriptions:
del self.subscriptions[topic]

return self.client.unsubscribe(topic, **kwargs)

async def mqtt_startup(self):
"""Initial connection for MQTT client, for lifespan startup."""
await self.connection()

async def mqtt_shutdown(self):
"""Final disconnection for MQTT client, for lifespan shutdown."""
await self.client.disconnect()

def init_app(self, app: FastAPI) -> None: # pragma: no cover
"""Add startup and shutdown event handlers for app without lifespan."""

@app.on_event("startup")
async def startup():
await self.connection()
await self.mqtt_startup()

@app.on_event("shutdown")
async def shutdown():
await self.client.disconnect()
await self.mqtt_shutdown()

def subscribe(
self,
Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def test_app():

@asynccontextmanager
async def _lifespan(application: FastAPI):
await fast_mqtt.connection()
await fast_mqtt.mqtt_startup()
logging.info("connection done, starting fastapi app now")
yield
await fast_mqtt.client.disconnect()
await fast_mqtt.mqtt_shutdown()

app = FastAPI(lifespan=_lifespan)

Expand Down