Skip to content

Commit 17bd0e0

Browse files
macdiceCommitfest Bot
authored andcommitted
Tolerate some more out of sync webhooks.
Trying to avoid all polling in practice... let's see.
1 parent f6df500 commit 17bd0e0

File tree

1 file changed

+34
-9
lines changed

1 file changed

+34
-9
lines changed

cfbot_cirrus.py

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,22 @@ def fetch_task_commands(conn, task_id):
199199
cfbot_work_queue.insert_work_queue(cursor, "fetch-task-logs", task_id)
200200

201201

202-
PRE_EXECUTING_STATUSES = ("CREATED", "TRIGGERED", "SCHEDULED")
202+
# Sometimes we seem to miss a webhook in the early fast-changing moments, or
203+
# even receive two out of order. Allow statuses to be skipped in limited cases
204+
# of early transitions where we know the only possible order. This avoids some
205+
# unnecessary polling.
206+
207+
208+
def build_status_follows(a, b):
209+
STATUSES = ("CREATED", "TRIGGERED", "EXECUTING")
210+
return a in STATUSES and b in STATUSES and STATUSES.index(a) < STATUSES.index(b)
211+
212+
213+
def task_status_follows(a, b):
214+
STATUSES = ("CREATED", "TRIGGERED", "SCHEDULED", "EXECUTING")
215+
return a == b or (
216+
a in STATUSES and b in STATUSES and STATUSES.index(a) < STATUSES.index(b)
217+
)
203218

204219

205220
# Compute backoff. Called when the current active build completes.
@@ -494,16 +509,15 @@ def ingest_webhook(conn, event_type, event):
494509
# build_status,
495510
# )
496511
return
497-
elif existing_build_status == old_build_status or (
498-
build_status == "EXECUTING"
499-
and existing_build_status in PRE_EXECUTING_STATUSES
500-
and old_build_status in PRE_EXECUTING_STATUSES
512+
elif existing_build_status == old_build_status or build_status_follows(
513+
existing_build_status, build_status
501514
):
502515
if existing_build_status != old_build_status:
503516
logging.info(
504-
"webhook out of sync, build %s expected to have %s but it has %s, assuming dropped webhooks and allowing transition to %s",
517+
"webhook for build %s wanted %s -> %s but we have %s -> %s, allowing it...",
505518
build_id,
506519
old_build_status,
520+
build_status,
507521
existing_build_status,
508522
build_status,
509523
)
@@ -600,12 +614,23 @@ def ingest_webhook(conn, event_type, event):
600614
# task_status,
601615
# )
602616
pass
603-
elif existing_task_status == old_task_status:
604-
# we have the expected old value, common case
617+
elif existing_task_status == old_task_status or task_status_follows(
618+
existing_task_status, task_status
619+
):
620+
if existing_task_status != old_task_status:
621+
logging.info(
622+
"webhook for task %s wanted %s -> %s but we have %s -> %s, allowing it...",
623+
task_id,
624+
old_task_status,
625+
task_status,
626+
existing_task_status,
627+
task_status,
628+
)
629+
605630
process_new_task_status(
606631
cursor,
607632
task_id,
608-
old_task_status,
633+
existing_task_status,
609634
task_status,
610635
"webhook",
611636
task_timestamp,

0 commit comments

Comments
 (0)