feat(wait): a run's end, for the background beside a detached run
A run launched detached, as the prepush skill says, tells the session nothing when it ends: a round finished unread for hours. `crafter-review wait --artifact <artifact> --hash <hash>`, the two values of the run line, follows the hash's run lock. A full run holds it until it ends; taking it, the run now clears the last run's identity there, and it names itself with its run number once it has one. When a run the wait has seen hold or take the lock lets it go, the wait reads the identity - under its own instant shared hold, so never a rewrite in flight - prints the summary of the final dump of the run the identity names and exits with the code its verdict maps to, as run's does: 2 when there is no final dump or no run number, 3 with the message of a preflight check that stopped the run after it took the lock, which the run adds to the identity. A wait that sees no run within --appear (120 s by default) exits 3. Every probe of the lock, a test or dry run's too, takes it shared for an instant, and the run lock is tried for 200 ms before another driver counts as its holder. A wait only reads: one that is stopped is started again and the run goes on. freeze prints the wait line before the run line, and the skill and the process start the wait in the background, then launch the run detached. Released as 1.2.0.
This commit is contained in:
parent
ad33242609
commit
4318c7d8f2
15 changed files with 607 additions and 33 deletions
|
|
@ -155,12 +155,25 @@ namespace Crafter::Review::State {
|
|||
const fs::path path = LockPath(gateDir, mode, hash8);
|
||||
fd = open(path.c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0644);
|
||||
if (fd < 0) throw std::runtime_error(std::format("{}: {}", Dyn::ErrnoText(errno), Dyn::Repr(std::string_view(path.string()))));
|
||||
if (flock(fd, LOCK_EX | LOCK_NB) != 0) {
|
||||
bool locked = flock(fd, LOCK_EX | LOCK_NB) == 0;
|
||||
for (std::int32_t attempt = 0; !locked && attempt < 20; ++attempt) {
|
||||
std::this_thread::sleep_for(std::chrono::milliseconds(10));
|
||||
locked = flock(fd, LOCK_EX | LOCK_NB) == 0;
|
||||
}
|
||||
if (!locked) {
|
||||
const Json info = ReadLockInfo(path);
|
||||
close(fd);
|
||||
fd = -1;
|
||||
throw RunLocked(Describe(info));
|
||||
}
|
||||
// The last run's identity is not this run's: until it names itself,
|
||||
// the holder reads as one that has just started.
|
||||
if (ftruncate(fd, 0) != 0) {
|
||||
const std::int32_t failure = errno;
|
||||
close(fd);
|
||||
fd = -1;
|
||||
throw std::runtime_error(std::format("{}: {}", Dyn::ErrnoText(failure), Dyn::Repr(std::string_view(path.string()))));
|
||||
}
|
||||
}
|
||||
|
||||
RunLock::~RunLock() {
|
||||
|
|
@ -168,6 +181,7 @@ namespace Crafter::Review::State {
|
|||
}
|
||||
|
||||
void RunLock::WriteInfo(const Json& info) {
|
||||
written = info;
|
||||
const std::string text = DumpSpaced(info);
|
||||
if (lseek(fd, 0, SEEK_SET) < 0 || ftruncate(fd, 0) != 0) return;
|
||||
std::size_t written = 0;
|
||||
|
|
@ -181,18 +195,34 @@ namespace Crafter::Review::State {
|
|||
}
|
||||
}
|
||||
|
||||
std::optional<Json> LockHolder(const fs::path& gateDir, std::string_view mode, std::string_view hash8) {
|
||||
const fs::path path = LockPath(gateDir, mode, hash8);
|
||||
void RunLock::AddInfo(std::string_view key, const Json& value) {
|
||||
if (!written.is_object()) return;
|
||||
Json info = written;
|
||||
info[std::string(key)] = value;
|
||||
WriteInfo(info);
|
||||
}
|
||||
|
||||
std::optional<LockState> ProbeRunLock(const fs::path& path) {
|
||||
const std::int32_t handle = open(path.c_str(), O_RDONLY | O_CLOEXEC);
|
||||
if (handle < 0) return std::nullopt;
|
||||
std::optional<Json> holder;
|
||||
if (flock(handle, LOCK_EX | LOCK_NB) == 0) {
|
||||
// Shared, for an instant: two probers never see each other, and a
|
||||
// starting run tries its exclusive lock for 200 ms (RunLock).
|
||||
LockState state;
|
||||
if (flock(handle, LOCK_SH | LOCK_NB) == 0) {
|
||||
state.info = ReadLockInfo(path);
|
||||
flock(handle, LOCK_UN);
|
||||
} else {
|
||||
holder = ReadLockInfo(path);
|
||||
state.held = true;
|
||||
state.info = ReadLockInfo(path);
|
||||
}
|
||||
close(handle);
|
||||
return holder;
|
||||
return state;
|
||||
}
|
||||
|
||||
std::optional<Json> LockHolder(const fs::path& gateDir, std::string_view mode, std::string_view hash8) {
|
||||
const std::optional<LockState> state = ProbeRunLock(LockPath(gateDir, mode, hash8));
|
||||
if (!state || !state->held) return std::nullopt;
|
||||
return state->info;
|
||||
}
|
||||
|
||||
std::int64_t NextRunNumber(const fs::path& gateDir, std::string_view mode, std::string_view hash8) {
|
||||
|
|
|
|||
Loading…
Reference in a new issue