Skip to content

Commit

Permalink
Allow spec config with code
Browse files Browse the repository at this point in the history
  • Loading branch information
Wh1isper committed Sep 4, 2024
1 parent 9449991 commit edd7ad2
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 12 deletions.
2 changes: 0 additions & 2 deletions brq/configs.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
import os
import uuid
from contextlib import asynccontextmanager
from typing import Any, AsyncGenerator

import redis.asyncio as redis
from pydantic import BaseModel
from pydantic_settings import BaseSettings, SettingsConfigDict

from brq.tools import get_redis_client, get_redis_url
Expand Down
89 changes: 79 additions & 10 deletions brq/decorator.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,25 +65,94 @@ def task(
*,
config: BrqConfig | None = None,
register_function_name: str | None = None,
# Redis connection
redis_host: str | None = None,
redis_port: int | None = None,
redis_db: int | None = None,
redis_cluster: bool | None = None,
redis_tls: bool | None = None,
redis_username: str | None = None,
redis_password: str | None = None,
# Scope
redis_key_prefix: str | None = None,
redis_key_seperator: str | None = None,
# Consumer
group_name: str | None = None,
consumer_identifier: str | None = None,
count_per_fetch: int | None = None,
block_time: int | None = None,
expire_time: int | None = None,
process_timeout: int | None = None,
retry_lock_time: int | None = None,
retry_cooldown_time: int | None = None,
enable_enque_deferred_job: bool | None = None,
enable_reprocess_timeout_job: bool | None = None,
enable_dead_queue: bool | None = None,
max_message_len: int | None = None,
delete_message_after_process: bool | None = None,
run_parallel: bool | None = None,
# Daemon
daemon_concurrency: int | None = None,
):

def _wrapper(

Check warning on line 98 in brq/decorator.py

View check run for this annotation

Codecov / codecov/patch

brq/decorator.py#L98

Added line #L98 was not covered by tests
func,
config: BrqConfig | None = None,
register_function_name: str | None = None,
):

return BrqTaskWrapper(func, config, register_function_name)

Check warning on line 104 in brq/decorator.py

View check run for this annotation

Codecov / codecov/patch

brq/decorator.py#L104

Added line #L104 was not covered by tests

kwargs = {

Check warning on line 106 in brq/decorator.py

View check run for this annotation

Codecov / codecov/patch

brq/decorator.py#L106

Added line #L106 was not covered by tests
k: v
for k, v in {
"redis_host": redis_host,
"redis_port": redis_port,
"redis_db": redis_db,
"redis_cluster": redis_cluster,
"redis_tls": redis_tls,
"redis_username": redis_username,
"redis_password": redis_password,
"redis_key_prefix": redis_key_prefix,
"redis_key_seperator": redis_key_seperator,
"consumer_group_name": group_name,
"consumer_identifier": consumer_identifier,
"consumer_count_per_fetch": count_per_fetch,
"consumer_block_time": block_time,
"consumer_expire_time": expire_time,
"consumer_process_timeout": process_timeout,
"consumer_retry_lock_time": retry_lock_time,
"consumer_retry_cooldown_time": retry_cooldown_time,
"consumer_enable_enque_deferred_job": enable_enque_deferred_job,
"consumer_enable_reprocess_timeout_job": enable_reprocess_timeout_job,
"consumer_enable_dead_queue": enable_dead_queue,
"consumer_max_message_len": max_message_len,
"consumer_delete_message_after_process": delete_message_after_process,
"consumer_run_parallel": run_parallel,
"daemon_concurrency": daemon_concurrency,
}.items()
if v is not None
}

if not config:
logger.info("Initializing config from environment variables and .env file")
config = BrqConfig()

Check warning on line 139 in brq/decorator.py

View check run for this annotation

Codecov / codecov/patch

brq/decorator.py#L137-L139

Added lines #L137 - L139 were not covered by tests
else:
logger.info("Using custom config")
config = config

Check warning on line 142 in brq/decorator.py

View check run for this annotation

Codecov / codecov/patch

brq/decorator.py#L141-L142

Added lines #L141 - L142 were not covered by tests

def _wrapper(func, config, register_function_name):
if not config:
logger.info("Initializing config from environment variables and .env file")
config = BrqConfig()
else:
logger.info("Using custom config")
config = config

return BrqTaskWrapper(func, config, register_function_name)
config = BrqConfig.model_validate(

Check warning on line 144 in brq/decorator.py

View check run for this annotation

Codecov / codecov/patch

brq/decorator.py#L144

Added line #L144 was not covered by tests
{
**config.model_dump(),
**kwargs,
}
)

if _func is None:
return _wrapper
return partial(

Check warning on line 152 in brq/decorator.py

View check run for this annotation

Codecov / codecov/patch

brq/decorator.py#L151-L152

Added lines #L151 - L152 were not covered by tests
_wrapper,
config=config,
register_function_name=register_function_name,
)
else:
return _wrapper(_func, config=config, register_function_name=register_function_name)

Check warning on line 158 in brq/decorator.py

View check run for this annotation

Codecov / codecov/patch

brq/decorator.py#L158

Added line #L158 was not covered by tests

0 comments on commit edd7ad2

Please sign in to comment.