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.

140 lines
4.2 KiB

  1. import time
  2. from threading import Thread
  3. from django.core.management.base import BaseCommand
  4. from django.utils import timezone
  5. from hc.api.models import Check, Flip
  6. from statsd.defaults.env import statsd
  7. def notify(flip_id, stdout):
  8. flip = Flip.objects.get(id=flip_id)
  9. check = flip.owner
  10. # Set the historic status here but *don't save it*.
  11. # It would be nicer to pass the status explicitly, as a separate parameter.
  12. check.status = flip.new_status
  13. # And just to make sure it doesn't get saved by a future coding accident:
  14. setattr(check, "save", None)
  15. tmpl = "Sending alert, status=%s, code=%s\n"
  16. stdout.write(tmpl % (flip.new_status, check.code))
  17. # Set dates for followup nags
  18. if flip.new_status == "down":
  19. check.project.set_next_nag_date()
  20. # Send notifications
  21. send_time = timezone.now()
  22. errors = flip.send_alerts()
  23. for ch, error in errors:
  24. stdout.write("ERROR: %s %s %s\n" % (ch.kind, ch.value, error))
  25. statsd.timing("hc.sendalerts.dwellTime", send_time - flip.created)
  26. def notify_on_thread(flip_id, stdout):
  27. t = Thread(target=notify, args=(flip_id, stdout))
  28. t.start()
  29. class Command(BaseCommand):
  30. help = "Sends UP/DOWN email alerts"
  31. def add_arguments(self, parser):
  32. parser.add_argument(
  33. "--no-loop",
  34. action="store_false",
  35. dest="loop",
  36. default=True,
  37. help="Do not keep running indefinitely in a 2 second wait loop",
  38. )
  39. parser.add_argument(
  40. "--no-threads",
  41. action="store_false",
  42. dest="use_threads",
  43. default=False,
  44. help="Send alerts synchronously, without using threads",
  45. )
  46. def process_one_flip(self, use_threads=True):
  47. """ Find unprocessed flip, send notifications. """
  48. # Order by processed, otherwise Django will automatically order by id
  49. # and make the query less efficient
  50. q = Flip.objects.filter(processed=None).order_by("processed")
  51. flip = q.first()
  52. if flip is None:
  53. return False
  54. q = Flip.objects.filter(id=flip.id, processed=None)
  55. num_updated = q.update(processed=timezone.now())
  56. if num_updated != 1:
  57. # Nothing got updated: another worker process got there first.
  58. return True
  59. if use_threads:
  60. notify_on_thread(flip.id, self.stdout)
  61. else:
  62. notify(flip.id, self.stdout)
  63. return True
  64. def handle_going_down(self):
  65. """ Process a single check going down. """
  66. now = timezone.now()
  67. q = Check.objects.filter(alert_after__lt=now).exclude(status="down")
  68. # Sort by alert_after, to avoid unnecessary sorting by id:
  69. check = q.order_by("alert_after").first()
  70. if check is None:
  71. return False
  72. old_status = check.status
  73. q = Check.objects.filter(id=check.id, status=old_status)
  74. if check.get_status(with_started=False) != "down":
  75. # It is not down yet. Update alert_after
  76. q.update(alert_after=check.going_down_after())
  77. return True
  78. # Atomically update status
  79. flip_time = check.going_down_after()
  80. num_updated = q.update(alert_after=None, status="down")
  81. if num_updated != 1:
  82. # Nothing got updated: another worker process got there first.
  83. return True
  84. flip = Flip(owner=check)
  85. flip.created = flip_time
  86. flip.old_status = old_status
  87. flip.new_status = "down"
  88. flip.save()
  89. return True
  90. def handle(self, use_threads=True, loop=True, *args, **options):
  91. self.stdout.write("sendalerts is now running\n")
  92. i, sent = 0, 0
  93. while True:
  94. # Create flips for any checks going down
  95. while self.handle_going_down():
  96. pass
  97. # Process the unprocessed flips
  98. while self.process_one_flip(use_threads):
  99. sent += 1
  100. if not loop:
  101. break
  102. time.sleep(2)
  103. i += 1
  104. if i % 60 == 0:
  105. timestamp = timezone.now().isoformat()
  106. self.stdout.write("-- MARK %s --\n" % timestamp)
  107. return "Sent %d alert(s)" % sent