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.

111 lines
3.4 KiB

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