You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

107 lines
3.3 KiB

  1. from datetime import timedelta
  2. import time
  3. from django.core.management.base import BaseCommand
  4. from django.db.models import Q
  5. from django.utils import timezone
  6. from hc.accounts.models import Profile
  7. from hc.api.models import Check
  8. def num_pinged_checks(profile):
  9. q = Check.objects.filter(user_id=profile.user.id,)
  10. q = q.filter(last_ping__isnull=False)
  11. return q.count()
  12. class Command(BaseCommand):
  13. help = 'Send due monthly reports and nags'
  14. tmpl = "Sent monthly report to %s"
  15. def add_arguments(self, parser):
  16. parser.add_argument(
  17. '--loop',
  18. action='store_true',
  19. dest='loop',
  20. default=False,
  21. help='Keep running indefinitely in a 300 second wait loop',
  22. )
  23. def handle_one_monthly_report(self):
  24. now = timezone.now()
  25. month_before = now - timedelta(days=30)
  26. month_after = now + timedelta(days=30)
  27. report_due = Q(next_report_date__lt=now)
  28. report_not_scheduled = Q(next_report_date__isnull=True)
  29. q = Profile.objects.filter(report_due | report_not_scheduled)
  30. q = q.filter(reports_allowed=True)
  31. q = q.filter(user__date_joined__lt=month_before)
  32. profile = q.first()
  33. if profile is None:
  34. return False
  35. # A sort of optimistic lock. Try to update next_report_date,
  36. # and if does get modified, we're in drivers seat:
  37. qq = Profile.objects.filter(id=profile.id,
  38. next_report_date=profile.next_report_date)
  39. num_updated = qq.update(next_report_date=month_after)
  40. if num_updated != 1:
  41. # next_report_date was already updated elsewhere, skipping
  42. return True
  43. if profile.send_report():
  44. self.stdout.write(self.tmpl % profile.user.email)
  45. # Pause before next report to avoid hitting sending quota
  46. time.sleep(1)
  47. return True
  48. def handle_one_nag(self):
  49. now = timezone.now()
  50. q = Profile.objects.filter(next_nag_date__lt=now)
  51. profile = q.first()
  52. if profile is None:
  53. return False
  54. qq = Profile.objects.filter(id=profile.id,
  55. next_nag_date=profile.next_nag_date)
  56. num_updated = qq.update(next_nag_date=now + profile.nag_period)
  57. if num_updated != 1:
  58. # next_rag_date was already updated elsewhere, skipping
  59. return True
  60. if profile.send_report(nag=True):
  61. self.stdout.write("Sent nag to %s" % profile.user.email)
  62. # Pause before next report to avoid hitting sending quota
  63. time.sleep(1)
  64. else:
  65. profile.next_nag_date = None
  66. profile.save()
  67. return True
  68. def handle(self, *args, **options):
  69. self.stdout.write("sendreports is now running")
  70. while True:
  71. # Monthly reports
  72. while self.handle_one_monthly_report():
  73. pass
  74. # Daily and hourly nags
  75. while self.handle_one_nag():
  76. pass
  77. if not options["loop"]:
  78. break
  79. formatted = timezone.now().isoformat()
  80. self.stdout.write("-- MARK %s --" % formatted)
  81. # Sleep for 1 minute before looking for more work
  82. time.sleep(60)