Skip to content

Commit 4fad907

Browse files
author
Omer Katz
authored
Reintroduce support for custom preload options (celery#6516)
* Restore preload options. Fixes celery#6307. * Document breaking changes for preload options in 5.0. Fixes celery#6379.
1 parent 0fa4db8 commit 4fad907

File tree

18 files changed

+74
-28
lines changed

18 files changed

+74
-28
lines changed

celery/bin/amqp.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
__all__ = ('amqp',)
1010

11+
from celery.bin.base import handle_preload_options
12+
1113

1214
def dump_message(message):
1315
if message is None:
@@ -54,6 +56,7 @@ def reconnect(self):
5456

5557
@click.group(invoke_without_command=True)
5658
@click.pass_context
59+
@handle_preload_options
5760
def amqp(ctx):
5861
"""AMQP Administration Shell.
5962

celery/bin/base.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Click customizations for Celery."""
22
import json
33
from collections import OrderedDict
4+
from functools import update_wrapper
45
from pprint import pformat
56

67
import click
78
from click import ParamType
89
from kombu.utils.objects import cached_property
910

1011
from celery._state import get_current_app
12+
from celery.signals import user_preload_options
1113
from celery.utils import text
1214
from celery.utils.log import mlevel
1315
from celery.utils.time import maybe_iso8601
@@ -113,6 +115,25 @@ def say_chat(self, direction, title, body='', show_body=False):
113115
self.echo(body)
114116

115117

118+
def handle_preload_options(f):
119+
def caller(ctx, *args, **kwargs):
120+
app = ctx.obj.app
121+
122+
preload_options = [o.name for o in app.user_options.get('preload', [])]
123+
124+
if preload_options:
125+
user_options = {
126+
preload_option: kwargs[preload_option]
127+
for preload_option in preload_options
128+
}
129+
130+
user_preload_options.send(sender=f, app=app, options=user_options)
131+
132+
return f(ctx, *args, **kwargs)
133+
134+
return update_wrapper(caller, f)
135+
136+
116137
class CeleryOption(click.Option):
117138
"""Customized option for Celery."""
118139

celery/bin/beat.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33

44
import click
55

6-
from celery.bin.base import LOG_LEVEL, CeleryDaemonCommand, CeleryOption
6+
from celery.bin.base import (LOG_LEVEL, CeleryDaemonCommand, CeleryOption,
7+
handle_preload_options)
78
from celery.platforms import detached, maybe_drop_privileges
89

910

@@ -43,6 +44,7 @@
4344
help_group="Beat Options",
4445
help="Logging level.")
4546
@click.pass_context
47+
@handle_preload_options
4648
def beat(ctx, detach=False, logfile=None, pidfile=None, uid=None,
4749
gid=None, umask=None, workdir=None, **kwargs):
4850
"""Start the beat periodic task scheduler."""

celery/bin/call.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
import click
33

44
from celery.bin.base import (ISO8601, ISO8601_OR_FLOAT, JSON, CeleryCommand,
5-
CeleryOption)
5+
CeleryOption, handle_preload_options)
66

77

8+
@click.command(cls=CeleryCommand)
89
@click.argument('name')
910
@click.option('-a',
1011
'--args',
@@ -52,8 +53,8 @@
5253
cls=CeleryOption,
5354
help_group="Routing Options",
5455
help="custom routing key.")
55-
@click.command(cls=CeleryCommand)
5656
@click.pass_context
57+
@handle_preload_options
5758
def call(ctx, name, args, kwargs, eta, countdown, expires, serializer, queue, exchange, routing_key):
5859
"""Call a task by name."""
5960
task_id = ctx.obj.app.send_task(

celery/bin/celery.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,9 @@ def celery(ctx, app, broker, result_backend, loader, config, workdir,
145145
beat.params.extend(ctx.obj.app.user_options.get('beat', []))
146146
events.params.extend(ctx.obj.app.user_options.get('events', []))
147147

148+
for command in celery.commands.values():
149+
command.params.extend(ctx.obj.app.user_options.get('preload', []))
150+
148151

149152
@celery.command(cls=CeleryCommand)
150153
@click.pass_context

celery/bin/control.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44
import click
55
from kombu.utils.json import dumps
66

7-
from celery.bin.base import COMMA_SEPARATED_LIST, CeleryCommand, CeleryOption
7+
from celery.bin.base import (COMMA_SEPARATED_LIST, CeleryCommand,
8+
CeleryOption, handle_preload_options)
89
from celery.platforms import EX_UNAVAILABLE
910
from celery.utils import text
1011
from celery.worker.control import Panel
@@ -71,6 +72,7 @@ def _compile_arguments(action, args):
7172
help_group='Remote Control Options',
7273
help='Use json as output format.')
7374
@click.pass_context
75+
@handle_preload_options
7476
def status(ctx, timeout, destination, json, **kwargs):
7577
"""Show list of workers that are online."""
7678
callback = None if json else partial(_say_remote_command_reply, ctx)
@@ -115,6 +117,7 @@ def status(ctx, timeout, destination, json, **kwargs):
115117
help_group='Remote Control Options',
116118
help='Use json as output format.')
117119
@click.pass_context
120+
@handle_preload_options
118121
def inspect(ctx, action, timeout, destination, json, **kwargs):
119122
"""Inspect the worker at runtime.
120123
@@ -164,6 +167,7 @@ def inspect(ctx, action, timeout, destination, json, **kwargs):
164167
help_group='Remote Control Options',
165168
help='Use json as output format.')
166169
@click.pass_context
170+
@handle_preload_options
167171
def control(ctx, action, timeout, destination, json):
168172
"""Workers remote control.
169173

celery/bin/events.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
import click
66

7-
from celery.bin.base import LOG_LEVEL, CeleryDaemonCommand, CeleryOption
7+
from celery.bin.base import (LOG_LEVEL, CeleryDaemonCommand, CeleryOption,
8+
handle_preload_options)
89
from celery.platforms import detached, set_process_title, strargv
910

1011

@@ -47,6 +48,7 @@ def _run_evtop(app):
4748
raise click.UsageError("The curses module is required for this command.")
4849

4950

51+
@handle_preload_options
5052
@click.command(cls=CeleryDaemonCommand)
5153
@click.option('-d',
5254
'--dump',
@@ -78,6 +80,7 @@ def _run_evtop(app):
7880
help_group="Snapshot",
7981
help="Logging level.")
8082
@click.pass_context
83+
@handle_preload_options
8184
def events(ctx, dump, camera, detach, frequency, maxrate, loglevel, **kwargs):
8285
"""Event-stream utilities."""
8386
app = ctx.obj.app

celery/bin/graph.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
import click
66

7-
from celery.bin.base import CeleryCommand
7+
from celery.bin.base import CeleryCommand, handle_preload_options
88
from celery.utils.graph import DependencyGraph, GraphFormatter
99

1010

1111
@click.group()
12+
@handle_preload_options
1213
def graph():
1314
"""The ``celery graph`` command."""
1415

celery/bin/list.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""The ``celery list bindings`` command, used to inspect queue bindings."""
22
import click
33

4-
from celery.bin.base import CeleryCommand
4+
from celery.bin.base import CeleryCommand, handle_preload_options
55

66

77
@click.group(name="list")
8+
@handle_preload_options
89
def list_():
910
"""Get info from broker.
1011

celery/bin/logtool.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
import click
77

8-
from celery.bin.base import CeleryCommand
8+
from celery.bin.base import CeleryCommand, handle_preload_options
99

1010
__all__ = ('logtool',)
1111

@@ -111,6 +111,7 @@ def report(self):
111111

112112

113113
@click.group()
114+
@handle_preload_options
114115
def logtool():
115116
"""The ``celery logtool`` command."""
116117

0 commit comments

Comments
 (0)