feat: add duration parser, ISO timestamps, job state machine; add AGENTS.md
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
9e5c3e3af1
commit
8cf40344a4
6 files changed
+261
-16
No files matched your search
@@ -0,0 +1,65 @@
|
||||
//! Job states and the allowed transitions between them.
|
||||
const std = @import("std");
|
||||
|
||||
pub const State = enum {
|
||||
queued,
|
||||
running,
|
||||
succeeded,
|
||||
failed,
|
||||
canceled,
|
||||
killed,
|
||||
|
||||
pub fn isTerminal(self: State) bool {
|
||||
return switch (self) {
|
||||
.succeeded, .canceled => true,
|
||||
else => false,
|
||||
};
|
||||
}
|
||||
|
||||
/// Lowercase form used in the `state` file and in `describe` output.
|
||||
pub fn lower(self: State) []const u8 {
|
||||
return @tagName(self);
|
||||
}
|
||||
|
||||
/// Parses a state name, case-insensitively.
|
||||
pub fn parse(text: []const u8) ?State {
|
||||
inline for (@typeInfo(State).@"enum".fields) |f| {
|
||||
if (std.ascii.eqlIgnoreCase(text, f.name)) return @field(State, f.name);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/// QUEUED -> RUNNING | FAILED | CANCELED
|
||||
/// RUNNING -> SUCCEEDED | FAILED | CANCELED | KILLED
|
||||
/// FAILED | KILLED -> QUEUED (retry)
|
||||
pub fn canTransitionTo(self: State, next: State) bool {
|
||||
return switch (self) {
|
||||
.queued => next == .running or next == .failed or next == .canceled,
|
||||
.running => next == .succeeded or next == .failed or next == .canceled or next == .killed,
|
||||
.failed, .killed => next == .queued,
|
||||
.succeeded, .canceled => false,
|
||||
};
|
||||
}
|
||||
};
|
||||
|
||||
test "valid transitions" {
|
||||
try std.testing.expect(State.queued.canTransitionTo(.running));
|
||||
try std.testing.expect(State.running.canTransitionTo(.killed));
|
||||
try std.testing.expect(State.failed.canTransitionTo(.queued));
|
||||
try std.testing.expect(State.killed.canTransitionTo(.queued));
|
||||
}
|
||||
|
||||
test "invalid transitions" {
|
||||
try std.testing.expect(!State.succeeded.canTransitionTo(.queued));
|
||||
try std.testing.expect(!State.canceled.canTransitionTo(.running));
|
||||
try std.testing.expect(!State.queued.canTransitionTo(.succeeded));
|
||||
try std.testing.expect(!State.failed.canTransitionTo(.running));
|
||||
}
|
||||
|
||||
test "parse and terminal" {
|
||||
try std.testing.expectEqual(State.running, State.parse("RUNNING").?);
|
||||
try std.testing.expectEqual(State.killed, State.parse("killed").?);
|
||||
try std.testing.expect(State.parse("bogus") == null);
|
||||
try std.testing.expect(State.succeeded.isTerminal());
|
||||
try std.testing.expect(!State.failed.isTerminal());
|
||||
}
|
||||
Reference in new issue
Block a user