Skip to content

Commit da734c1

Browse files
committed
Merge branch 'worker-passthrough-extra-settings'
2 parents f927908 + 5230a44 commit da734c1

File tree

5 files changed

+101
-86
lines changed

5 files changed

+101
-86
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import warnings
2+
from typing import Any, Optional
3+
4+
from django.core.management.base import BaseCommand, CommandParser
5+
6+
from .utils import load_extra_settings
7+
from .constants import SETTING_NAME_PREFIX
8+
9+
10+
class CommandWithExtraSettings(BaseCommand):
11+
"""
12+
Base class for handling `--extra-settings`.
13+
14+
Derived classes must call `handle_extra_settings` at the top of their
15+
`handle` method. For example:
16+
17+
class Command(CommandWithExtraSettings):
18+
def handle(self, **options: Any) -> None:
19+
super().handle_extra_settings(**options)
20+
...
21+
"""
22+
23+
def add_arguments(self, parser: CommandParser) -> None:
24+
super().add_arguments(parser)
25+
26+
extra_settings_group = parser.add_mutually_exclusive_group()
27+
extra_settings_group.add_argument(
28+
'--config',
29+
action='store',
30+
default=None,
31+
help="The path to an additional django-style config file to load "
32+
"(this spelling is deprecated in favour of '--extra-settings')",
33+
)
34+
extra_settings_group.add_argument(
35+
'--extra-settings',
36+
action='store',
37+
default=None,
38+
help="The path to an additional django-style settings file to load. "
39+
f"{SETTING_NAME_PREFIX}* settings discovered in this file will "
40+
"override those from the default Django settings.",
41+
)
42+
43+
def handle_extra_settings(
44+
self,
45+
*,
46+
config: Optional[str] = None,
47+
extra_settings: Optional[str],
48+
**_: Any
49+
) -> Optional[str]:
50+
"""
51+
Load extra settings if there are any.
52+
53+
Returns the filename (if any) of the extra settings that have been loaded.
54+
"""
55+
56+
if config is not None:
57+
warnings.warn(
58+
"Use of '--config' is deprecated in favour of '--extra-settings'.",
59+
category=DeprecationWarning,
60+
)
61+
extra_settings = config
62+
63+
# Configuration overrides
64+
if extra_settings is not None:
65+
load_extra_settings(extra_settings)
66+
67+
return extra_settings

django_lightweight_queue/management/commands/queue_configuration.py

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,14 @@
1-
import warnings
21
from typing import Any
32

4-
from django.core.management.base import BaseCommand, CommandParser
5-
63
from ... import app_settings
7-
from ...utils import get_backend, get_queue_counts, load_extra_settings
8-
from ...constants import SETTING_NAME_PREFIX
4+
from ...utils import get_backend, get_queue_counts
5+
from ...command_utils import CommandWithExtraSettings
96
from ...cron_scheduler import get_cron_config
107

118

12-
class Command(BaseCommand):
13-
def add_arguments(self, parser: CommandParser) -> None:
14-
extra_settings_group = parser.add_mutually_exclusive_group()
15-
extra_settings_group.add_argument(
16-
'--config',
17-
action='store',
18-
default=None,
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.",
29-
)
30-
9+
class Command(CommandWithExtraSettings):
3110
def handle(self, **options: Any) -> None:
32-
extra_config = options.pop('config')
33-
if extra_config is not None:
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)
11+
super().handle_extra_settings(**options)
4412

4513
print("django-lightweight-queue")
4614
print("========================")

django_lightweight_queue/management/commands/queue_runner.py

Lines changed: 16 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,21 @@
1-
import warnings
21
from typing import Any, Dict, Optional
32

43
import daemonize
54

65
from django.apps import apps
7-
from django.core.management.base import (
8-
BaseCommand,
9-
CommandError,
10-
CommandParser,
11-
)
6+
from django.core.management.base import CommandError, CommandParser
127

138
from ...types import QueueName
14-
from ...utils import (
15-
get_logger,
16-
get_backend,
17-
get_middleware,
18-
load_extra_settings,
19-
)
9+
from ...utils import get_logger, get_backend, get_middleware
2010
from ...runner import runner
21-
from ...constants import SETTING_NAME_PREFIX
11+
from ...command_utils import CommandWithExtraSettings
2212
from ...machine_types import Machine, PooledMachine, DirectlyConfiguredMachine
2313

2414

25-
class Command(BaseCommand):
15+
class Command(CommandWithExtraSettings):
2616
def add_arguments(self, parser: CommandParser) -> None:
17+
super().add_arguments(parser)
18+
2719
parser.add_argument(
2820
'--pidfile',
2921
action='store',
@@ -58,22 +50,6 @@ def add_arguments(self, parser: CommandParser) -> None:
5850
default=None,
5951
help="Only run the given queue, useful for local debugging",
6052
)
61-
extra_settings_group = parser.add_mutually_exclusive_group()
62-
extra_settings_group.add_argument(
63-
'--config',
64-
action='store',
65-
default=None,
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.",
76-
)
7753
parser.add_argument(
7854
'--exact-configuration',
7955
action='store_true',
@@ -83,19 +59,11 @@ def add_arguments(self, parser: CommandParser) -> None:
8359
" '--of'.",
8460
)
8561

86-
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-
62+
def validate_and_normalise(self, options: Dict[str, Any], had_extra_settings: bool) -> None:
9563
if options['exact_configuration']:
96-
if not options['extra_settings']:
64+
if not had_extra_settings:
9765
raise CommandError(
98-
"Must provide a value for '--config' when using "
66+
"Must provide a value for '--extra-settings' when using "
9967
"'--exact-configuration'.",
10068
)
10169

@@ -127,19 +95,19 @@ def validate_and_normalise(self, options: Dict[str, Any]) -> None:
12795
def handle(self, **options: Any) -> None:
12896
logger = get_logger('dlq.master')
12997

130-
self.validate_and_normalise(options)
98+
extra_settings = super().handle_extra_settings(**options)
99+
100+
self.validate_and_normalise(
101+
options,
102+
had_extra_settings=extra_settings is not None,
103+
)
131104

132105
def touch_filename(name: str) -> Optional[str]:
133106
try:
134107
return options['touchfile'] % name
135108
except TypeError:
136109
return None
137110

138-
# Configuration overrides
139-
extra_config = options['extra_settings']
140-
if extra_config is not None:
141-
load_extra_settings(extra_config)
142-
143111
logger.info("Starting queue master")
144112

145113
# Ensure children will be able to import our backend
@@ -164,7 +132,7 @@ def touch_filename(name: str) -> Optional[str]:
164132
)
165133

166134
def run() -> None:
167-
runner(touch_filename, machine, logger)
135+
runner(touch_filename, machine, logger, extra_settings)
168136

169137
# fork() only after we have started enough to catch failure, including
170138
# being able to write to our pidfile.

django_lightweight_queue/management/commands/queue_worker.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
from typing import Any
22

3-
from django.core.management.base import BaseCommand, CommandParser
3+
from django.core.management.base import CommandParser
44

55
from ...types import QueueName, WorkerNumber
66
from ...worker import Worker
7+
from ...command_utils import CommandWithExtraSettings
78

89

9-
class Command(BaseCommand):
10+
class Command(CommandWithExtraSettings):
1011
help = "Run an individual queue worker" # noqa:A003 # inherited name
1112

1213
def add_arguments(self, parser: CommandParser) -> None:
14+
super().add_arguments(parser)
15+
1316
parser.add_argument(
1417
'queue',
1518
help="queue for which this is a worker",
@@ -40,6 +43,8 @@ def handle(
4043
touch_filename: str,
4144
**options: Any
4245
) -> None:
46+
super().handle_extra_settings(**options)
47+
4348
worker = Worker(
4449
queue=queue,
4550
worker_num=number,

django_lightweight_queue/runner.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ def runner(
2020
touch_filename_fn: Callable[[QueueName], Optional[str]],
2121
machine: Machine,
2222
logger: Logger,
23+
extra_settings_filename: Optional[str],
2324
) -> None:
2425
set_process_title("Master process")
2526

@@ -117,6 +118,12 @@ def handle_term(signum: int, stack: object) -> None:
117118
touch_filename,
118119
])
119120

121+
if extra_settings_filename is not None:
122+
args.extend([
123+
'--extra-settings',
124+
extra_settings_filename,
125+
])
126+
120127
worker = subprocess.Popen(args)
121128
workers[(queue, worker_num)] = (worker, worker_name)
122129

0 commit comments

Comments
 (0)