Skip to content

Commit 9866b6b

Browse files
lpthong90auvipy
authored andcommitted
Fix bug when run command celery (#569)
* fix bug when run command `celery` * refactor get preload_options from base * fix failed by flask8 * fix tests * remove redundant * fix typo
1 parent 367b530 commit 9866b6b

File tree

11 files changed

+40
-39
lines changed

11 files changed

+40
-39
lines changed

djcelery/db.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ def commit_on_success(*args, **kwargs):
2727
transaction.managed(True, *args, **kwargs)
2828
try:
2929
yield
30-
except:
30+
except Exception:
3131
if transaction.is_dirty(*args, **kwargs):
3232
transaction.rollback(*args, **kwargs)
3333
raise
3434
else:
3535
if transaction.is_dirty(*args, **kwargs):
3636
try:
3737
transaction.commit(*args, **kwargs)
38-
except:
38+
except Exception:
3939
transaction.rollback(*args, **kwargs)
4040
raise
4141
finally:

djcelery/loaders.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,8 @@ def read_configuration(self):
5959
self.configured = True
6060
# Default backend needs to be the database backend for backward
6161
# compatibility.
62-
backend = (getattr(settings, 'CELERY_RESULT_BACKEND', None) or
63-
getattr(settings, 'CELERY_BACKEND', None))
62+
backend = getattr(settings, 'CELERY_RESULT_BACKEND', None)
63+
backend = backend or getattr(settings, 'CELERY_BACKEND', None)
6464
if not backend:
6565
settings.CELERY_RESULT_BACKEND = 'database'
6666
return DictAttribute(settings)

djcelery/management/base.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,20 @@ def _init(self, *args, **kwargs):
3535
__old__init__(self, *args, **kwargs)
3636
self._thread_ident = _get_ident()
3737

38+
def _validate_thread(self):
39+
is_valid = self.allow_thread_sharing
40+
is_valid = is_valid or self._thread_ident == _get_ident()
41+
return is_valid
42+
3843
def _validate_thread_sharing(self):
39-
if (not self.allow_thread_sharing and
40-
self._thread_ident != _get_ident()):
44+
if not self.validate_thread():
4145
raise DatabaseError(
4246
DB_SHARED_THREAD % (
4347
self.alias, self._thread_ident, _get_ident()),
4448
)
4549

4650
BaseDatabaseWrapper.__init__ = _init
51+
BaseDatabaseWrapper.validate_thread = _validate_thread
4752
BaseDatabaseWrapper.validate_thread_sharing = \
4853
_validate_thread_sharing
4954

@@ -70,8 +75,8 @@ def add_arguments(self, parser):
7075
option = {k: v
7176
for k, v in opt.__dict__.items()
7277
if v is not None}
73-
flags = (option.get("_long_opts", []) +
74-
option.get("_short_opts", []))
78+
flags = option.get("_long_opts", [])
79+
flags += option.get("_short_opts", [])
7580
if option.get('default') == ('NO', 'DEFAULT'):
7681
option['default'] = None
7782
if option.get("nargs") == 1:

djcelery/management/commands/celery.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,8 @@ class Command(CeleryCommand):
1313
help = 'celery commands, see celery help'
1414
cc_options = CeleryCommand.options if CeleryCommand.options else []
1515
base_options = base.get_options() if base.get_options() else []
16-
if hasattr(base, "preload_options"):
17-
preload_options = base.preload_options if base.preload_options else []
18-
else:
19-
preload_options = []
20-
preload_options = base.preload_options if base.preload_options else []
21-
options = (cc_options +
22-
base_options +
23-
preload_options)
16+
preload_options = getattr(base, 'preload_options', []) or []
17+
options = cc_options + base_options + preload_options
2418

2519
def run_from_argv(self, argv):
2620
argv = self.handle_default_options(argv)

djcelery/management/commands/celerybeat.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
class Command(CeleryCommand):
1717
"""Run the celery periodic task scheduler."""
18-
options = (CeleryCommand.options +
19-
beat.get_options() +
20-
beat.preload_options)
2118
help = 'Old alias to the "celery beat" command.'
19+
options = CeleryCommand.options
20+
options += beat.get_options()
21+
options += beat.preload_options
2222

2323
def handle(self, *args, **options):
2424
beat.run(*args, **options)

djcelery/management/commands/celerycam.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515

1616
class Command(CeleryCommand):
1717
"""Run the celery curses event viewer."""
18-
options = (CeleryCommand.options +
19-
ev.get_options() +
20-
ev.preload_options)
2118
help = 'Takes snapshots of the clusters state to the database.'
19+
options = CeleryCommand.options
20+
options += ev.get_options()
21+
options += ev.preload_options
2222

2323
def handle(self, *args, **options):
2424
"""Handle the management command."""

djcelery/management/commands/celeryd.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
class Command(CeleryCommand):
1717
"""Run the celery daemon."""
1818
help = 'Old alias to the "celery worker" command.'
19-
options = (CeleryCommand.options +
20-
worker.get_options() +
21-
worker.preload_options)
19+
options = CeleryCommand.options
20+
options += worker.get_options()
21+
options += worker.preload_options
2222

2323
def handle(self, *args, **options):
2424
worker.check_args(args)

djcelery/management/commands/celerymon.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030

3131
class Command(CeleryCommand):
3232
"""Run the celery monitor."""
33-
options = (CeleryCommand.options +
34-
(mon and mon.get_options() + mon.preload_options or ()))
3533
help = 'Run the celery monitor'
34+
options = CeleryCommand.options
35+
options += (mon and mon.get_options() + mon.preload_options or ())
3636

3737
def handle(self, *args, **options):
3838
"""Handle the management command."""

djcelery/management/commands/djcelerymon.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ def run(self):
3232
class Command(CeleryCommand):
3333
"""Run the celery curses event viewer."""
3434
args = '[optional port number, or ipaddr:port]'
35-
options = (runserver.Command.option_list +
36-
ev.get_options() +
37-
ev.preload_options)
35+
options = runserver.Command.option_list
36+
options += ev.get_options()
37+
options += ev.preload_options
38+
3839
help = 'Starts Django Admin instance and celerycam in the same process.'
3940
# see http://code.djangoproject.com/changeset/13319.
4041
stdout, stderr = sys.stdout, sys.stderr

djcelery/schedulers.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,11 @@ def __init__(self, *args, **kwargs):
159159
self._dirty = set()
160160
self._finalize = Finalize(self, self.sync, exitpriority=5)
161161
Scheduler.__init__(self, *args, **kwargs)
162-
self.max_interval = (
163-
kwargs.get('max_interval') or
164-
self.app.conf.CELERYBEAT_MAX_LOOP_INTERVAL or
165-
DEFAULT_MAX_INTERVAL)
162+
self.max_interval = kwargs.get('max_interval')
163+
if self.max_interval is None:
164+
self.max_interval = self.app.conf.CELERYBEAT_MAX_LOOP_INTERVAL
165+
if self.max_interval is None:
166+
self.max_interval = DEFAULT_MAX_INTERVAL
166167

167168
def setup_schedule(self):
168169
self.install_default_entries(self.schedule)

0 commit comments

Comments
 (0)