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.

113 lines
3.5 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 NO_NAG, Profile
  7. from hc.api.models import Check
  8. from hc.lib.date import choose_next_report_date
  9. def num_pinged_checks(profile):
  10. q = Check.objects.filter(user_id=profile.user.id)
  11. q = q.filter(last_ping__isnull=False)
  12. return q.count()
  13. class Command(BaseCommand):
  14. help = "Send due monthly reports and nags"
  15. tmpl = "Sent monthly report to %s"
  16. def pause(self):
  17. time.sleep(1)
  18. def add_arguments(self, parser):
  19. parser.add_argument(
  20. "--loop",
  21. action="store_true",
  22. dest="loop",
  23. default=False,
  24. help="Keep running indefinitely in a 300 second wait loop",
  25. )
  26. def handle_one_monthly_report(self):
  27. report_due = Q(next_report_date__lt=timezone.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. profile = q.first()
  32. if profile is None:
  33. # No matching profiles found – nothing to do right now.
  34. return False
  35. # A sort of optimistic lock. Will try to update next_report_date,
  36. # and if does get modified, we're in drivers seat:
  37. qq = Profile.objects.filter(
  38. id=profile.id, next_report_date=profile.next_report_date
  39. )
  40. # Next report date is currently not scheduled: schedule it and move on.
  41. if profile.next_report_date is None:
  42. qq.update(next_report_date=choose_next_report_date())
  43. return True
  44. num_updated = qq.update(next_report_date=choose_next_report_date())
  45. if num_updated != 1:
  46. # next_report_date was already updated elsewhere, skipping
  47. return True
  48. if profile.send_report():
  49. self.stdout.write(self.tmpl % profile.user.email)
  50. # Pause before next report to avoid hitting sending quota
  51. self.pause()
  52. return True
  53. def handle_one_nag(self):
  54. now = timezone.now()
  55. q = Profile.objects.filter(next_nag_date__lt=now)
  56. q = q.exclude(nag_period=NO_NAG)
  57. profile = q.first()
  58. if profile is None:
  59. return False
  60. qq = Profile.objects.filter(id=profile.id, next_nag_date=profile.next_nag_date)
  61. num_updated = qq.update(next_nag_date=now + profile.nag_period)
  62. if num_updated != 1:
  63. # next_rag_date was already updated elsewhere, skipping
  64. return True
  65. if profile.send_report(nag=True):
  66. self.stdout.write("Sent nag to %s" % profile.user.email)
  67. # Pause before next report to avoid hitting sending quota
  68. self.pause()
  69. else:
  70. profile.next_nag_date = None
  71. profile.save()
  72. return True
  73. def handle(self, *args, **options):
  74. self.stdout.write("sendreports is now running")
  75. while True:
  76. # Monthly reports
  77. while self.handle_one_monthly_report():
  78. pass
  79. # Daily and hourly nags
  80. while self.handle_one_nag():
  81. pass
  82. if not options["loop"]:
  83. break
  84. formatted = timezone.now().isoformat()
  85. self.stdout.write("-- MARK %s --" % formatted)
  86. # Sleep for 1 minute before looking for more work
  87. time.sleep(60)