@@ -199,7 +199,22 @@ def fetch_task_commands(conn, task_id):
199
199
cfbot_work_queue .insert_work_queue (cursor , "fetch-task-logs" , task_id )
200
200
201
201
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
+ )
203
218
204
219
205
220
# Compute backoff. Called when the current active build completes.
@@ -494,16 +509,15 @@ def ingest_webhook(conn, event_type, event):
494
509
# build_status,
495
510
# )
496
511
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
501
514
):
502
515
if existing_build_status != old_build_status :
503
516
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... " ,
505
518
build_id ,
506
519
old_build_status ,
520
+ build_status ,
507
521
existing_build_status ,
508
522
build_status ,
509
523
)
@@ -600,12 +614,23 @@ def ingest_webhook(conn, event_type, event):
600
614
# task_status,
601
615
# )
602
616
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
+
605
630
process_new_task_status (
606
631
cursor ,
607
632
task_id ,
608
- old_task_status ,
633
+ existing_task_status ,
609
634
task_status ,
610
635
"webhook" ,
611
636
task_timestamp ,
0 commit comments