Browse Source

feat(crons): Start writing to next_checkin_latest (#53784)

Start writing to `next_checkin_latest`
Richard Ortenberg 1 year ago
parent
commit
4185aaa848

+ 7 - 2
src/sentry/monitors/models.py

@@ -517,13 +517,16 @@ class MonitorEnvironment(Model):
         elif reason == MonitorFailure.DURATION:
             new_status = MonitorStatus.TIMEOUT
 
+        next_checkin_latest = self.monitor.get_next_scheduled_checkin_with_margin(next_checkin_base)
+
         affected = (
             type(self)
             .objects.filter(
                 Q(last_checkin__lte=last_checkin) | Q(last_checkin__isnull=True), id=self.id
             )
             .update(
-                next_checkin=self.monitor.get_next_scheduled_checkin_with_margin(next_checkin_base),
+                next_checkin=next_checkin_latest,
+                next_checkin_latest=next_checkin_latest,
                 status=new_status,
                 last_checkin=last_checkin,
             )
@@ -637,9 +640,11 @@ class MonitorEnvironment(Model):
         return True
 
     def mark_ok(self, checkin: MonitorCheckIn, ts: datetime):
+        next_checkin_latest = self.monitor.get_next_scheduled_checkin_with_margin(ts)
         params = {
             "last_checkin": ts,
-            "next_checkin": self.monitor.get_next_scheduled_checkin_with_margin(ts),
+            "next_checkin": next_checkin_latest,
+            "next_checkin_latest": next_checkin_latest,
         }
         if checkin.status == CheckInStatus.OK and self.monitor.status != ObjectStatus.DISABLED:
             params["status"] = MonitorStatus.OK

+ 4 - 0
tests/sentry/monitors/endpoints/test_monitor_ingest_checkin_details.py

@@ -116,6 +116,7 @@ class UpdateMonitorIngestCheckinTest(MonitorIngestTestCase):
 
             monitor_environment = MonitorEnvironment.objects.get(id=monitor_environment.id)
             assert monitor_environment.next_checkin > checkin.date_added
+            assert monitor_environment.next_checkin_latest > checkin.date_added
             assert monitor_environment.status == MonitorStatus.OK
             assert monitor_environment.last_checkin > checkin.date_added
 
@@ -150,6 +151,7 @@ class UpdateMonitorIngestCheckinTest(MonitorIngestTestCase):
 
             monitor_environment = MonitorEnvironment.objects.get(id=monitor_environment.id)
             assert monitor_environment.next_checkin > checkin.date_added
+            assert monitor_environment.next_checkin_latest > checkin.date_added
             assert monitor_environment.status == MonitorStatus.OK
             assert monitor_environment.last_checkin > checkin.date_added
 
@@ -192,6 +194,7 @@ class UpdateMonitorIngestCheckinTest(MonitorIngestTestCase):
 
             monitor_environment = MonitorEnvironment.objects.get(id=monitor_environment.id)
             assert monitor_environment.next_checkin > checkin.date_added
+            assert monitor_environment.next_checkin_latest > checkin.date_added
             assert monitor_environment.status == MonitorStatus.ERROR
             assert monitor_environment.last_checkin > checkin.date_added
 
@@ -305,6 +308,7 @@ class UpdateMonitorIngestCheckinTest(MonitorIngestTestCase):
 
             monitor_environment = MonitorEnvironment.objects.get(id=monitor_environment.id)
             assert monitor_environment.next_checkin > checkin2.date_added
+            assert monitor_environment.next_checkin_latest > checkin2.date_added
             assert monitor_environment.status == MonitorStatus.OK
             assert monitor_environment.last_checkin > checkin2.date_added
 

+ 12 - 0
tests/sentry/monitors/endpoints/test_monitor_ingest_checkin_index.py

@@ -87,6 +87,10 @@ class CreateMonitorCheckInTest(MonitorIngestTestCase):
                 monitor_environment.next_checkin
                 == monitor.get_next_scheduled_checkin_with_margin(checkin.date_added)
             )
+            assert (
+                monitor_environment.next_checkin_latest
+                == monitor.get_next_scheduled_checkin_with_margin(checkin.date_added)
+            )
 
             # Confirm next check-in is populated with config and expected time
             expected_time = monitor_environment.next_checkin
@@ -158,6 +162,10 @@ class CreateMonitorCheckInTest(MonitorIngestTestCase):
                 monitor_environment.next_checkin
                 == monitor.get_next_scheduled_checkin_with_margin(checkin.date_added)
             )
+            assert (
+                monitor_environment.next_checkin_latest
+                == monitor.get_next_scheduled_checkin_with_margin(checkin.date_added)
+            )
 
     def test_disabled(self):
         for path_func in self._get_path_functions():
@@ -180,6 +188,10 @@ class CreateMonitorCheckInTest(MonitorIngestTestCase):
                 monitor_environment.next_checkin
                 == monitor.get_next_scheduled_checkin_with_margin(checkin.date_added)
             )
+            assert (
+                monitor_environment.next_checkin_latest
+                == monitor.get_next_scheduled_checkin_with_margin(checkin.date_added)
+            )
 
     def test_pending_deletion(self):
         monitor = self._create_monitor(status=ObjectStatus.PENDING_DELETION)

+ 32 - 0
tests/sentry/monitors/test_monitor_consumer.py

@@ -101,6 +101,10 @@ class MonitorConsumerTest(TestCase):
         assert monitor_environment.next_checkin == monitor.get_next_scheduled_checkin_with_margin(
             checkin.date_added
         )
+        assert (
+            monitor_environment.next_checkin_latest
+            == monitor.get_next_scheduled_checkin_with_margin(checkin.date_added)
+        )
 
         # Process another check-in to verify we set an expected time for the next check-in
         expected_time = monitor_environment.next_checkin
@@ -124,6 +128,10 @@ class MonitorConsumerTest(TestCase):
         assert monitor_environment.next_checkin == monitor.get_next_scheduled_checkin_with_margin(
             checkin.date_added
         )
+        assert (
+            monitor_environment.next_checkin_latest
+            == monitor.get_next_scheduled_checkin_with_margin(checkin.date_added)
+        )
 
         # Process another check-in to verify we set an expected time for the next check-in
         expected_time = monitor_environment.next_checkin
@@ -145,6 +153,10 @@ class MonitorConsumerTest(TestCase):
         assert monitor_environment.next_checkin == monitor.get_next_scheduled_checkin_with_margin(
             checkin.date_added
         )
+        assert (
+            monitor_environment.next_checkin_latest
+            == monitor.get_next_scheduled_checkin_with_margin(checkin.date_added)
+        )
 
     def test_disabled(self):
         monitor = self._create_monitor(status=ObjectStatus.DISABLED)
@@ -162,6 +174,10 @@ class MonitorConsumerTest(TestCase):
         assert monitor_environment.next_checkin == monitor.get_next_scheduled_checkin_with_margin(
             checkin.date_added
         )
+        assert (
+            monitor_environment.next_checkin_latest
+            == monitor.get_next_scheduled_checkin_with_margin(checkin.date_added)
+        )
 
     def test_create_lock(self):
         monitor = self._create_monitor(slug="my-monitor")
@@ -256,6 +272,10 @@ class MonitorConsumerTest(TestCase):
         assert monitor_environment.next_checkin == monitor.get_next_scheduled_checkin_with_margin(
             checkin.date_added
         )
+        assert (
+            monitor_environment.next_checkin_latest
+            == monitor.get_next_scheduled_checkin_with_margin(checkin.date_added)
+        )
 
     def test_monitor_create(self):
         self.send_message(
@@ -277,6 +297,12 @@ class MonitorConsumerTest(TestCase):
                 checkin.date_added
             )
         )
+        assert (
+            monitor_environment.next_checkin_latest
+            == monitor_environment.monitor.get_next_scheduled_checkin_with_margin(
+                checkin.date_added
+            )
+        )
 
     def test_monitor_update(self):
         monitor = self._create_monitor(slug="my-monitor")
@@ -302,6 +328,12 @@ class MonitorConsumerTest(TestCase):
                 checkin.date_added
             )
         )
+        assert (
+            monitor_environment.next_checkin_latest
+            == monitor_environment.monitor.get_next_scheduled_checkin_with_margin(
+                checkin.date_added
+            )
+        )
 
     def test_check_in_empty_id(self):
         monitor = self._create_monitor(slug="my-monitor")