schedula process_map_updates usind celery on new MapUpdate creation
This commit is contained in:
parent
0565869eb2
commit
efbbbe2495
3 changed files with 31 additions and 15 deletions
|
@ -1,22 +1,10 @@
|
|||
from django.core.management.base import BaseCommand
|
||||
from django.utils.formats import date_format
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.translation import ungettext_lazy
|
||||
|
||||
from c3nav.mapdata.models import MapUpdate
|
||||
from c3nav.mapdata.tasks import process_map_updates
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
help = 'process unprocessed map updates'
|
||||
|
||||
def handle(self, *args, **options):
|
||||
updates = MapUpdate.process_updates()
|
||||
|
||||
print()
|
||||
print(ungettext_lazy('%d map update processed.', '%d map updates processed.', len(updates)) % len(updates))
|
||||
|
||||
if updates:
|
||||
print(_('Last processed update: %(date)s (#%(id)d)') % {
|
||||
'date': date_format(updates[-1].datetime, 'DATETIME_FORMAT'),
|
||||
'id': updates[-1].pk,
|
||||
})
|
||||
process_map_updates()
|
||||
|
|
|
@ -8,6 +8,8 @@ from django.utils.http import int_to_base36
|
|||
from django.utils.timezone import make_naive
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
|
||||
from c3nav.mapdata.tasks import process_map_updates
|
||||
|
||||
|
||||
class MapUpdate(models.Model):
|
||||
"""
|
||||
|
@ -101,7 +103,8 @@ class MapUpdate(models.Model):
|
|||
return new_updates
|
||||
|
||||
def save(self, **kwargs):
|
||||
if self.pk is not None and (self.was_processed or not self.processed):
|
||||
new = self.pk is None
|
||||
if not new and (self.was_processed or not self.processed):
|
||||
raise TypeError
|
||||
|
||||
from c3nav.mapdata.cache import changed_geometries
|
||||
|
@ -110,3 +113,6 @@ class MapUpdate(models.Model):
|
|||
super().save(**kwargs)
|
||||
|
||||
cache.set('mapdata:last_update', self.to_tuple, 900)
|
||||
|
||||
if new and settings.HAS_CELERY:
|
||||
process_map_updates.apply_async()
|
||||
|
|
22
src/c3nav/mapdata/tasks.py
Normal file
22
src/c3nav/mapdata/tasks.py
Normal file
|
@ -0,0 +1,22 @@
|
|||
from django.utils.formats import date_format
|
||||
from django.utils.translation import ugettext_lazy as _
|
||||
from django.utils.translation import ungettext_lazy
|
||||
|
||||
from c3nav.celery import app
|
||||
|
||||
|
||||
@app.task(rate_limit='1/m')
|
||||
def process_map_updates():
|
||||
from c3nav.mapdata.models import MapUpdate
|
||||
updates = MapUpdate.process_updates()
|
||||
|
||||
if updates:
|
||||
print()
|
||||
|
||||
print(ungettext_lazy('%d map update processed.', '%d map updates processed.', len(updates)) % len(updates))
|
||||
|
||||
if updates:
|
||||
print(_('Last processed update: %(date)s (#%(id)d)') % {
|
||||
'date': date_format(updates[-1].datetime, 'DATETIME_FORMAT'),
|
||||
'id': updates[-1].pk,
|
||||
})
|
Loading…
Add table
Add a link
Reference in a new issue