Skip to content

Commit

Permalink
update last_updated and appsupport immediately (bug 571057)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Balogh committed Sep 24, 2010
1 parent edc0ac1 commit 394e60b
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 45 deletions.
46 changes: 5 additions & 41 deletions apps/addons/cron.py
@@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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)
34 changes: 32 additions & 2 deletions apps/addons/models.py
Expand Up @@ -6,7 +6,7 @@

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
Expand All @@ -24,7 +24,7 @@
from users.models import UserProfile, PersonaAuthor
from versions.models import Version

from . import query
from . import query, signals

log = commonware.log.getLogger('z.addons')

Expand Down Expand Up @@ -275,6 +275,7 @@ def update_current_version(self):
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)' %
Expand Down Expand Up @@ -472,6 +473,35 @@ def share_counts(self):
.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."""
Expand Down
4 changes: 4 additions & 0 deletions apps/addons/signals.py
@@ -0,0 +1,4 @@
import django.dispatch


version_changed = django.dispatch.Signal()
60 changes: 59 additions & 1 deletion apps/addons/tasks.py
@@ -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)
2 changes: 1 addition & 1 deletion apps/addons/tests/test_cron.py
Expand Up @@ -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():
Expand Down

0 comments on commit 394e60b

Please sign in to comment.