Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ members = [
]

[workspace.package]
version = "0.2.90"
version = "0.2.91"
edition = "2024"
license = "MIT"
repository = "https://github.com/profullstack/c0mpute"
Expand Down
2 changes: 1 addition & 1 deletion apps/tui/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@c0mpute/tui",
"version": "0.2.90",
"version": "0.2.91",
"private": true,
"type": "module",
"bin": {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "c0mpute",
"version": "0.2.90",
"version": "0.2.91",
"private": true,
"type": "module",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/app/releases/latest.json/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export const dynamic = "force-dynamic";
* this in sync with the workspace version in `Cargo.toml` on each release so a
* freshly built CLI reports "already latest" rather than a phantom upgrade.
*/
const FALLBACK_VERSION = "0.2.90";
const FALLBACK_VERSION = "0.2.91";
const FALLBACK_MIN_REQUIRED = "0.0.1";

type Channel = "stable" | "beta" | "nightly";
Expand Down
67 changes: 67 additions & 0 deletions node/crates/c0mpute-core/src/job_progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,51 @@ impl ProgressBoard {
}
}

/// A finished tracked request, kept so a caller that lost its connection (an
/// app restarting mid-song) can still collect it: `GET /v1/result/{id}`.
#[derive(Clone, Debug)]
pub enum Stored {
Done {
bytes: Arc<Vec<u8>>,
content_type: &'static str,
job_id: String,
worker: String,
},
Failed {
status: u16,
message: String,
},
}

/// How many finished results are held at once (songs are ~2-5 MB each).
const MAX_RESULTS: usize = 48;

#[derive(Clone, Default)]
pub struct ResultStore(Arc<Mutex<HashMap<String, (u64, Stored)>>>);

impl ResultStore {
pub fn put(&self, key: &str, stored: Stored) {
let now = now_ms();
let Ok(mut map) = self.0.lock() else { return };
map.retain(|_, (at, _)| now.saturating_sub(*at) < KEEP_MS);
while map.len() >= MAX_RESULTS {
let Some(oldest) = map.iter().min_by_key(|(_, (at, _))| *at).map(|(k, _)| k.clone()) else {
break;
};
map.remove(&oldest);
}
map.insert(key.to_string(), (now, stored));
}

pub fn get(&self, key: &str) -> Option<Stored> {
let now = now_ms();
let map = self.0.lock().ok()?;
map.get(key)
.filter(|(at, _)| now.saturating_sub(*at) < KEEP_MS)
.map(|(_, s)| s.clone())
}
}

/// A caller-chosen request id: short and URL-safe.
pub fn valid_key(key: &str) -> bool {
!key.is_empty()
Expand Down Expand Up @@ -145,6 +190,28 @@ mod tests {
assert!(board.snapshot("nope").is_none());
}

#[test]
fn finished_results_are_kept_for_a_caller_that_reconnects() {
let store = ResultStore::default();
assert!(store.get("song-1").is_none());
store.put(
"song-1",
Stored::Done {
bytes: Arc::new(vec![1, 2, 3]),
content_type: "audio/mpeg",
job_id: "j".into(),
worker: "w".into(),
},
);
store.put("song-2", Stored::Failed { status: 503, message: "no worker".into() });
assert!(matches!(store.get("song-1"), Some(Stored::Done { ref bytes, .. }) if bytes.as_slice() == [1, 2, 3]));
assert!(matches!(store.get("song-2"), Some(Stored::Failed { status: 503, .. })));
for i in 0..(MAX_RESULTS + 5) {
store.put(&format!("k{i}"), Stored::Failed { status: 502, message: String::new() });
}
assert!(store.0.lock().unwrap().len() <= MAX_RESULTS, "bounded");
}

#[test]
fn request_ids_must_be_short_and_url_safe() {
assert!(valid_key("17a1b21d-a95e-4345-a3fe-d14dfd6868e3"));
Expand Down
Loading
Loading