fix(containers): remove a container once it exits, and every exited one at start
Jason Schwarzenberger authored 1 parent fc99e25commit 7edf3d00c5e8d366ad0e934099034d05adc6816bBrowse files
1 file changed+65−32
modifiedcrates/celld/container.rs+65−32View file
| @@ -840,33 +840,42 @@ | ||
| 840 | 840 | } |
| 841 | 841 | |
| 842 | 842 | async fn reap(&self) -> anyhow::Result<()> { |
| 843 | - let filters = json!({ "label": [format!("celld.node={}", self.node)] }).to_string(); | |
| 844 | - let reply = self | |
| 845 | - .docker | |
| 846 | - .expect( | |
| 847 | - "GET", | |
| 848 | - &format!( | |
| 849 | - "/containers/json?all=true&filters={}", | |
| 850 | - percent_encoding::utf8_percent_encode( | |
| 851 | - &filters, | |
| 852 | - percent_encoding::NON_ALPHANUMERIC | |
| 853 | - ) | |
| 854 | - ), | |
| 855 | - None, | |
| 856 | - "list containers", | |
| 857 | - ) | |
| 858 | - .await?; | |
| 859 | - let list = reply.json()?; | |
| 860 | - for entry in list.as_array().into_iter().flatten() { | |
| 861 | - if let Some(id) = entry.get("Id").and_then(Value::as_str) { | |
| 862 | - let _ = self | |
| 863 | - .docker | |
| 864 | - .call( | |
| 865 | - "DELETE", | |
| 866 | - &format!("/containers/{id}?force=true&v=true"), | |
| 867 | - None, | |
| 868 | - ) | |
| 869 | - .await; | |
| 843 | + // homeflare: also every celld container that has exited, whichever | |
| 844 | + // node left it. A node gets a new id each start, so the containers of | |
| 845 | + // one that stopped were otherwise never reaped; a running one belongs | |
| 846 | + // to a live node, and an exited one is never started again. | |
| 847 | + let filters = [ | |
| 848 | + json!({ "label": [format!("celld.node={}", self.node)] }), | |
| 849 | + json!({ "label": ["celld.node"], "status": ["exited", "dead"] }), | |
| 850 | + ]; | |
| 851 | + for filters in filters { | |
| 852 | + let reply = self | |
| 853 | + .docker | |
| 854 | + .expect( | |
| 855 | + "GET", | |
| 856 | + &format!( | |
| 857 | + "/containers/json?all=true&filters={}", | |
| 858 | + percent_encoding::utf8_percent_encode( | |
| 859 | + &filters.to_string(), | |
| 860 | + percent_encoding::NON_ALPHANUMERIC | |
| 861 | + ) | |
| 862 | + ), | |
| 863 | + None, | |
| 864 | + "list containers", | |
| 865 | + ) | |
| 866 | + .await?; | |
| 867 | + let list = reply.json()?; | |
| 868 | + for entry in list.as_array().into_iter().flatten() { | |
| 869 | + if let Some(id) = entry.get("Id").and_then(Value::as_str) { | |
| 870 | + let _ = self | |
| 871 | + .docker | |
| 872 | + .call( | |
| 873 | + "DELETE", | |
| 874 | + &format!("/containers/{id}?force=true&v=true"), | |
| 875 | + None, | |
| 876 | + ) | |
| 877 | + .await; | |
| 878 | + } | |
| 870 | 879 | } |
| 871 | 880 | } |
| 872 | 881 | Ok(()) |
| @@ -1417,6 +1426,15 @@ | ||
| 1417 | 1426 | let docker = self.docker.clone(); |
| 1418 | 1427 | let cell_ = cell.clone(); |
| 1419 | 1428 | asyncrt::spawn(async move { |
| 1429 | + // homeflare: the container's id, so it is removed once it exits | |
| 1430 | + // (below) and a later run's container, under the same name, is not. | |
| 1431 | + let id = docker | |
| 1432 | + .call("GET", &format!("/containers/{}/json", cell_.name), None) | |
| 1433 | + .await | |
| 1434 | + .ok() | |
| 1435 | + .filter(|reply| reply.status.is_success()) | |
| 1436 | + .and_then(|reply| reply.json().ok()) | |
| 1437 | + .and_then(|json| json.get("Id").and_then(Value::as_str).map(str::to_string)); | |
| 1420 | 1438 | let result = docker |
| 1421 | 1439 | .call("POST", &format!("/containers/{}/wait", cell_.name), None) |
| 1422 | 1440 | .await |
| @@ -1435,13 +1453,28 @@ | ||
| 1435 | 1453 | .ok_or_else(|| anyhow!("wait answered without a status code")) |
| 1436 | 1454 | }) |
| 1437 | 1455 | .map_err(|error| format!("{error:#}")); |
| 1438 | - let mut state = cell_.state.lock().unwrap(); | |
| 1439 | - if state.run == run { | |
| 1440 | - state.running = false; | |
| 1441 | - state.address = Address::None; | |
| 1456 | + { | |
| 1457 | + let mut state = cell_.state.lock().unwrap(); | |
| 1458 | + if state.run == run { | |
| 1459 | + state.running = false; | |
| 1460 | + state.address = Address::None; | |
| 1461 | + } | |
| 1442 | 1462 | } |
| 1443 | - drop(state); | |
| 1444 | 1463 | cell_.exit.send_replace(Some((run, result))); |
| 1464 | + // homeflare: an exited container is removed, with its volumes (a | |
| 1465 | + // CI runner's Docker storage), as a Cloudflare Container is gone | |
| 1466 | + // once it stops; the next start creates a fresh one anyway. Kept, | |
| 1467 | + // a container whose node restarted (a new node id) was never | |
| 1468 | + // reaped, and every finished CI job left gigabytes behind. | |
| 1469 | + if let Some(id) = id { | |
| 1470 | + let _ = docker | |
| 1471 | + .call( | |
| 1472 | + "DELETE", | |
| 1473 | + &format!("/containers/{id}?force=true&v=true"), | |
| 1474 | + None, | |
| 1475 | + ) | |
| 1476 | + .await; | |
| 1477 | + } | |
| 1445 | 1478 | }) |
| 1446 | 1479 | .detach(); |
| 1447 | 1480 | } |