-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add redis and safir dependencies to the project Add redis to tests Co-authored-by: Jonathan Sick <[email protected]>
- Loading branch information
1 parent
97fe2ee
commit bfbece6
Showing
14 changed files
with
375 additions
and
525 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,4 +20,4 @@ types-PyYAML | |
|
||
# Documentation | ||
documenteer[guide]==1.0.0a1 | ||
sphinx-click | ||
sphinx-click |
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
"""Redis dependency for FastAPI.""" | ||
|
||
from typing import Optional | ||
|
||
from redis.asyncio import Redis | ||
|
||
__all__ = ["RedisDependency", "redis_dependency"] | ||
|
||
|
||
class RedisDependency: | ||
"""Provides an asyncio-based Redis client as a dependency. | ||
Notes | ||
----- | ||
This dependency must be initialized in a start-up hook (`initialize`) and | ||
closed in a shut down hook (`close`). | ||
""" | ||
|
||
def __init__(self) -> None: | ||
self.redis: Redis | None = None | ||
|
||
async def initialize( | ||
self, redis_url: str, password: Optional[str] = None | ||
) -> None: | ||
self.redis = Redis.from_url(redis_url, password=password) | ||
|
||
async def __call__(self) -> Redis: | ||
"""Returns the redis pool.""" | ||
if self.redis is None: | ||
raise RuntimeError("RedisDependency is not initialized") | ||
return self.redis | ||
|
||
async def close(self) -> None: | ||
"""Close the open Redis pool. | ||
Should be called from a shutdown hook to ensure that the Redis clients | ||
are cleanly shut down and any pending writes are complete. | ||
""" | ||
if self.redis: | ||
await self.redis.close() | ||
await self.redis.connection_pool.disconnect() | ||
self.redis = None | ||
|
||
|
||
redis_dependency = RedisDependency() | ||
"""The dependency that will return the Redis pool.""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
from .ping import ping | ||
|
||
__all__ = [ | ||
"ping", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
"""A proof-of-concept worker function.""" | ||
|
||
from __future__ import annotations | ||
|
||
from typing import Any, Dict | ||
|
||
|
||
async def ping(ctx: Dict[Any, Any]) -> str: | ||
logger = ctx["logger"].bind(task="ping") | ||
logger.info("Running ping") | ||
return "pong" |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
"""Arq-based queue worker lifecycle configuration.""" | ||
|
||
from __future__ import annotations | ||
|
||
import uuid | ||
from typing import Any, Dict | ||
|
||
import httpx | ||
import structlog | ||
from safir.logging import configure_logging | ||
|
||
from semaphore.config import config | ||
from semaphore.dependencies.redis import redis_dependency | ||
|
||
from .functions import ping | ||
|
||
|
||
async def startup(ctx: Dict[Any, Any]) -> None: | ||
"""Runs during working start-up to set up the worker context.""" | ||
configure_logging( | ||
profile=config.profile, | ||
log_level=config.log_level, | ||
name="semaphore", | ||
) | ||
logger = structlog.get_logger("semaphore") | ||
# The instance key uniquely identifies this worker in logs | ||
instance_key = uuid.uuid4().hex | ||
logger = logger.bind(worker_instance=instance_key) | ||
|
||
logger.info("Starting up worker") | ||
|
||
http_client = httpx.AsyncClient() | ||
ctx["http_client"] = http_client | ||
|
||
ctx["logger"] = logger | ||
logger.info("Start up complete") | ||
await redis_dependency.initialize(config.redis_url) | ||
|
||
|
||
async def shutdown(ctx: Dict[Any, Any]) -> None: | ||
"""Runs during worker shut-down to resources.""" | ||
if "logger" in ctx.keys(): | ||
logger = ctx["logger"] | ||
else: | ||
logger = structlog.get_logger(__name__) | ||
logger.info("Running worker shutdown.") | ||
|
||
await redis_dependency.close() | ||
|
||
try: | ||
await ctx["http_client"].aclose() | ||
except Exception as e: | ||
logger.warning("Issue closing the http_client: %s", str(e)) | ||
|
||
logger.info("Worker shutdown complete.") | ||
|
||
|
||
class WorkerSettings: | ||
"""Configuration for a Times Square arq worker. | ||
See `arq.worker.Worker` for details on these attributes. | ||
""" | ||
|
||
functions = [ | ||
ping, | ||
] | ||
|
||
redis_settings = config.arq_redis_settings | ||
|
||
on_startup = startup | ||
|
||
on_shutdown = shutdown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters