Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
66 lines
2.2 KiB
Zig
66 lines
2.2 KiB
Zig
//! 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());
|
|
}
|