-
Notifications
You must be signed in to change notification settings - Fork 6
Expand Settings to handle different Django Architectures #62
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
Closed
Closed
Changes from 3 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
8d396f2
resolve conflicts
itsthejoker c00635f
make all settings changeable at runtime
itsthejoker 3b9e3a6
make all settings changeable at runtime, try 2
itsthejoker 737f80d
fix missing type hint and revert name to app_settings
itsthejoker 0b202ac
revert version number update
itsthejoker 97b36d4
run isort
itsthejoker 76631b4
move readme changes to own PR
itsthejoker 73cfefe
appease mypy with appropriate blood-based rituals
itsthejoker 6ee7218
revert mock to use .patch.dict()
itsthejoker c9c5b84
Refactor for layer approach
itsthejoker fc48298
add type hints for getattr
itsthejoker ecbcad3
fix support for old python
itsthejoker 05ebae5
refactor shortening correctly
itsthejoker d29f36a
re-add accidentally-dropped comment
itsthejoker af4eb29
Update django_lightweight_queue/app_settings.py
itsthejoker 496828a
Update django_lightweight_queue/app_settings.py
itsthejoker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,52 +1,177 @@ | ||
from typing import Dict, Union, Mapping, TypeVar, Callable, Optional, Sequence | ||
from typing import Union, Mapping, TypeVar, Callable, Optional, Sequence | ||
|
||
from django.conf import settings | ||
from django.conf import settings as django_settings | ||
|
||
from . import constants | ||
from .types import Logger, QueueName | ||
|
||
T = TypeVar('T') | ||
|
||
|
||
def setting(suffix: str, default: T) -> T: | ||
attr_name = '{}{}'.format(constants.SETTING_NAME_PREFIX, suffix) | ||
return getattr(settings, attr_name, default) | ||
class Settings(): | ||
itsthejoker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
def _get(self, suffix: str, default: T) -> T: | ||
attr_name = '{}{}'.format(constants.SETTING_NAME_PREFIX, suffix) | ||
return getattr(django_settings, attr_name, default) | ||
|
||
# adjustable values at runtime | ||
_workers = None | ||
_backend = None | ||
_logger_factory = None | ||
_backend_overrides = None | ||
_middleware = None | ||
_ignore_apps = None | ||
_redis_host = None | ||
_redis_port = None | ||
_redis_password = None | ||
_redis_prefix = None | ||
_enable_prometheus = None | ||
_prometheus_start_port = None | ||
_atomic_jobs = None | ||
|
||
WORKERS = setting('WORKERS', {}) # type: Dict[QueueName, int] | ||
BACKEND = setting( | ||
'BACKEND', | ||
'django_lightweight_queue.backends.synchronous.SynchronousBackend', | ||
) # type: str | ||
@property | ||
def WORKERS(self): | ||
itsthejoker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
if not self._workers: | ||
itsthejoker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
self._workers = self._get('WORKERS', {}) | ||
return self._workers | ||
|
||
LOGGER_FACTORY = setting( | ||
'LOGGER_FACTORY', | ||
'logging.getLogger', | ||
) # type: Union[str, Callable[[str], Logger]] | ||
@WORKERS.setter | ||
def WORKERS(self, value): | ||
self._workers = value | ||
|
||
# Allow per-queue overrides of the backend. | ||
BACKEND_OVERRIDES = setting('BACKEND_OVERRIDES', {}) # type: Mapping[QueueName, str] | ||
@property | ||
def BACKEND(self): | ||
if not self._backend: | ||
self._backend = self._get( | ||
'BACKEND', | ||
'django_lightweight_queue.backends.synchronous.SynchronousBackend', | ||
) | ||
return self._backend # type: str | ||
|
||
MIDDLEWARE = setting('MIDDLEWARE', ( | ||
'django_lightweight_queue.middleware.logging.LoggingMiddleware', | ||
'django_lightweight_queue.middleware.transaction.TransactionMiddleware', | ||
)) # type: Sequence[str] | ||
@BACKEND.setter | ||
def BACKEND(self, value): | ||
self._backend = value | ||
|
||
# Apps to ignore when looking for tasks. Apps must be specified as the dotted | ||
# name used in `INSTALLED_APPS`. This is expected to be useful when you need to | ||
# have a file called `tasks.py` within an app, but don't want | ||
# django-lightweight-queue to import that file. | ||
# Note: this _doesn't_ prevent tasks being registered from these apps. | ||
IGNORE_APPS = setting('IGNORE_APPS', ()) # type: Sequence[str] | ||
@property | ||
def LOGGER_FACTORY(self): | ||
if not self._logger_factory: | ||
self._logger_factory = self._get( | ||
'LOGGER_FACTORY', | ||
'logging.getLogger', | ||
) | ||
return self._logger_factory # type: Union[str, Callable[[str], Logger]] | ||
|
||
# Backend-specific settings | ||
REDIS_HOST = setting('REDIS_HOST', '127.0.0.1') # type: str | ||
REDIS_PORT = setting('REDIS_PORT', 6379) # type: int | ||
REDIS_PASSWORD = setting('REDIS_PASSWORD', None) # type: Optional[str] | ||
REDIS_PREFIX = setting('REDIS_PREFIX', '') # type: str | ||
@LOGGER_FACTORY.setter | ||
def LOGGER_FACTORY(self, value): | ||
self._logger_factory = value | ||
|
||
ENABLE_PROMETHEUS = setting('ENABLE_PROMETHEUS', False) # type: bool | ||
# Workers will export metrics on this port, and ports following it | ||
PROMETHEUS_START_PORT = setting('PROMETHEUS_START_PORT', 9300) # type: int | ||
@property | ||
def BACKEND_OVERRIDES(self): | ||
# Allow per-queue overrides of the backend. | ||
if not self._backend_overrides: | ||
self._backend_overrides = self._get('BACKEND_OVERRIDES', {}) | ||
return self._backend_overrides # type: Mapping[QueueName, str] | ||
|
||
ATOMIC_JOBS = setting('ATOMIC_JOBS', True) # type: bool | ||
@BACKEND_OVERRIDES.setter | ||
def BACKEND_OVERRIDES(self, value): | ||
self._backend_overrides = value | ||
|
||
@property | ||
def MIDDLEWARE(self): | ||
if not self._middleware: | ||
self._middleware = self._get('MIDDLEWARE', ( | ||
'django_lightweight_queue.middleware.logging.LoggingMiddleware', | ||
)) | ||
return self._middleware # type: Sequence[str] | ||
|
||
@MIDDLEWARE.setter | ||
def MIDDLEWARE(self, value): | ||
self._middleware = value | ||
|
||
@property | ||
def IGNORE_APPS(self): | ||
# Apps to ignore when looking for tasks. Apps must be specified as the dotted | ||
# name used in `INSTALLED_APPS`. This is expected to be useful when you need to | ||
# have a file called `tasks.py` within an app, but don't want | ||
# django-lightweight-queue to import that file. | ||
# Note: this _doesn't_ prevent tasks being registered from these apps. | ||
if not self._ignore_apps: | ||
self._ignore_apps = self._get('IGNORE_APPS', ()) | ||
return self._ignore_apps # type: Sequence[str] | ||
|
||
@IGNORE_APPS.setter | ||
def IGNORE_APPS(self, value): | ||
self._ignore_apps = value | ||
|
||
@property | ||
def REDIS_HOST(self): | ||
if not self._redis_host: | ||
self._redis_host = self._get('REDIS_HOST', '127.0.0.1') | ||
return self._redis_host # type: str | ||
|
||
@REDIS_HOST.setter | ||
def REDIS_HOST(self, value): | ||
self._redis_host = value | ||
|
||
@property | ||
def REDIS_PORT(self): | ||
if not self._redis_port: | ||
self._redis_port = self._get('REDIS_PORT', 6379) | ||
return self._redis_port # type: int | ||
|
||
@REDIS_PORT.setter | ||
def REDIS_PORT(self, value): | ||
self._redis_port = value | ||
|
||
@property | ||
def REDIS_PASSWORD(self): | ||
if not self._redis_password: | ||
self._redis_password = self._get('REDIS_PASSWORD', None) | ||
return self._redis_password # type: Optional[str] | ||
|
||
@REDIS_PASSWORD.setter | ||
def REDIS_PASSWORD(self, value): | ||
self._redis_password = value | ||
|
||
@property | ||
def REDIS_PREFIX(self): | ||
if not self._redis_prefix: | ||
self._redis_prefix = self._get('REDIS_PREFIX', '') | ||
return self._redis_prefix # type: str | ||
|
||
@REDIS_PREFIX.setter | ||
def REDIS_PREFIX(self, value): | ||
self._redis_prefix = value | ||
|
||
@property | ||
def ENABLE_PROMETHEUS(self): | ||
if not self._enable_prometheus: | ||
self._enable_prometheus = self._get('ENABLE_PROMETHEUS', False) | ||
return self._enable_prometheus # type: bool | ||
|
||
@ENABLE_PROMETHEUS.setter | ||
def ENABLE_PROMETHEUS(self, value): | ||
self._enable_prometheus = value | ||
|
||
@property | ||
def PROMETHEUS_START_PORT(self): | ||
# Workers will export metrics on this port, and ports following it | ||
if not self._prometheus_start_port: | ||
self._prometheus_start_port = self._get('PROMETHEUS_START_PORT', 9300) | ||
return self._prometheus_start_port # type: int | ||
|
||
@PROMETHEUS_START_PORT.setter | ||
def PROMETHEUS_START_PORT(self, value): | ||
self._prometheus_start_port = value | ||
|
||
@property | ||
def ATOMIC_JOBS(self): | ||
if not self._atomic_jobs: | ||
self._atomic_jobs = self._get('ATOMIC_JOBS', True) | ||
return self._atomic_jobs # type: bool | ||
|
||
@ATOMIC_JOBS.setter | ||
def ATOMIC_JOBS(self, value): | ||
self._atomic_jobs = value | ||
|
||
|
||
settings = Settings() | ||
itsthejoker marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.