fix(jobs): allow QUEUED -> FAILED state transition

Jobs can fail before they start running (e.g., command not found,
permission denied). Previously, the state machine only allowed
QUEUED -> RUNNING or CANCELED, so jobs that failed to start would
stay stuck in QUEUED state with the scheduler retrying endlessly.

Now jobs can transition directly from QUEUED to FAILED, properly
recording the error message for commands that cannot be executed.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
Daniel SamsonandClaude Opus 4.5 committed 2026-01-21 22:11:53 +00:00
1 parent 5d45b53abe
commit 555879be60
2 files changed
+8 -9

No files matched your search

@@ -12,7 +12,7 @@ import com.samsonmedia.barn.state.JobState;
* *
* <p>Valid transitions: * <p>Valid transitions:
* <ul> * <ul>
* <li>QUEUED → RUNNING, CANCELED</li> * <li>QUEUED → RUNNING, FAILED, CANCELED</li>
* <li>RUNNING → SUCCEEDED, FAILED, CANCELED</li> * <li>RUNNING → SUCCEEDED, FAILED, CANCELED</li>
* <li>FAILED → QUEUED (for retry)</li> * <li>FAILED → QUEUED (for retry)</li>
* <li>SUCCEEDED → (none, terminal)</li> * <li>SUCCEEDED → (none, terminal)</li>
@@ -22,7 +22,7 @@ import com.samsonmedia.barn.state.JobState;
public final class JobStateMachine { public final class JobStateMachine {
private static final Map<JobState, Set<JobState>> VALID_TRANSITIONS = Map.of( private static final Map<JobState, Set<JobState>> VALID_TRANSITIONS = Map.of(
JobState.QUEUED, Set.of(JobState.RUNNING, JobState.CANCELED), JobState.QUEUED, Set.of(JobState.RUNNING, JobState.FAILED, JobState.CANCELED),
JobState.RUNNING, Set.of(JobState.SUCCEEDED, JobState.FAILED, JobState.CANCELED), JobState.RUNNING, Set.of(JobState.SUCCEEDED, JobState.FAILED, JobState.CANCELED),
JobState.FAILED, Set.of(JobState.QUEUED), // For retry JobState.FAILED, Set.of(JobState.QUEUED), // For retry
JobState.SUCCEEDED, Set.of(), JobState.SUCCEEDED, Set.of(),
@@ -60,10 +60,9 @@ class JobStateMachineTest {
} }
@Test @Test
void queued_toFailed_shouldThrowException() { void queued_toFailed_shouldBeValid() {
assertThatThrownBy(() -> JobStateMachine.validateTransition(JobState.QUEUED, JobState.FAILED);
JobStateMachine.validateTransition(JobState.QUEUED, JobState.FAILED)) // No exception means valid - job can fail before starting (e.g., command not found)
.isInstanceOf(IllegalStateException.class);
} }
@Test @Test
@@ -107,6 +106,7 @@ class JobStateMachineTest {
@Test @Test
void validTransitions_shouldReturnTrue() { void validTransitions_shouldReturnTrue() {
assertThat(JobStateMachine.isValidTransition(JobState.QUEUED, JobState.RUNNING)).isTrue(); assertThat(JobStateMachine.isValidTransition(JobState.QUEUED, JobState.RUNNING)).isTrue();
assertThat(JobStateMachine.isValidTransition(JobState.QUEUED, JobState.FAILED)).isTrue();
assertThat(JobStateMachine.isValidTransition(JobState.QUEUED, JobState.CANCELED)).isTrue(); assertThat(JobStateMachine.isValidTransition(JobState.QUEUED, JobState.CANCELED)).isTrue();
assertThat(JobStateMachine.isValidTransition(JobState.RUNNING, JobState.SUCCEEDED)).isTrue(); assertThat(JobStateMachine.isValidTransition(JobState.RUNNING, JobState.SUCCEEDED)).isTrue();
assertThat(JobStateMachine.isValidTransition(JobState.RUNNING, JobState.FAILED)).isTrue(); assertThat(JobStateMachine.isValidTransition(JobState.RUNNING, JobState.FAILED)).isTrue();
@@ -117,7 +117,6 @@ class JobStateMachineTest {
@Test @Test
void invalidTransitions_shouldReturnFalse() { void invalidTransitions_shouldReturnFalse() {
assertThat(JobStateMachine.isValidTransition(JobState.QUEUED, JobState.SUCCEEDED)).isFalse(); assertThat(JobStateMachine.isValidTransition(JobState.QUEUED, JobState.SUCCEEDED)).isFalse();
assertThat(JobStateMachine.isValidTransition(JobState.QUEUED, JobState.FAILED)).isFalse();
assertThat(JobStateMachine.isValidTransition(JobState.SUCCEEDED, JobState.QUEUED)).isFalse(); assertThat(JobStateMachine.isValidTransition(JobState.SUCCEEDED, JobState.QUEUED)).isFalse();
assertThat(JobStateMachine.isValidTransition(JobState.CANCELED, JobState.RUNNING)).isFalse(); assertThat(JobStateMachine.isValidTransition(JobState.CANCELED, JobState.RUNNING)).isFalse();
} }
@@ -133,9 +132,9 @@ class JobStateMachineTest {
class GetValidTransitions { class GetValidTransitions {
@Test @Test
void forQueued_shouldReturnRunningAndCanceled() { void forQueued_shouldReturnRunningFailedAndCanceled() {
assertThat(JobStateMachine.getValidTransitions(JobState.QUEUED)) assertThat(JobStateMachine.getValidTransitions(JobState.QUEUED))
.containsExactlyInAnyOrder(JobState.RUNNING, JobState.CANCELED); .containsExactlyInAnyOrder(JobState.RUNNING, JobState.FAILED, JobState.CANCELED);
} }
@Test @Test