update last_updated and appsupport immediately (bug 571057)

This commit is contained in:
Jeff Balogh 2010-09-21 15:33:39 -07:00
Родитель edc0ac1075
Коммит 394e60b441
5 изменённых файлов: 101 добавлений и 45 удалений

Просмотреть файл

@ -1,7 +1,7 @@
from datetime import datetime, timedelta
from django.db import connection, connections, transaction
from django.db.models import Max, Q, F
from django.db import connections, transaction
from django.db.models import Q, F
import commonware.log
from celery.messaging import establish_connection
@ -144,24 +144,7 @@ def _change_last_updated(next):
@cronjobs.register
def addon_last_updated():
next = {}
public = (Addon.uncached.filter(status=amo.STATUS_PUBLIC,
versions__files__status=amo.STATUS_PUBLIC).values('id')
.annotate(last_updated=Max('versions__files__datestatuschanged')))
exp = (Addon.uncached.exclude(status=amo.STATUS_PUBLIC)
.filter(versions__files__status__in=amo.VALID_STATUSES)
.values('id')
.annotate(last_updated=Max('versions__files__created')))
listed = (Addon.uncached.filter(status=amo.STATUS_LISTED)
.values('id')
.annotate(last_updated=Max('versions__created')))
personas = (Addon.uncached.filter(type=amo.ADDON_PERSONA)
.extra(select={'last_updated': 'created'}))
for q in (public, exp, listed, personas):
for q in Addon._last_updated_queries().values():
for addon, last_updated in q.values_list('id', 'last_updated'):
next[addon] = last_updated
@ -194,24 +177,5 @@ def update_addon_appsupport():
@task(rate_limit='30/m')
@transaction.commit_manually
def _update_appsupport(ids, **kw):
task_log.info("[%s@%s] Updating addons app_support." %
(len(ids), _update_appsupport.rate_limit))
delete = 'DELETE FROM appsupport WHERE addon_id IN (%s)'
insert = """INSERT INTO appsupport (addon_id, app_id, created, modified)
VALUES %s"""
addons = Addon.uncached.filter(id__in=ids).no_transforms()
apps = [(addon.id, app.id) for addon in addons
for app in addon.compatible_apps]
s = ','.join('(%s, %s, NOW(), NOW())' % x for x in apps)
if not apps:
return
cursor = connection.cursor()
cursor.execute(delete % ','.join(map(str, ids)))
cursor.execute(insert % s)
transaction.commit()
# All our updates were sql, so invalidate manually.
Addon.objects.invalidate(*addons)
from .tasks import update_appsupport
update_appsupport(ids)

Просмотреть файл

@ -6,7 +6,7 @@ import time
from django.conf import settings
from django.db import models
from django.db.models import Q, Sum
from django.db.models import Q, Sum, Max
import caching.base as caching
import commonware.log
@ -24,7 +24,7 @@ from translations.fields import TranslatedField, PurifiedField, LinkifiedField
from users.models import UserProfile, PersonaAuthor
from versions.models import Version
from . import query
from . import query, signals
log = commonware.log.getLogger('z.addons')
@ -275,6 +275,7 @@ class Addon(amo.models.ModelBase):
self._current_version = current_version
try:
self.save()
signals.version_changed.send(sender=self)
return True
except Exception, e:
log.error('Could not save version %s for addon %s (%s)' %
@ -472,6 +473,35 @@ class Addon(amo.models.ModelBase):
.values_list('service', 'count'))
return rv
@classmethod
def _last_updated_queries(cls):
"""
Get the queries used to calculate addon.last_updated.
"""
public = (Addon.uncached.filter(status=amo.STATUS_PUBLIC,
versions__files__status=amo.STATUS_PUBLIC)
.exclude(type=amo.ADDON_PERSONA).values('id')
.annotate(last_updated=Max('versions__files__datestatuschanged')))
exp = (Addon.uncached.exclude(status=amo.STATUS_PUBLIC)
.filter(versions__files__status__in=amo.VALID_STATUSES)
.values('id')
.annotate(last_updated=Max('versions__files__created')))
listed = (Addon.uncached.filter(status=amo.STATUS_LISTED)
.values('id')
.annotate(last_updated=Max('versions__created')))
personas = (Addon.uncached.filter(type=amo.ADDON_PERSONA)
.extra(select={'last_updated': 'created'}))
return dict(public=public, exp=exp, listed=listed, personas=personas)
def version_changed(sender, **kw):
from . import tasks
tasks.version_changed.delay(sender.id)
signals.version_changed.connect(version_changed)
class Persona(caching.CachingMixin, models.Model):
"""Personas-specific additions to the add-on model."""

4
apps/addons/signals.py Normal file
Просмотреть файл

@ -0,0 +1,4 @@
import django.dispatch
version_changed = django.dispatch.Signal()

Просмотреть файл

@ -1 +1,59 @@
from . import cron
import logging
from django.db import connection, transaction
from celeryutils import task
import amo
from amo.decorators import write
from . import cron # Pull in tasks from cron.
from .models import Addon
log = logging.getLogger('z.task')
@task
@write
def version_changed(addon_id, **kw):
update_last_updated(addon_id)
update_appsupport([addon_id])
def update_last_updated(addon_id):
log.info('[1@None] Updating last updated for %s.' % addon_id)
queries = Addon._last_updated_queries()
addon = Addon.objects.get(pk=addon_id)
if addon.is_persona():
q = 'personas'
elif addon.status == amo.STATUS_PUBLIC:
q = 'public'
elif addon.status == amo.STATUS_LISTED:
q = 'listed'
else:
q = 'exp'
pk, t = queries[q].filter(pk=addon_id).values_list('id', 'last_updated')[0]
Addon.objects.filter(pk=pk).update(last_updated=t)
@transaction.commit_manually
def update_appsupport(ids):
log.info("[%s@None] Updating appsupport for %s." % (len(ids), ids))
delete = 'DELETE FROM appsupport WHERE addon_id IN (%s)'
insert = """INSERT INTO appsupport (addon_id, app_id, created, modified)
VALUES %s"""
addons = Addon.uncached.filter(id__in=ids).no_transforms()
apps = [(addon.id, app.id) for addon in addons
for app in addon.compatible_apps]
s = ','.join('(%s, %s, NOW(), NOW())' % x for x in apps)
if not apps:
return
cursor = connection.cursor()
cursor.execute(delete % ','.join(map(str, ids)))
cursor.execute(insert % s)
transaction.commit()
# All our updates were sql, so invalidate manually.
Addon.objects.invalidate(*addons)

Просмотреть файл

@ -28,7 +28,7 @@ class TestLastUpdated(amo.test_utils.ExtraSetup, test_utils.TestCase):
fixtures = ('base/addon_3615', 'addons/listed')
def test_personas(self):
Addon.objects.update(type=amo.ADDON_PERSONA)
Addon.objects.update(type=amo.ADDON_PERSONA, status=amo.STATUS_PUBLIC)
cron.addon_last_updated()
for addon in Addon.objects.all():