Implement 'barn clean' command #13

Closed
opened 2026-10-04 19:28:11 +00:00 by daniel · 1 comment
Owner

Imported from GitHub samson-media/barn#13 — opened by @daniel-samson on 2026-01-21 12:39:53 UTC

Overview

Implement the barn clean command that removes completed jobs.

Context

  • Read first: docs/config.md (cleanup settings)
  • Depends on: #8 (CLI framework), #4 (job model), #3 (filesystem state)

Requirements

1. Clean Command

com.samsonmedia.barn.cli.CleanCommand.java:

@Command(name = "clean", description = "Remove completed jobs")
public class CleanCommand extends BaseCommand {
    @Option(names = {"--all"}, description = "Remove all completed jobs regardless of age")
    private boolean all;

    @Option(names = {"--older-than"}, description = "Remove jobs older than duration (e.g., 24h, 7d)")
    private String olderThan;

    @Option(names = {"--include-failed"}, description = "Also remove failed jobs")
    private boolean includeFailed;

    @Option(names = {"--dry-run"}, description = "Show what would be removed without removing")
    private boolean dryRun;

    @Option(names = {"--job-id"}, description = "Remove specific job")
    private String jobId;

    @Override
    public Integer call() {
        // 1. Find jobs matching criteria
        // 2. Filter by age/state
        // 3. Remove (or show dry-run)
        // 4. Output summary
    }
}

2. Usage Examples

# Remove completed jobs older than default max_age
barn clean

# Remove all completed jobs regardless of age
barn clean --all

# Remove jobs older than 24 hours
barn clean --older-than=24h

# Include failed jobs
barn clean --include-failed

# Dry run - show what would be removed
barn clean --dry-run

# Remove specific job
barn clean --job-id=job-9f83c

3. Age Duration Parser

Support human-readable durations:

  • 30m - 30 minutes
  • 24h - 24 hours
  • 7d - 7 days
  • 2w - 2 weeks
public static Duration parseDuration(String input) {
    // Parse "24h" -> Duration.ofHours(24)
}

4. Cleanup Rules

Job State Default Cleanup With --include-failed
succeeded After max_age_hours After max_age_hours
canceled After max_age_hours After max_age_hours
failed After keep_failed_jobs_hours After max_age_hours
running Never Never
queued Never Never

5. Output

Human format:

Cleaned 3 jobs:
  job-a7b2d (succeeded, 3 days old)
  job-c4e1f (failed, 5 days old)
  job-e8f3h (canceled, 2 days old)

Disk space freed: 1.2 GB

Dry-run format:

Would clean 3 jobs:
  job-a7b2d (succeeded, 3 days old) - 450 MB
  job-c4e1f (failed, 5 days old) - 320 MB
  job-e8f3h (canceled, 2 days old) - 430 MB

Would free: 1.2 GB

JSON format:

{
  "cleaned": [
    {"id": "job-a7b2d", "state": "succeeded", "age_hours": 72, "size_bytes": 471859200}
  ],
  "count": 3,
  "bytes_freed": 1288490188
}

6. Safety

  • Never remove running or queued jobs
  • Acquire job lock before removal
  • Log all removals to daemon log

7. Tests Required

  • CleanCommandTest.java: Test filtering, age parsing
  • DurationParserTest.java: Test duration string parsing
  • Integration test: Create jobs, clean them, verify removal

Acceptance Criteria

  • barn clean removes old completed jobs
  • --all removes all completed jobs
  • --older-than respects duration
  • --include-failed removes failed jobs
  • --dry-run shows preview without deleting
  • --job-id removes specific job
  • Never removes running/queued jobs
  • mvn verify passes
  • Tests have 80%+ coverage

Implementation Notes

Calculate directory size:

public static long directorySize(Path dir) throws IOException {
    return Files.walk(dir)
        .filter(Files::isRegularFile)
        .mapToLong(p -> p.toFile().length())
        .sum();
}

Format size:

public static String formatSize(long bytes) {
    if (bytes < 1024) return bytes + " B";
    if (bytes < 1024 * 1024) return bytes / 1024 + " KB";
    if (bytes < 1024 * 1024 * 1024) return bytes / (1024 * 1024) + " MB";
    return String.format("%.1f GB", bytes / (1024.0 * 1024 * 1024));
}

Definition of Done

  • Clean removes jobs correctly
  • Filtering options work
  • Dry-run is accurate
  • All tests pass
<!-- github-import:issue:13 --> _Imported from GitHub samson-media/barn#13 — opened by **@daniel-samson** on 2026-01-21 12:39:53 UTC_ ## Overview Implement the `barn clean` command that removes completed jobs. ## Context - **Read first**: `docs/config.md` (cleanup settings) - **Depends on**: #8 (CLI framework), #4 (job model), #3 (filesystem state) ## Requirements ### 1. Clean Command `com.samsonmedia.barn.cli.CleanCommand.java`: ```java @Command(name = "clean", description = "Remove completed jobs") public class CleanCommand extends BaseCommand { @Option(names = {"--all"}, description = "Remove all completed jobs regardless of age") private boolean all; @Option(names = {"--older-than"}, description = "Remove jobs older than duration (e.g., 24h, 7d)") private String olderThan; @Option(names = {"--include-failed"}, description = "Also remove failed jobs") private boolean includeFailed; @Option(names = {"--dry-run"}, description = "Show what would be removed without removing") private boolean dryRun; @Option(names = {"--job-id"}, description = "Remove specific job") private String jobId; @Override public Integer call() { // 1. Find jobs matching criteria // 2. Filter by age/state // 3. Remove (or show dry-run) // 4. Output summary } } ``` ### 2. Usage Examples ```bash # Remove completed jobs older than default max_age barn clean # Remove all completed jobs regardless of age barn clean --all # Remove jobs older than 24 hours barn clean --older-than=24h # Include failed jobs barn clean --include-failed # Dry run - show what would be removed barn clean --dry-run # Remove specific job barn clean --job-id=job-9f83c ``` ### 3. Age Duration Parser Support human-readable durations: - `30m` - 30 minutes - `24h` - 24 hours - `7d` - 7 days - `2w` - 2 weeks ```java public static Duration parseDuration(String input) { // Parse "24h" -> Duration.ofHours(24) } ``` ### 4. Cleanup Rules | Job State | Default Cleanup | With --include-failed | |-----------|-----------------|----------------------| | succeeded | After max_age_hours | After max_age_hours | | canceled | After max_age_hours | After max_age_hours | | failed | After keep_failed_jobs_hours | After max_age_hours | | running | Never | Never | | queued | Never | Never | ### 5. Output Human format: ``` Cleaned 3 jobs: job-a7b2d (succeeded, 3 days old) job-c4e1f (failed, 5 days old) job-e8f3h (canceled, 2 days old) Disk space freed: 1.2 GB ``` Dry-run format: ``` Would clean 3 jobs: job-a7b2d (succeeded, 3 days old) - 450 MB job-c4e1f (failed, 5 days old) - 320 MB job-e8f3h (canceled, 2 days old) - 430 MB Would free: 1.2 GB ``` JSON format: ```json { "cleaned": [ {"id": "job-a7b2d", "state": "succeeded", "age_hours": 72, "size_bytes": 471859200} ], "count": 3, "bytes_freed": 1288490188 } ``` ### 6. Safety - Never remove running or queued jobs - Acquire job lock before removal - Log all removals to daemon log ### 7. Tests Required - `CleanCommandTest.java`: Test filtering, age parsing - `DurationParserTest.java`: Test duration string parsing - Integration test: Create jobs, clean them, verify removal ## Acceptance Criteria - [ ] `barn clean` removes old completed jobs - [ ] `--all` removes all completed jobs - [ ] `--older-than` respects duration - [ ] `--include-failed` removes failed jobs - [ ] `--dry-run` shows preview without deleting - [ ] `--job-id` removes specific job - [ ] Never removes running/queued jobs - [ ] `mvn verify` passes - [ ] Tests have 80%+ coverage ## Implementation Notes Calculate directory size: ```java public static long directorySize(Path dir) throws IOException { return Files.walk(dir) .filter(Files::isRegularFile) .mapToLong(p -> p.toFile().length()) .sum(); } ``` Format size: ```java public static String formatSize(long bytes) { if (bytes < 1024) return bytes + " B"; if (bytes < 1024 * 1024) return bytes / 1024 + " KB"; if (bytes < 1024 * 1024 * 1024) return bytes / (1024 * 1024) + " MB"; return String.format("%.1f GB", bytes / (1024.0 * 1024 * 1024)); } ``` ## Definition of Done - Clean removes jobs correctly - Filtering options work - Dry-run is accurate - All tests pass
daniel added this to the M3: CLI Commands milestone 2026-10-04 19:28:11 +00:00
daniel added the cli label 2026-10-04 19:28:11 +00:00
Author
Owner

@daniel-samson commented on 2026-01-21 14:29:20 UTC:

Implemented barn clean command with tests

<!-- github-import:comment:3778429074 --> _**@daniel-samson** commented on 2026-01-21 14:29:20 UTC:_ Implemented barn clean command with tests
This repo is archived. You cannot comment on issues.