O4: parallel workers, locks, provider quota (#12)
This commit was merged in pull request #12.
This commit is contained in:
1 parent
afafc41376
commit
06662a085f
10 files changed
+410
-43
No files matched your search
+56
-17
@@ -1,11 +1,15 @@
|
||||
mod adapter;
|
||||
mod lock;
|
||||
mod profile;
|
||||
mod run;
|
||||
mod state;
|
||||
|
||||
use anyhow::{Context, Result, bail};
|
||||
use clap::{Parser, Subcommand};
|
||||
use std::collections::VecDeque;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::sync::Mutex;
|
||||
use std::sync::atomic::{AtomicBool, AtomicUsize, Ordering};
|
||||
|
||||
#[derive(Parser)]
|
||||
#[command(version, about = "Run hx coding agents on tasks")]
|
||||
@@ -78,24 +82,59 @@ fn main() -> Result<std::process::ExitCode> {
|
||||
Cmd::Pull { profile, limit } => {
|
||||
let p = profile::Profile::load(&profile)?;
|
||||
let src = adapter::source(p.source.as_ref().context("profile has no [source]")?)?;
|
||||
let (mut ok, mut n) = (true, 0);
|
||||
for t in src.list()? {
|
||||
if limit.is_some_and(|l| n >= l) {
|
||||
break;
|
||||
let queue = Mutex::new(src.list()?.into_iter().collect::<VecDeque<_>>());
|
||||
let (ok, claimed) = (AtomicBool::new(true), AtomicUsize::new(0));
|
||||
let account = p.account(&profile).to_string();
|
||||
std::thread::scope(|s| {
|
||||
for _ in 0..p.workers {
|
||||
s.spawn(|| {
|
||||
let res = (|| -> Result<()> {
|
||||
loop {
|
||||
if lock::paused_until(&account) > state::now() {
|
||||
eprintln!(
|
||||
"account {account} paused (quota); leaving the rest queued"
|
||||
);
|
||||
return Ok(());
|
||||
}
|
||||
let Some(t) = queue.lock().unwrap().pop_front() else {
|
||||
return Ok(());
|
||||
};
|
||||
let Some(repo) = t.repo.clone().or_else(|| p.repo.clone()) else {
|
||||
eprintln!(
|
||||
"{}: no repo (task `repo` or profile `repo`), skipped",
|
||||
t.id
|
||||
);
|
||||
ok.store(false, Ordering::SeqCst);
|
||||
continue;
|
||||
};
|
||||
// task lock first: a second orch on the same queue skips what is already taken
|
||||
let Some(_held) = lock::task(&profile, &t.id)? else {
|
||||
continue;
|
||||
};
|
||||
if limit
|
||||
.is_some_and(|l| claimed.fetch_add(1, Ordering::SeqCst) >= l)
|
||||
{
|
||||
return Ok(());
|
||||
}
|
||||
if !src.claim(&t)? {
|
||||
continue;
|
||||
}
|
||||
let r = process(&t, &repo, &profile, &p, Some(src.as_ref()))?;
|
||||
if r.status != state::Status::Done
|
||||
&& r.status != state::Status::Requeued
|
||||
{
|
||||
ok.store(false, Ordering::SeqCst);
|
||||
}
|
||||
}
|
||||
})();
|
||||
if let Err(e) = res {
|
||||
eprintln!("worker: {e:#}");
|
||||
ok.store(false, Ordering::SeqCst);
|
||||
}
|
||||
});
|
||||
}
|
||||
let Some(repo) = t.repo.clone().or_else(|| p.repo.clone()) else {
|
||||
eprintln!("{}: no repo (task `repo` or profile `repo`), skipped", t.id);
|
||||
ok = false;
|
||||
continue;
|
||||
};
|
||||
if !src.claim(&t)? {
|
||||
continue;
|
||||
}
|
||||
n += 1;
|
||||
let r = process(&t, &repo, &profile, &p, Some(src.as_ref()))?;
|
||||
ok &= r.status == state::Status::Done;
|
||||
}
|
||||
Ok(code(ok))
|
||||
});
|
||||
Ok(code(ok.load(Ordering::SeqCst)))
|
||||
}
|
||||
Cmd::Status { id: Some(id) } => {
|
||||
println!("{}", serde_json::to_string_pretty(&state::Run::load(&id)?)?);
|
||||
|
||||
Reference in new issue
Block a user