|
6 | 6 | import timezone_field
|
7 | 7 | from celery import schedules
|
8 | 8 | from celery.five import python_2_unicode_compatible
|
| 9 | +from django.conf import settings |
9 | 10 | from django.core.exceptions import MultipleObjectsReturned, ValidationError
|
10 | 11 | from django.core.validators import MaxValueValidator
|
11 | 12 | from django.db import models
|
12 | 13 | from django.db.models import signals
|
| 14 | +from django.db.models.indexes import Index |
13 | 15 | from django.utils.translation import ugettext_lazy as _
|
14 | 16 |
|
15 | 17 | from . import managers, validators
|
@@ -39,6 +41,23 @@ def cronexp(field):
|
39 | 41 | return field and str(field).replace(' ', '') or '*'
|
40 | 42 |
|
41 | 43 |
|
| 44 | +class CeleryMySQLIndex(Index): |
| 45 | + def create_sql(self, model, schema_editor, using=''): |
| 46 | + sql_create_index = 'CREATE INDEX %(name)s ON %(table)s (%(columns)s(%(size)d))%(extra)s' |
| 47 | + sql_parameters = self.get_sql_create_template_values( |
| 48 | + model, |
| 49 | + schema_editor, |
| 50 | + using |
| 51 | + ) |
| 52 | + sql_parameters['size'] = getattr( |
| 53 | + settings, |
| 54 | + 'DJANGO_CELERY_BEAT_NAME_MAX_LENGTH', |
| 55 | + 200 |
| 56 | + ) |
| 57 | + sql = sql_create_index % sql_parameters |
| 58 | + return sql |
| 59 | + |
| 60 | + |
42 | 61 | @python_2_unicode_compatible
|
43 | 62 | class SolarSchedule(models.Model):
|
44 | 63 | """Schedule following astronomical patterns."""
|
@@ -248,8 +267,14 @@ class PeriodicTask(models.Model):
|
248 | 267 | """Model representing a periodic task."""
|
249 | 268 |
|
250 | 269 | name = models.CharField(
|
251 |
| - _('name'), max_length=200, unique=True, |
252 |
| - help_text=_('Useful description'), |
| 270 | + _('name'), |
| 271 | + max_length=getattr( |
| 272 | + settings, |
| 273 | + 'DJANGO_CELERY_BEAT_NAME_MAX_LENGTH', |
| 274 | + 200 |
| 275 | + ), |
| 276 | + unique=True, |
| 277 | + help_text=_('Useful description') |
253 | 278 | )
|
254 | 279 | task = models.CharField(_('task name'), max_length=200)
|
255 | 280 | interval = models.ForeignKey(
|
@@ -316,6 +341,7 @@ class Meta:
|
316 | 341 |
|
317 | 342 | verbose_name = _('periodic task')
|
318 | 343 | verbose_name_plural = _('periodic tasks')
|
| 344 | + indexes = [CeleryMySQLIndex(fields=['name'])] |
319 | 345 |
|
320 | 346 | def validate_unique(self, *args, **kwargs):
|
321 | 347 | super(PeriodicTask, self).validate_unique(*args, **kwargs)
|
|
0 commit comments