@@ -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