From 252fa1f884fab454d325504493c2291ce4bc0617 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?P=C4=93teris=20Caune?= Date: Mon, 19 Sep 2016 21:19:10 +0300 Subject: [PATCH] sendreports now can be run concurrently too --- hc/api/management/commands/sendreports.py | 24 ++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/hc/api/management/commands/sendreports.py b/hc/api/management/commands/sendreports.py index cb513d62..0979909e 100644 --- a/hc/api/management/commands/sendreports.py +++ b/hc/api/management/commands/sendreports.py @@ -30,6 +30,7 @@ class Command(BaseCommand): def handle_one_run(self): now = timezone.now() month_before = now - timedelta(days=30) + month_after = now + timedelta(days=30) report_due = Q(next_report_date__lt=now) report_not_scheduled = Q(next_report_date__isnull=True) @@ -37,12 +38,25 @@ class Command(BaseCommand): q = Profile.objects.filter(report_due | report_not_scheduled) q = q.filter(reports_allowed=True) q = q.filter(user__date_joined__lt=month_before) + profiles = list(q) + sent = 0 - for profile in q: - if num_pinged_checks(profile) > 0: - self.stdout.write(self.tmpl % profile.user.email) - profile.send_report() - sent += 1 + for profile in profiles: + qq = Profile.objects + qq = qq.filter(id=profile.id, + next_report_date=profile.next_report_date) + + num_updated = qq.update(next_report_date=month_after) + if num_updated != 1: + # Was updated elsewhere, skipping + continue + + if num_pinged_checks(profile) == 0: + continue + + self.stdout.write(self.tmpl % profile.user.email) + profile.send_report() + sent += 1 return sent