From 68fe11a41bbd941eed72d04310da95ad18b648af Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Fri, 8 Jan 2016 14:16:50 +0200 Subject: [PATCH] fillnpings catches up with pings received during the execution of the command --- hc/api/management/commands/fillnpings.py | 27 ++++++++++++++---------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/hc/api/management/commands/fillnpings.py b/hc/api/management/commands/fillnpings.py index 27802ae3..ee772dbf 100644 --- a/hc/api/management/commands/fillnpings.py +++ b/hc/api/management/commands/fillnpings.py @@ -47,21 +47,26 @@ class Command(BaseCommand): last_pk = Ping.objects.order_by('-pk')[0].pk queryset = Ping.objects.order_by('pk') - transaction.set_autocommit(False) while pk < last_pk: - for ping in queryset.filter(pk__gt=pk)[:chunksize]: - pk = ping.pk - counts[ping.owner_id] += 1 + transaction.set_autocommit(False) + while pk < last_pk: + for ping in queryset.filter(pk__gt=pk)[:chunksize]: + pk = ping.pk + counts[ping.owner_id] += 1 - ping.n = counts[ping.owner_id] - ping.save(update_fields=("n", )) + ping.n = counts[ping.owner_id] + ping.save(update_fields=("n", )) - gc.collect() - progress = 100 * pk / last_pk - self.stdout.write("Processed ping id %d (%.2f%%)" % (pk, progress)) + gc.collect() + progress = 100 * pk / last_pk + self.stdout.write( + "Processed ping id %d (%.2f%%)" % (pk, progress)) - transaction.commit() - transaction.set_autocommit(True) + transaction.commit() + + transaction.set_autocommit(True) + # last_pk might have increased because of influx of new pings: + last_pk = Ping.objects.order_by('-pk')[0].pk self.stdout.write("Updating check.n_pings") for check_id, n_pings in counts.items():