Skip to content

Commit a401c57

Browse files
committed
Merge branch 'rename-extra-config'
2 parents 466d089 + 602ce6b commit a401c57

File tree

8 files changed

+78
-29
lines changed

8 files changed

+78
-29
lines changed

README.md

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ LIGHTWEIGHT_QUEUE_REDIS_PORT = 12345
6969
and then running:
7070

7171
```
72-
$ python manage.py queue_runner --config=special.py
72+
$ python manage.py queue_runner --extra-settings=special.py
7373
```
7474

7575
will result in the runner to use the settings from the specified configuration
@@ -149,10 +149,13 @@ part of a pool:
149149
$ python manage.py queue_runner --machine 2 --of 4
150150
```
151151

152-
Alternatively a runner can be told explicitly which configuration to use:
152+
Alternatively a runner can be told explicitly how to behave by having
153+
extra settings loaded (any `LIGHTWEIGHT_QUEUE_*` constants found in the file
154+
will replace equivalent django settings) and being configured to run exactly as
155+
the settings describe:
153156

154157
```
155-
$ python manage.py queue_runner --exact-configuration --config=special.py
158+
$ python manage.py queue_runner --exact-configuration --extra-settings=special.py
156159
```
157160

158161
When using `--exact-configuration` the number of workers is configured exactly,
@@ -188,7 +191,7 @@ $ python manage.py queue_runner --machine 3 --of 3
188191
will result in one worker for `queue1` on the current machine, while:
189192

190193
```
191-
$ python manage.py queue_runner --exact-configuration --config=special.py
194+
$ python manage.py queue_runner --exact-configuration --extra-settings=special.py
192195
```
193196

194197
will result in two workers on the current machine.

django_lightweight_queue/management/commands/queue_configuration.py

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,46 @@
1+
import warnings
12
from typing import Any
23

34
from django.core.management.base import BaseCommand, CommandParser
45

56
from ... import app_settings
6-
from ...utils import get_backend, get_queue_counts, load_extra_config
7+
from ...utils import get_backend, get_queue_counts, load_extra_settings
8+
from ...constants import SETTING_NAME_PREFIX
79
from ...cron_scheduler import get_cron_config
810

911

1012
class Command(BaseCommand):
1113
def add_arguments(self, parser: CommandParser) -> None:
12-
parser.add_argument(
14+
extra_settings_group = parser.add_mutually_exclusive_group()
15+
extra_settings_group.add_argument(
1316
'--config',
1417
action='store',
1518
default=None,
16-
help="The path to an additional django-style config file to load",
19+
help="The path to an additional django-style config file to load "
20+
"(this spelling is deprecated in favour of '--extra-settings')",
21+
)
22+
extra_settings_group.add_argument(
23+
'--extra-settings',
24+
action='store',
25+
default=None,
26+
help="The path to an additional django-style settings file to load. "
27+
f"{SETTING_NAME_PREFIX}* settings discovered in this file will "
28+
"override those from the default Django settings.",
1729
)
1830

1931
def handle(self, **options: Any) -> None:
20-
# Configuration overrides
21-
extra_config = options['config']
32+
extra_config = options.pop('config')
2233
if extra_config is not None:
23-
load_extra_config(extra_config)
34+
warnings.warn(
35+
"Use of '--config' is deprecated in favour of '--extra-settings'.",
36+
category=DeprecationWarning,
37+
)
38+
options['extra_settings'] = extra_config
39+
40+
# Configuration overrides
41+
extra_settings = options['extra_settings']
42+
if extra_settings is not None:
43+
load_extra_settings(extra_settings)
2444

2545
print("django-lightweight-queue")
2646
print("========================")

django_lightweight_queue/management/commands/queue_runner.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import warnings
12
from typing import Any, Dict, Optional
23

34
import daemonize
@@ -10,8 +11,14 @@
1011
)
1112

1213
from ...types import QueueName
13-
from ...utils import get_logger, get_backend, get_middleware, load_extra_config
14+
from ...utils import (
15+
get_logger,
16+
get_backend,
17+
get_middleware,
18+
load_extra_settings,
19+
)
1420
from ...runner import runner
21+
from ...constants import SETTING_NAME_PREFIX
1522
from ...machine_types import Machine, PooledMachine, DirectlyConfiguredMachine
1623

1724

@@ -51,23 +58,42 @@ def add_arguments(self, parser: CommandParser) -> None:
5158
default=None,
5259
help="Only run the given queue, useful for local debugging",
5360
)
54-
parser.add_argument(
61+
extra_settings_group = parser.add_mutually_exclusive_group()
62+
extra_settings_group.add_argument(
5563
'--config',
5664
action='store',
5765
default=None,
58-
help="The path to an additional django-style config file to load",
66+
help="The path to an additional django-style config file to load "
67+
"(this spelling is deprecated in favour of '--extra-settings')",
68+
)
69+
extra_settings_group.add_argument(
70+
'--extra-settings',
71+
action='store',
72+
default=None,
73+
help="The path to an additional django-style settings file to load. "
74+
f"{SETTING_NAME_PREFIX}* settings discovered in this file will "
75+
"override those from the default Django settings.",
5976
)
6077
parser.add_argument(
6178
'--exact-configuration',
6279
action='store_true',
6380
help="Run queues on this machine exactly as specified. Requires the"
64-
" use of the '--config' option in addition. It is an error to"
65-
" use this option together with either '--machine' or '--of'.",
81+
" use of the '--extra-settings' option in addition. It is an"
82+
" error to use this option together with either '--machine' or"
83+
" '--of'.",
6684
)
6785

6886
def validate_and_normalise(self, options: Dict[str, Any]) -> None:
87+
extra_config = options.pop('config')
88+
if extra_config is not None:
89+
warnings.warn(
90+
"Use of '--config' is deprecated in favour of '--extra-settings'.",
91+
category=DeprecationWarning,
92+
)
93+
options['extra_settings'] = extra_config
94+
6995
if options['exact_configuration']:
70-
if not options['config']:
96+
if not options['extra_settings']:
7197
raise CommandError(
7298
"Must provide a value for '--config' when using "
7399
"'--exact-configuration'.",
@@ -110,9 +136,9 @@ def touch_filename(name: str) -> Optional[str]:
110136
return None
111137

112138
# Configuration overrides
113-
extra_config = options['config']
139+
extra_config = options['extra_settings']
114140
if extra_config is not None:
115-
load_extra_config(extra_config)
141+
load_extra_settings(extra_config)
116142

117143
logger.info("Starting queue master")
118144

django_lightweight_queue/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
FIVE_SECONDS = datetime.timedelta(seconds=5)
3333

3434

35-
def load_extra_config(file_path: str) -> None:
35+
def load_extra_settings(file_path: str) -> None:
3636
# Based on https://docs.python.org/3/library/importlib.html#importing-a-source-file-directly
3737
spec = importlib.util.spec_from_file_location('extra_settings', file_path)
3838
extra_settings = importlib.util.module_from_spec(spec) # type: ignore[arg-type]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from django_lightweight_queue.types import QueueName
22

33
LIGHTWEIGHT_QUEUE_BACKEND_OVERRIDES = {
4-
QueueName('test-queue'): 'tests.test_extra_config.TestBackend',
4+
QueueName('test-queue'): 'tests.test_extra_settings.TestBackend',
55
}
66

77
LIGHTWEIGHT_QUEUE_REDIS_PASSWORD = 'a very bad password'
File renamed without changes.
File renamed without changes.

tests/test_extra_config.py renamed to tests/test_extra_settings.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from django_lightweight_queue import app_settings
88
from django_lightweight_queue.job import Job
99
from django_lightweight_queue.types import QueueName, WorkerNumber
10-
from django_lightweight_queue.utils import get_backend, load_extra_config
10+
from django_lightweight_queue.utils import get_backend, load_extra_settings
1111
from django_lightweight_queue.backends.base import BaseBackend
1212

1313
TESTS_DIR = Path(__file__).parent
@@ -24,7 +24,7 @@ def length(self, queue: QueueName) -> int:
2424
pass
2525

2626

27-
class ExtraConfigTests(SimpleTestCase):
27+
class ExtraSettingsTests(SimpleTestCase):
2828
def setUp(self) -> None:
2929
get_backend.cache_clear()
3030
super().setUp()
@@ -34,8 +34,8 @@ def tearDown(self) -> None:
3434
get_backend.cache_clear()
3535
super().tearDown()
3636

37-
def test_updates_configuration(self) -> None:
38-
load_extra_config(str(TESTS_DIR / '_demo_extra_config.py'))
37+
def test_updates_settings(self) -> None:
38+
load_extra_settings(str(TESTS_DIR / '_demo_extra_settings.py'))
3939

4040
backend = get_backend('test-queue')
4141
self.assertIsInstance(backend, TestBackend)
@@ -44,17 +44,17 @@ def test_updates_configuration(self) -> None:
4444

4545
def test_warns_about_unexpected_settings(self) -> None:
4646
with self.assertWarnsRegex(Warning, r'Ignoring unexpected setting.+\bNOT_REDIS_PASSWORD\b'):
47-
load_extra_config(str(TESTS_DIR / '_demo_extra_config_unexpected.py'))
47+
load_extra_settings(str(TESTS_DIR / '_demo_extra_settings_unexpected.py'))
4848

4949
self.assertEqual('expected', app_settings.REDIS_PASSWORD)
5050

51-
def test_updates_configuration_with_falsey_values(self) -> None:
52-
load_extra_config(str(TESTS_DIR / '_demo_extra_config.py'))
53-
load_extra_config(str(TESTS_DIR / '_demo_extra_config_falsey.py'))
51+
def test_updates_settings_with_falsey_values(self) -> None:
52+
load_extra_settings(str(TESTS_DIR / '_demo_extra_settings.py'))
53+
load_extra_settings(str(TESTS_DIR / '_demo_extra_settings_falsey.py'))
5454

5555
self.assertIsNone(app_settings.REDIS_PASSWORD)
5656
self.assertFalse(app_settings.ATOMIC_JOBS)
5757

5858
def test_rejects_missing_file(self) -> None:
5959
with self.assertRaises(FileNotFoundError):
60-
load_extra_config(str(TESTS_DIR / '_no_such_file.py'))
60+
load_extra_settings(str(TESTS_DIR / '_no_such_file.py'))

0 commit comments

Comments
 (0)