Implement configuration system #2

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

Imported from GitHub samson-media/barn#2 — opened by @daniel-samson on 2026-01-21 12:35:05 UTC

Overview

Implement the TOML configuration parser and configuration model for Barn.

Context

  • Read first: docs/config.md (defines all config options)
  • Depends on: #1 (project structure)

Requirements

1. Configuration Model

Create records/classes in com.samsonmedia.barn.config/:

// Config.java - top-level configuration
public record Config(
    ServiceConfig service,
    JobsConfig jobs,
    CleanupConfig cleanup,
    StorageConfig storage
) { }

// ServiceConfig.java
public record ServiceConfig(
    LogLevel logLevel,        // debug, info, warn, error
    int maxConcurrentJobs,    // default: 4
    int heartbeatIntervalSeconds,  // default: 5
    Path ipcSocket,           // platform-specific default
    int staleHeartbeatThresholdSeconds  // default: 30
) { }

// JobsConfig.java
public record JobsConfig(
    int defaultTimeoutSeconds,     // default: 3600
    int maxRetries,                // default: 3
    int retryDelaySeconds,         // default: 30
    double retryBackoffMultiplier, // default: 2.0
    List<Integer> retryOnExitCodes // default: empty (retry all)
) { }

// CleanupConfig.java
public record CleanupConfig(
    boolean enabled,           // default: true
    int maxAgeHours,          // default: 72
    int cleanupIntervalMinutes, // default: 60
    boolean keepFailedJobs,    // default: true
    int keepFailedJobsHours   // default: 168
) { }

// StorageConfig.java
public record StorageConfig(
    Path baseDir,             // default: platform temp/barn
    int maxDiskUsageGb,       // default: 50
    boolean preserveWorkDir   // default: false
) { }

2. Configuration Loader

ConfigLoader.java:

  • Load from file paths in priority order (see docs/config.md)
  • Support --config CLI override
  • Parse TOML format (use existing library or simple parser)
  • Apply defaults for missing values
  • Validate all values

3. Environment Variable Override

Pattern: BARN_<SECTION>_<KEY> (uppercase, underscores)

// Example: BARN_SERVICE_LOG_LEVEL=debug overrides [service] log_level

4. Platform-Specific Defaults

public final class ConfigDefaults {
    public static Path getDefaultConfigPath() {
        return switch (OS.current()) {
            case LINUX -> Path.of("/etc/barn/barn.conf");
            case MACOS -> Path.of("/etc/barn/barn.conf");
            case WINDOWS -> Path.of(System.getenv("PROGRAMDATA"), "barn", "barn.conf");
        };
    }
    
    public static Path getDefaultIpcSocket() {
        return switch (OS.current()) {
            case LINUX, MACOS -> Path.of("/tmp/barn/barn.sock");
            case WINDOWS -> Path.of("\\\\.\\pipe\\barn");
        };
    }
}

5. Tests Required

  • ConfigTest.java: Test record creation with defaults
  • ConfigLoaderTest.java: Test file loading, env override, validation
  • ConfigDefaultsTest.java: Test platform detection

Acceptance Criteria

  • All config options from docs/config.md are supported
  • Environment variables override file values
  • Missing config file uses defaults (no error)
  • Invalid config values produce clear error messages
  • mvn verify passes
  • Tests have 80%+ coverage

Implementation Notes

For TOML parsing, consider:

  • Simple regex-based parser for the subset we use
  • Or add toml4j dependency

Validation should check:

  • Positive integers where expected
  • Valid log levels
  • Paths are absolute or can be resolved
  • Multiplier > 1.0

Definition of Done

  • All config options parseable
  • Defaults work without config file
  • Environment overrides work
  • Validation catches invalid values
  • All tests pass
<!-- github-import:issue:2 --> _Imported from GitHub samson-media/barn#2 — opened by **@daniel-samson** on 2026-01-21 12:35:05 UTC_ ## Overview Implement the TOML configuration parser and configuration model for Barn. ## Context - **Read first**: `docs/config.md` (defines all config options) - **Depends on**: #1 (project structure) ## Requirements ### 1. Configuration Model Create records/classes in `com.samsonmedia.barn.config/`: ```java // Config.java - top-level configuration public record Config( ServiceConfig service, JobsConfig jobs, CleanupConfig cleanup, StorageConfig storage ) { } // ServiceConfig.java public record ServiceConfig( LogLevel logLevel, // debug, info, warn, error int maxConcurrentJobs, // default: 4 int heartbeatIntervalSeconds, // default: 5 Path ipcSocket, // platform-specific default int staleHeartbeatThresholdSeconds // default: 30 ) { } // JobsConfig.java public record JobsConfig( int defaultTimeoutSeconds, // default: 3600 int maxRetries, // default: 3 int retryDelaySeconds, // default: 30 double retryBackoffMultiplier, // default: 2.0 List<Integer> retryOnExitCodes // default: empty (retry all) ) { } // CleanupConfig.java public record CleanupConfig( boolean enabled, // default: true int maxAgeHours, // default: 72 int cleanupIntervalMinutes, // default: 60 boolean keepFailedJobs, // default: true int keepFailedJobsHours // default: 168 ) { } // StorageConfig.java public record StorageConfig( Path baseDir, // default: platform temp/barn int maxDiskUsageGb, // default: 50 boolean preserveWorkDir // default: false ) { } ``` ### 2. Configuration Loader `ConfigLoader.java`: - Load from file paths in priority order (see docs/config.md) - Support `--config` CLI override - Parse TOML format (use existing library or simple parser) - Apply defaults for missing values - Validate all values ### 3. Environment Variable Override Pattern: `BARN_<SECTION>_<KEY>` (uppercase, underscores) ```java // Example: BARN_SERVICE_LOG_LEVEL=debug overrides [service] log_level ``` ### 4. Platform-Specific Defaults ```java public final class ConfigDefaults { public static Path getDefaultConfigPath() { return switch (OS.current()) { case LINUX -> Path.of("/etc/barn/barn.conf"); case MACOS -> Path.of("/etc/barn/barn.conf"); case WINDOWS -> Path.of(System.getenv("PROGRAMDATA"), "barn", "barn.conf"); }; } public static Path getDefaultIpcSocket() { return switch (OS.current()) { case LINUX, MACOS -> Path.of("/tmp/barn/barn.sock"); case WINDOWS -> Path.of("\\\\.\\pipe\\barn"); }; } } ``` ### 5. Tests Required - `ConfigTest.java`: Test record creation with defaults - `ConfigLoaderTest.java`: Test file loading, env override, validation - `ConfigDefaultsTest.java`: Test platform detection ## Acceptance Criteria - [ ] All config options from docs/config.md are supported - [ ] Environment variables override file values - [ ] Missing config file uses defaults (no error) - [ ] Invalid config values produce clear error messages - [ ] `mvn verify` passes - [ ] Tests have 80%+ coverage ## Implementation Notes For TOML parsing, consider: - Simple regex-based parser for the subset we use - Or add `toml4j` dependency Validation should check: - Positive integers where expected - Valid log levels - Paths are absolute or can be resolved - Multiplier > 1.0 ## Definition of Done - All config options parseable - Defaults work without config file - Environment overrides work - Validation catches invalid values - All tests pass
daniel added this to the M1: Foundation milestone 2026-10-04 19:27:52 +00:00
daniel added the foundation label 2026-10-04 19:27:52 +00:00
Author
Owner

@daniel-samson commented on 2026-01-21 12:59:23 UTC:

Completed implementation:

Deliverables:

  • ✅ Config records: Config, ServiceConfig, JobsConfig, CleanupConfig, StorageConfig
  • ✅ LogLevel enum with case-insensitive parsing
  • ✅ OperatingSystem enum with platform detection
  • ✅ ConfigDefaults for platform-specific paths
  • ✅ TomlParser supporting all required data types
  • ✅ ConfigLoader with file loading, env overrides, and validation
  • ✅ 93 new tests (119 total), 80%+ coverage
  • ✅ mvn verify passes

Commit: 79f89be

<!-- github-import:comment:3778018979 --> _**@daniel-samson** commented on 2026-01-21 12:59:23 UTC:_ Completed implementation: **Deliverables:** - ✅ Config records: Config, ServiceConfig, JobsConfig, CleanupConfig, StorageConfig - ✅ LogLevel enum with case-insensitive parsing - ✅ OperatingSystem enum with platform detection - ✅ ConfigDefaults for platform-specific paths - ✅ TomlParser supporting all required data types - ✅ ConfigLoader with file loading, env overrides, and validation - ✅ 93 new tests (119 total), 80%+ coverage - ✅ mvn verify passes **Commit:** 79f89be
This repo is archived. You cannot comment on issues.