From 4a2725aaa6f875b69f97f1ef5618b9ad61354546 Mon Sep 17 00:00:00 2001 From: Ilya Kreymer Date: Mon, 5 Aug 2024 16:00:21 -0700 Subject: [PATCH] operator: adjust state transition rules to ensure 'running' state always accounted for in db (#1989) don't rely on current status, always set state to running when running to ensure idempotency in case of multiple calls --- backend/btrixcloud/operator/crawls.py | 36 +++++++++++++-------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/backend/btrixcloud/operator/crawls.py b/backend/btrixcloud/operator/crawls.py index 5f11d6e20..81cd77ff0 100644 --- a/backend/btrixcloud/operator/crawls.py +++ b/backend/btrixcloud/operator/crawls.py @@ -807,27 +807,25 @@ async def sync_crawl_state( status.resync_after = self.fast_retry_secs return status - # set state to running (if not already) - if status.state not in RUNNING_STATES: - # if true (state is set), also run webhook - if await self.set_state( - "running", - status, - crawl, - allowed_from=["starting", "waiting_capacity"], - ): - if not crawl.qa_source_crawl_id: - self.run_task( - self.event_webhook_ops.create_crawl_started_notification( - crawl.id, crawl.oid, scheduled=crawl.scheduled - ) + # if true (state is set), also run webhook + if await self.set_state( + "running", + status, + crawl, + allowed_from=["starting", "waiting_capacity"], + ): + if not crawl.qa_source_crawl_id: + self.run_task( + self.event_webhook_ops.create_crawl_started_notification( + crawl.id, crawl.oid, scheduled=crawl.scheduled ) - else: - self.run_task( - self.event_webhook_ops.create_qa_analysis_started_notification( - crawl.id, crawl.oid, crawl.qa_source_crawl_id - ) + ) + else: + self.run_task( + self.event_webhook_ops.create_qa_analysis_started_notification( + crawl.id, crawl.oid, crawl.qa_source_crawl_id ) + ) # update lastActiveTime if crawler is running if crawler_running: