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.

112 lines
3.5 KiB

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