Skip to content

Commit 1bcf81b

Browse files
b1ngzOmer Katz
authored and
Omer Katz
committed
support task-priorities when use RabbitMQ / Redis as broker (#189)
* add priority field * fix ci failed * add unicode_literals import * priority default to None * Happify lint. * fix ci W504 error * flake8 ignore W503, W504
1 parent 215f232 commit 1bcf81b

File tree

7 files changed

+49
-18
lines changed

7 files changed

+49
-18
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Generated by Django 2.0.6 on 2018-10-22 05:20
2+
from __future__ import absolute_import, unicode_literals
3+
4+
import django.core.validators
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
dependencies = [
10+
('django_celery_beat', '0005_add_solarschedule_events_choices_squashed_0009_merge_20181012_1416'), # noqa
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name='periodictask',
16+
name='priority',
17+
field=models.PositiveIntegerField(
18+
blank=True,
19+
default=None,
20+
null=True,
21+
validators=[django.core.validators.MaxValueValidator(255)],
22+
verbose_name='priority'),
23+
),
24+
]

django_celery_beat/models.py

+5
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from celery import schedules
88
from celery.five import python_2_unicode_compatible
99
from django.core.exceptions import MultipleObjectsReturned, ValidationError
10+
from django.core.validators import MaxValueValidator
1011
from django.db import models
1112
from django.db.models import signals
1213
from django.utils.translation import ugettext_lazy as _
@@ -281,6 +282,10 @@ class PeriodicTask(models.Model):
281282
routing_key = models.CharField(
282283
_('routing key'), max_length=200, blank=True, null=True, default=None,
283284
)
285+
priority = models.PositiveIntegerField(
286+
_('priority'), default=None, validators=[MaxValueValidator(255)],
287+
blank=True, null=True
288+
)
284289
expires = models.DateTimeField(
285290
_('expires'), blank=True, null=True,
286291
)

django_celery_beat/schedulers.py

+7-5
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,8 @@ def __init__(self, model, app=None):
7777
self._disable(model)
7878

7979
self.options = {}
80-
for option in ['queue', 'exchange', 'routing_key', 'expires']:
80+
for option in ['queue', 'exchange', 'routing_key', 'expires',
81+
'priority']:
8182
value = getattr(model, option)
8283
if value is None:
8384
continue
@@ -170,11 +171,12 @@ def _unpack_fields(cls, schedule,
170171

171172
@classmethod
172173
def _unpack_options(cls, queue=None, exchange=None, routing_key=None,
173-
**kwargs):
174+
priority=None, **kwargs):
174175
return {
175176
'queue': queue,
176177
'exchange': exchange,
177178
'routing_key': routing_key,
179+
'priority': priority
178180
}
179181

180182
def __repr__(self):
@@ -202,9 +204,9 @@ def __init__(self, *args, **kwargs):
202204
Scheduler.__init__(self, *args, **kwargs)
203205
self._finalize = Finalize(self, self.sync, exitpriority=5)
204206
self.max_interval = (
205-
kwargs.get('max_interval') or
206-
self.app.conf.beat_max_loop_interval or
207-
DEFAULT_MAX_INTERVAL)
207+
kwargs.get('max_interval')
208+
or self.app.conf.beat_max_loop_interval
209+
or DEFAULT_MAX_INTERVAL)
208210

209211
def setup_schedule(self):
210212
self.install_default_entries(self.schedule)

django_celery_beat/tzcrontab.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ def __reduce__(self):
7070

7171
def __eq__(self, other):
7272
if isinstance(other, schedules.crontab):
73-
return (other.month_of_year == self.month_of_year and
74-
other.day_of_month == self.day_of_month and
75-
other.day_of_week == self.day_of_week and
76-
other.hour == self.hour and
77-
other.minute == self.minute and
78-
other.tz == self.tz)
73+
return (other.month_of_year == self.month_of_year
74+
and other.day_of_month == self.day_of_month
75+
and other.day_of_week == self.day_of_week
76+
and other.hour == self.hour
77+
and other.minute == self.minute
78+
and other.tz == self.tz)
7979
return NotImplemented

django_celery_beat/utils.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,6 @@ def is_database_scheduler(scheduler):
3939
from kombu.utils import symbol_by_name
4040
from .schedulers import DatabaseScheduler
4141
return (
42-
scheduler == 'django' or
43-
issubclass(symbol_by_name(scheduler), DatabaseScheduler)
42+
scheduler == 'django'
43+
or issubclass(symbol_by_name(scheduler), DatabaseScheduler)
4444
)

setup.cfg

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ DJANGO_SETTINGS_MODULE=t.proj.settings
66
[flake8]
77
# classes can be lowercase, arguments and variables can be uppercase
88
# whenever it makes the code more readable.
9-
ignore = N806, N802, N801, N803
9+
ignore = N806, N802, N801, N803, W503, W504
1010

1111
[pep257]
1212
ignore = D102,D104,D203,D105,D213

t/unit/test_schedulers.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -427,10 +427,10 @@ def test_scheduler_schedules_equality_on_change(self, monkeypatch):
427427
class test_models(SchedulerCase):
428428

429429
def test_IntervalSchedule_unicode(self):
430-
assert (text_t(IntervalSchedule(every=1, period='seconds')) ==
431-
'every second')
432-
assert (text_t(IntervalSchedule(every=10, period='seconds')) ==
433-
'every 10 seconds')
430+
assert (text_t(IntervalSchedule(every=1, period='seconds'))
431+
== 'every second')
432+
assert (text_t(IntervalSchedule(every=10, period='seconds'))
433+
== 'every 10 seconds')
434434

435435
def test_CrontabSchedule_unicode(self):
436436
assert text_t(CrontabSchedule(

0 commit comments

Comments
 (0)