Archived
feat(jobs): add KILLED state for orphaned jobs with auto-retry
When the barn service is terminated unexpectedly (crash, SIGKILL), jobs that were running are now marked as KILLED instead of FAILED. This distinguishes between jobs that failed due to their own errors vs jobs that were killed due to external factors. Key changes: - Add KILLED state to JobState enum - Update JobStateMachine with KILLED transitions (RUNNING→KILLED, KILLED→QUEUED) - CrashRecovery now marks orphaned jobs as KILLED and auto-retries them - Add markKilled() method to JobRepository - Add killed count to service status output - Update HumanFormatter to display KILLED state in red The auto-retry behavior uses exponential backoff (configurable) and respects the maxRetries limit. Retry history is recorded in the retry_history file. Closes #25 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
27648a03a8
commit
f916fc2f8b
11 files changed
+206
-44
No files matched your search
@@ -14,6 +14,7 @@ import org.junit.jupiter.api.Nested;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.io.TempDir;
|
||||
|
||||
import com.samsonmedia.barn.config.Config;
|
||||
import com.samsonmedia.barn.config.JobsConfig;
|
||||
import com.samsonmedia.barn.jobs.Job;
|
||||
import com.samsonmedia.barn.jobs.JobRepository;
|
||||
@@ -33,7 +34,8 @@ class CrashRecoveryTest {
|
||||
private JobRepository repository;
|
||||
private HeartbeatChecker heartbeatChecker;
|
||||
private CrashRecovery crashRecovery;
|
||||
private JobsConfig config;
|
||||
private JobsConfig jobsConfig;
|
||||
private Config config;
|
||||
|
||||
@BeforeEach
|
||||
void setUp() throws IOException {
|
||||
@@ -41,8 +43,9 @@ class CrashRecoveryTest {
|
||||
dirs.initialize();
|
||||
repository = new JobRepository(dirs);
|
||||
heartbeatChecker = new HeartbeatChecker(Duration.ofSeconds(30));
|
||||
crashRecovery = new CrashRecovery(repository, heartbeatChecker, dirs);
|
||||
config = JobsConfig.withDefaults();
|
||||
config = Config.withDefaults();
|
||||
crashRecovery = new CrashRecovery(repository, heartbeatChecker, dirs, config);
|
||||
jobsConfig = JobsConfig.withDefaults();
|
||||
}
|
||||
|
||||
@Nested
|
||||
@@ -50,19 +53,25 @@ class CrashRecoveryTest {
|
||||
|
||||
@Test
|
||||
void constructor_withNullRepository_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new CrashRecovery(null, heartbeatChecker, dirs))
|
||||
assertThatThrownBy(() -> new CrashRecovery(null, heartbeatChecker, dirs, config))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withNullHeartbeatChecker_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new CrashRecovery(repository, null, dirs))
|
||||
assertThatThrownBy(() -> new CrashRecovery(repository, null, dirs, config))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withNullDirs_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new CrashRecovery(repository, heartbeatChecker, null))
|
||||
assertThatThrownBy(() -> new CrashRecovery(repository, heartbeatChecker, null, config))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void constructor_withNullConfig_shouldThrowException() {
|
||||
assertThatThrownBy(() -> new CrashRecovery(repository, heartbeatChecker, dirs, null))
|
||||
.isInstanceOf(NullPointerException.class);
|
||||
}
|
||||
}
|
||||
@@ -79,7 +88,7 @@ class CrashRecoveryTest {
|
||||
|
||||
@Test
|
||||
void recoverOrphanedJobs_withQueuedJobs_shouldNotRecover() throws IOException {
|
||||
repository.create(List.of("echo", "test"), "test", config);
|
||||
repository.create(List.of("echo", "test"), "test", jobsConfig);
|
||||
|
||||
List<Job> recovered = crashRecovery.recoverOrphanedJobs();
|
||||
|
||||
@@ -88,7 +97,7 @@ class CrashRecoveryTest {
|
||||
|
||||
@Test
|
||||
void recoverOrphanedJobs_withCompletedJobs_shouldNotRecover() throws IOException {
|
||||
Job job = repository.create(List.of("echo", "test"), "test", config);
|
||||
Job job = repository.create(List.of("echo", "test"), "test", jobsConfig);
|
||||
repository.markStarted(job.id(), 12345);
|
||||
repository.markCompleted(job.id(), 0, null);
|
||||
|
||||
@@ -99,7 +108,7 @@ class CrashRecoveryTest {
|
||||
|
||||
@Test
|
||||
void recoverOrphanedJobs_withRunningJobFreshHeartbeat_shouldNotRecover() throws IOException {
|
||||
Job job = repository.create(List.of("echo", "test"), "test", config);
|
||||
Job job = repository.create(List.of("echo", "test"), "test", jobsConfig);
|
||||
repository.markStarted(job.id(), ProcessUtils.getCurrentPid()); // Use current PID (alive)
|
||||
repository.updateHeartbeat(job.id(), Instant.now()); // Fresh heartbeat
|
||||
|
||||
@@ -110,7 +119,7 @@ class CrashRecoveryTest {
|
||||
|
||||
@Test
|
||||
void recoverOrphanedJobs_withRunningJobStaleHeartbeatDeadProcess_shouldRecover() throws IOException {
|
||||
Job job = repository.create(List.of("echo", "test"), "test", config);
|
||||
Job job = repository.create(List.of("echo", "test"), "test", jobsConfig);
|
||||
// Use a PID that's unlikely to exist (very high number)
|
||||
repository.markStarted(job.id(), 999999999L);
|
||||
// Set a stale heartbeat
|
||||
@@ -121,15 +130,15 @@ class CrashRecoveryTest {
|
||||
assertThat(recovered).hasSize(1);
|
||||
assertThat(recovered.get(0).id()).isEqualTo(job.id());
|
||||
|
||||
// Verify job is now marked as failed
|
||||
// Verify job was killed and auto-retried (state is QUEUED with retry count incremented)
|
||||
Job updatedJob = repository.findById(job.id()).orElseThrow();
|
||||
assertThat(updatedJob.state()).isEqualTo(JobState.FAILED);
|
||||
assertThat(updatedJob.error()).contains("orphaned");
|
||||
assertThat(updatedJob.state()).isEqualTo(JobState.QUEUED);
|
||||
assertThat(updatedJob.retryCount()).isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test
|
||||
void recoverOrphanedJobs_withRunningJobNoHeartbeatDeadProcess_shouldRecover() throws IOException {
|
||||
Job job = repository.create(List.of("echo", "test"), "test", config);
|
||||
Job job = repository.create(List.of("echo", "test"), "test", jobsConfig);
|
||||
repository.markStarted(job.id(), 999999999L);
|
||||
// Clear heartbeat to simulate crash before first heartbeat
|
||||
clearHeartbeat(job.id());
|
||||
@@ -142,7 +151,7 @@ class CrashRecoveryTest {
|
||||
|
||||
@Test
|
||||
void recoverOrphanedJobs_withRunningJobStaleHeartbeatAliveProcess_shouldNotRecover() throws IOException {
|
||||
Job job = repository.create(List.of("echo", "test"), "test", config);
|
||||
Job job = repository.create(List.of("echo", "test"), "test", jobsConfig);
|
||||
// Use current process PID which is definitely alive
|
||||
repository.markStarted(job.id(), ProcessUtils.getCurrentPid());
|
||||
// Set a stale heartbeat
|
||||
@@ -156,7 +165,7 @@ class CrashRecoveryTest {
|
||||
|
||||
@Test
|
||||
void recoverOrphanedJobs_withRunningJobNoPid_shouldRecover() throws IOException {
|
||||
Job job = repository.create(List.of("echo", "test"), "test", config);
|
||||
Job job = repository.create(List.of("echo", "test"), "test", jobsConfig);
|
||||
// Manually set running state without PID (simulates partial write)
|
||||
StateFiles stateFiles = new StateFiles(dirs.getJobDir(job.id()));
|
||||
stateFiles.writeState(JobState.RUNNING);
|
||||
@@ -171,7 +180,7 @@ class CrashRecoveryTest {
|
||||
void recoverOrphanedJobs_withMultipleOrphanedJobs_shouldRecoverAll() throws IOException {
|
||||
// Create 3 orphaned jobs
|
||||
for (int i = 0; i < 3; i++) {
|
||||
Job job = repository.create(List.of("echo", "test" + i), "test", config);
|
||||
Job job = repository.create(List.of("echo", "test" + i), "test", jobsConfig);
|
||||
repository.markStarted(job.id(), 999999999L + i);
|
||||
setStaleHeartbeat(job.id(), Duration.ofMinutes(5));
|
||||
}
|
||||
@@ -183,16 +192,41 @@ class CrashRecoveryTest {
|
||||
|
||||
@Test
|
||||
void recoverOrphanedJobs_shouldSetCorrectExitCode() throws IOException {
|
||||
Job job = repository.create(List.of("echo", "test"), "test", config);
|
||||
Job job = repository.create(List.of("echo", "test"), "test", jobsConfig);
|
||||
repository.markStarted(job.id(), 999999999L);
|
||||
setStaleHeartbeat(job.id(), Duration.ofMinutes(5));
|
||||
|
||||
crashRecovery.recoverOrphanedJobs();
|
||||
|
||||
Job updatedJob = repository.findById(job.id()).orElseThrow();
|
||||
// Exit code is stored as symbolic string, so we check error message
|
||||
assertThat(updatedJob.error()).contains("orphaned");
|
||||
assertThat(updatedJob.finishedAt()).isNotNull();
|
||||
// Verify the job was killed and auto-retried (state is QUEUED with retry count incremented)
|
||||
assertThat(updatedJob.state()).isEqualTo(JobState.QUEUED);
|
||||
assertThat(updatedJob.retryCount()).isEqualTo(1);
|
||||
assertThat(updatedJob.retryAt()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void recoverOrphanedJobs_withRetriesExhausted_shouldStayKilled() throws IOException {
|
||||
// Create a config with maxRetries = 0 so no auto-retry
|
||||
Config noRetryConfig = new Config(
|
||||
config.service(),
|
||||
new JobsConfig(config.jobs().defaultTimeoutSeconds(), 0, 1, 1.0, List.of()),
|
||||
config.cleanup(),
|
||||
config.storage()
|
||||
);
|
||||
CrashRecovery noRetryCrashRecovery = new CrashRecovery(
|
||||
repository, heartbeatChecker, dirs, noRetryConfig);
|
||||
|
||||
Job job = repository.create(List.of("echo", "test"), "test", jobsConfig);
|
||||
repository.markStarted(job.id(), 999999999L);
|
||||
setStaleHeartbeat(job.id(), Duration.ofMinutes(5));
|
||||
|
||||
noRetryCrashRecovery.recoverOrphanedJobs();
|
||||
|
||||
Job updatedJob = repository.findById(job.id()).orElseThrow();
|
||||
// With retries disabled, job should stay in KILLED state
|
||||
assertThat(updatedJob.state()).isEqualTo(JobState.KILLED);
|
||||
assertThat(updatedJob.error()).contains("killed");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -227,6 +227,35 @@ class JobRepositoryTest {
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class MarkKilled {
|
||||
|
||||
@Test
|
||||
void markKilled_shouldSetKilledState() throws IOException {
|
||||
Job job = repository.create(List.of("echo"), null, config);
|
||||
repository.markStarted(job.id(), 12345L);
|
||||
|
||||
repository.markKilled(job.id(), "Process killed - daemon restarted");
|
||||
|
||||
Job updated = repository.findById(job.id()).orElseThrow();
|
||||
assertThat(updated.state()).isEqualTo(JobState.KILLED);
|
||||
assertThat(updated.error()).isEqualTo("Process killed - daemon restarted");
|
||||
assertThat(updated.finishedAt()).isNotNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
void markKilled_withNullError_shouldSucceed() throws IOException {
|
||||
Job job = repository.create(List.of("echo"), null, config);
|
||||
repository.markStarted(job.id(), 12345L);
|
||||
|
||||
repository.markKilled(job.id(), null);
|
||||
|
||||
Job updated = repository.findById(job.id()).orElseThrow();
|
||||
assertThat(updated.state()).isEqualTo(JobState.KILLED);
|
||||
assertThat(updated.error()).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
class ScheduleRetry {
|
||||
|
||||
@@ -244,6 +273,21 @@ class JobRepositoryTest {
|
||||
assertThat(updated.retryCount()).isEqualTo(1);
|
||||
assertThat(updated.retryAt()).isEqualTo(retryAt);
|
||||
}
|
||||
|
||||
@Test
|
||||
void scheduleRetry_fromKilledState_shouldTransitionToQueued() throws IOException {
|
||||
Job job = repository.create(List.of("echo"), null, config);
|
||||
repository.markStarted(job.id(), 12345L);
|
||||
repository.markKilled(job.id(), "Daemon restarted");
|
||||
Instant retryAt = Instant.now().plusSeconds(30);
|
||||
|
||||
repository.scheduleRetry(job.id(), retryAt);
|
||||
|
||||
Job updated = repository.findById(job.id()).orElseThrow();
|
||||
assertThat(updated.state()).isEqualTo(JobState.QUEUED);
|
||||
assertThat(updated.retryCount()).isEqualTo(1);
|
||||
assertThat(updated.retryAt()).isEqualTo(retryAt);
|
||||
}
|
||||
}
|
||||
|
||||
@Nested
|
||||
|
||||
@@ -44,11 +44,28 @@ class JobStateMachineTest {
|
||||
JobStateMachine.validateTransition(JobState.RUNNING, JobState.CANCELED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void running_toKilled_shouldBeValid() {
|
||||
JobStateMachine.validateTransition(JobState.RUNNING, JobState.KILLED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void failed_toQueued_shouldBeValid() {
|
||||
JobStateMachine.validateTransition(JobState.FAILED, JobState.QUEUED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void killed_toQueued_shouldBeValid() {
|
||||
JobStateMachine.validateTransition(JobState.KILLED, JobState.QUEUED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void killed_toRunning_shouldThrowException() {
|
||||
assertThatThrownBy(() ->
|
||||
JobStateMachine.validateTransition(JobState.KILLED, JobState.RUNNING))
|
||||
.isInstanceOf(IllegalStateException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
void queued_toSucceeded_shouldThrowException() {
|
||||
assertThatThrownBy(() ->
|
||||
@@ -111,7 +128,9 @@ class JobStateMachineTest {
|
||||
assertThat(JobStateMachine.isValidTransition(JobState.RUNNING, JobState.SUCCEEDED)).isTrue();
|
||||
assertThat(JobStateMachine.isValidTransition(JobState.RUNNING, JobState.FAILED)).isTrue();
|
||||
assertThat(JobStateMachine.isValidTransition(JobState.RUNNING, JobState.CANCELED)).isTrue();
|
||||
assertThat(JobStateMachine.isValidTransition(JobState.RUNNING, JobState.KILLED)).isTrue();
|
||||
assertThat(JobStateMachine.isValidTransition(JobState.FAILED, JobState.QUEUED)).isTrue();
|
||||
assertThat(JobStateMachine.isValidTransition(JobState.KILLED, JobState.QUEUED)).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -119,6 +138,7 @@ class JobStateMachineTest {
|
||||
assertThat(JobStateMachine.isValidTransition(JobState.QUEUED, JobState.SUCCEEDED)).isFalse();
|
||||
assertThat(JobStateMachine.isValidTransition(JobState.SUCCEEDED, JobState.QUEUED)).isFalse();
|
||||
assertThat(JobStateMachine.isValidTransition(JobState.CANCELED, JobState.RUNNING)).isFalse();
|
||||
assertThat(JobStateMachine.isValidTransition(JobState.KILLED, JobState.RUNNING)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -138,9 +158,9 @@ class JobStateMachineTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
void forRunning_shouldReturnSucceededFailedCanceled() {
|
||||
void forRunning_shouldReturnSucceededFailedCanceledKilled() {
|
||||
assertThat(JobStateMachine.getValidTransitions(JobState.RUNNING))
|
||||
.containsExactlyInAnyOrder(JobState.SUCCEEDED, JobState.FAILED, JobState.CANCELED);
|
||||
.containsExactlyInAnyOrder(JobState.SUCCEEDED, JobState.FAILED, JobState.CANCELED, JobState.KILLED);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -159,6 +179,12 @@ class JobStateMachineTest {
|
||||
assertThat(JobStateMachine.getValidTransitions(JobState.CANCELED)).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void forKilled_shouldReturnQueued() {
|
||||
assertThat(JobStateMachine.getValidTransitions(JobState.KILLED))
|
||||
.containsExactly(JobState.QUEUED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void withNull_shouldThrowException() {
|
||||
assertThatThrownBy(() -> JobStateMachine.getValidTransitions(null))
|
||||
|
||||
@@ -17,6 +17,7 @@ class JobStateTest {
|
||||
assertThat(JobState.fromString("succeeded")).isEqualTo(JobState.SUCCEEDED);
|
||||
assertThat(JobState.fromString("failed")).isEqualTo(JobState.FAILED);
|
||||
assertThat(JobState.fromString("canceled")).isEqualTo(JobState.CANCELED);
|
||||
assertThat(JobState.fromString("killed")).isEqualTo(JobState.KILLED);
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -54,7 +55,7 @@ class JobStateTest {
|
||||
assertThatThrownBy(() -> JobState.fromString("invalid"))
|
||||
.isInstanceOf(IllegalArgumentException.class)
|
||||
.hasMessageContaining("Invalid job state")
|
||||
.hasMessageContaining("queued, running, succeeded, failed, canceled");
|
||||
.hasMessageContaining("queued, running, succeeded, failed, canceled, killed");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -64,6 +65,7 @@ class JobStateTest {
|
||||
assertThat(JobState.SUCCEEDED.toLowercase()).isEqualTo("succeeded");
|
||||
assertThat(JobState.FAILED.toLowercase()).isEqualTo("failed");
|
||||
assertThat(JobState.CANCELED.toLowercase()).isEqualTo("canceled");
|
||||
assertThat(JobState.KILLED.toLowercase()).isEqualTo("killed");
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -71,6 +73,7 @@ class JobStateTest {
|
||||
assertThat(JobState.SUCCEEDED.isTerminal()).isTrue();
|
||||
assertThat(JobState.FAILED.isTerminal()).isTrue();
|
||||
assertThat(JobState.CANCELED.isTerminal()).isTrue();
|
||||
assertThat(JobState.KILLED.isTerminal()).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -90,5 +93,6 @@ class JobStateTest {
|
||||
assertThat(JobState.SUCCEEDED.isActive()).isFalse();
|
||||
assertThat(JobState.FAILED.isActive()).isFalse();
|
||||
assertThat(JobState.CANCELED.isActive()).isFalse();
|
||||
assertThat(JobState.KILLED.isActive()).isFalse();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user