@@ -84,11 +84,13 @@ def supports_check_run_worker_health(self):
8484 return True
8585
8686 def check_run_worker_health (self , _run ): # pyright: ignore[reportIncompatibleMethodOverride]
87- return (
88- CheckRunHealthResult (WorkerStatus .RUNNING , "" )
89- if os .environ .get ("DAGSTER_TEST_RUN_HEALTH_CHECK_RESULT" ) == "healthy"
90- else CheckRunHealthResult (WorkerStatus .NOT_FOUND , "" )
91- )
87+ health_check_env = os .environ .get ("DAGSTER_TEST_RUN_HEALTH_CHECK_RESULT" )
88+ if health_check_env == "healthy" :
89+ return CheckRunHealthResult (WorkerStatus .RUNNING , "" )
90+ elif health_check_env == "unknown" :
91+ return CheckRunHealthResult (WorkerStatus .UNKNOWN , "Cannot reach broker" )
92+ else :
93+ return CheckRunHealthResult (WorkerStatus .NOT_FOUND , "" )
9294
9395
9496@pytest .fixture
@@ -423,7 +425,7 @@ def test_long_running_termination(
423425 assert len (run_failure_events ) == 1
424426 event = run_failure_events [0 ].dagster_event
425427 assert event
426- assert event . message == "Exceeded maximum runtime of 500 seconds."
428+ assert "forcibly marked as failed" in event . message
427429
428430 monitor_started_run (instance , workspace , too_long_other_tag_value_record , logger )
429431 run = instance .get_run_by_id (too_long_other_tag_value_record .dagster_run .run_id )
@@ -440,7 +442,7 @@ def test_long_running_termination(
440442 assert len (run_failure_events ) == 1
441443 event = run_failure_events [0 ].dagster_event
442444 assert event
443- assert event . message == "Exceeded maximum runtime of 500 seconds."
445+ assert "forcibly marked as failed" in event . message
444446
445447 # Wait long enough for the instance default to kick in
446448 eval_time = started_time + datetime .timedelta (seconds = 751 )
@@ -562,3 +564,111 @@ def test_invalid_max_runtime_tag_value(
562564
563565 # Verify warning was logged
564566 assert "Invalid max runtime value: invalid" in caplog .text
567+
568+
569+ def test_monitor_started_unknown_below_threshold (
570+ instance : DagsterInstance , workspace_context : WorkspaceProcessContext , logger : Logger
571+ ):
572+ """UNKNOWN health check below threshold should NOT trigger resume — just log and wait."""
573+ run_id = create_run_for_test (instance , job_name = "foo" , status = DagsterRunStatus .STARTED ).run_id
574+ run_record = instance .get_run_record_by_id (run_id )
575+ assert run_record is not None
576+ workspace = workspace_context .create_request_context ()
577+ run_launcher = cast ("MockRunLauncher" , instance .run_launcher )
578+
579+ # First UNKNOWN check — below threshold of 3
580+ with environ ({"DAGSTER_TEST_RUN_HEALTH_CHECK_RESULT" : "unknown" }):
581+ monitor_started_run (instance , workspace , run_record , logger )
582+
583+ run = instance .get_run_by_id (run_id )
584+ assert run
585+ assert run .status == DagsterRunStatus .STARTED
586+ assert run_launcher .resume_run_calls == 0
587+
588+ # Verify UNKNOWN engine event was logged
589+ engine_events = instance .all_logs (run_id , of_type = DagsterEventType .ENGINE_EVENT )
590+ unknown_events = [e for e in engine_events if "UNKNOWN" in (e .message or "" )]
591+ assert len (unknown_events ) == 1
592+
593+
594+ def test_monitor_started_unknown_at_threshold (
595+ instance : DagsterInstance , workspace_context : WorkspaceProcessContext , logger : Logger
596+ ):
597+ """After reaching the UNKNOWN threshold (3), resume should be triggered."""
598+ run_id = create_run_for_test (instance , job_name = "foo" , status = DagsterRunStatus .STARTED ).run_id
599+ workspace = workspace_context .create_request_context ()
600+ run_launcher = cast ("MockRunLauncher" , instance .run_launcher )
601+
602+ with environ ({"DAGSTER_TEST_RUN_HEALTH_CHECK_RESULT" : "unknown" }):
603+ # Checks 1, 2 — below threshold, no action
604+ for _ in range (2 ):
605+ run_record = instance .get_run_record_by_id (run_id )
606+ assert run_record is not None
607+ monitor_started_run (instance , workspace , run_record , logger )
608+
609+ assert run_launcher .resume_run_calls == 0
610+
611+ # Check 3 — at threshold, should trigger resume
612+ run_record = instance .get_run_record_by_id (run_id )
613+ assert run_record is not None
614+ monitor_started_run (instance , workspace , run_record , logger )
615+
616+ assert run_launcher .resume_run_calls == 1
617+ run = instance .get_run_by_id (run_id )
618+ assert run
619+ assert run .status == DagsterRunStatus .STARTED
620+
621+
622+ def test_monitor_started_unknown_then_healthy_resets (
623+ instance : DagsterInstance , workspace_context : WorkspaceProcessContext , logger : Logger
624+ ):
625+ """UNKNOWN followed by healthy check should reset the counter."""
626+ run_id = create_run_for_test (instance , job_name = "foo" , status = DagsterRunStatus .STARTED ).run_id
627+ workspace = workspace_context .create_request_context ()
628+ run_launcher = cast ("MockRunLauncher" , instance .run_launcher )
629+
630+ # Two UNKNOWN checks
631+ with environ ({"DAGSTER_TEST_RUN_HEALTH_CHECK_RESULT" : "unknown" }):
632+ for _ in range (2 ):
633+ run_record = instance .get_run_record_by_id (run_id )
634+ assert run_record is not None
635+ monitor_started_run (instance , workspace , run_record , logger )
636+
637+ assert run_launcher .resume_run_calls == 0
638+
639+ # Healthy check — should not trigger resume and resets are implicit
640+ # (next UNKNOWN streak starts from 0)
641+ with environ ({"DAGSTER_TEST_RUN_HEALTH_CHECK_RESULT" : "healthy" }):
642+ run_record = instance .get_run_record_by_id (run_id )
643+ assert run_record is not None
644+ monitor_started_run (instance , workspace , run_record , logger )
645+
646+ assert run_launcher .resume_run_calls == 0
647+
648+ # Two more UNKNOWN checks — should NOT trigger resume (reset happened)
649+ with environ ({"DAGSTER_TEST_RUN_HEALTH_CHECK_RESULT" : "unknown" }):
650+ for _ in range (2 ):
651+ run_record = instance .get_run_record_by_id (run_id )
652+ assert run_record is not None
653+ monitor_started_run (instance , workspace , run_record , logger )
654+
655+ assert run_launcher .resume_run_calls == 0
656+
657+
658+ def test_monitor_started_failed_still_immediate (
659+ instance : DagsterInstance , workspace_context : WorkspaceProcessContext , logger : Logger
660+ ):
661+ """FAILED/NOT_FOUND status should still trigger immediate resume (no threshold)."""
662+ run_id = create_run_for_test (instance , job_name = "foo" , status = DagsterRunStatus .STARTED ).run_id
663+ run_record = instance .get_run_record_by_id (run_id )
664+ assert run_record is not None
665+ workspace = workspace_context .create_request_context ()
666+ run_launcher = cast ("MockRunLauncher" , instance .run_launcher )
667+
668+ # NOT_FOUND triggers immediate resume on first check
669+ monitor_started_run (instance , workspace , run_record , logger )
670+
671+ assert run_launcher .resume_run_calls == 1
672+ run = instance .get_run_by_id (run_id )
673+ assert run
674+ assert run .status == DagsterRunStatus .STARTED
0 commit comments