Skip to content

Commit 3362205

Browse files
committed
Create Container::new and move container_removed_future there.
1 parent 7632f3a commit 3362205

File tree

2 files changed

+31
-38
lines changed

2 files changed

+31
-38
lines changed

src/docker/container.rs

+28-3
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,44 @@ use super::{IoStream, IoStreamSource};
55
use anyhow::{anyhow, Context, Error, Result};
66
use bollard::service::EventMessage;
77
use futures::future::{BoxFuture, Shared};
8+
use futures::FutureExt;
89
use tokio::io::AsyncWriteExt;
910
use tokio::signal::unix::{signal, SignalKind};
1011
use tokio::task::{spawn, JoinHandle};
1112
use tokio_stream::StreamExt;
1213

1314
#[derive(Clone)]
1415
pub struct Container {
15-
pub(super) id: String,
16-
pub(super) docker: bollard::Docker,
17-
pub(super) remove_event: Shared<BoxFuture<'static, Option<EventMessage>>>,
16+
id: String,
17+
docker: bollard::Docker,
18+
remove_event: Shared<BoxFuture<'static, Option<EventMessage>>>,
1819
}
1920

2021
impl Container {
22+
pub(super) fn new(id: &str, docker: &bollard::Docker) -> Result<Self> {
23+
let mut remove_events = docker.events(Some(bollard::system::EventsOptions {
24+
filters: [
25+
("container".to_owned(), vec![id.to_owned()]),
26+
("type".to_owned(), vec!["container".to_owned()]),
27+
("event".to_owned(), vec!["destroy".to_owned()]),
28+
]
29+
.into(),
30+
..Default::default()
31+
}));
32+
33+
// Spawn the future to start listening event.
34+
let remove_evevnt = tokio::spawn(async move { remove_events.next().await?.ok() })
35+
.map(|x| x.ok().flatten())
36+
.boxed()
37+
.shared();
38+
39+
Ok(Self {
40+
id: id.to_owned(),
41+
docker: docker.clone(),
42+
remove_event: remove_evevnt,
43+
})
44+
}
45+
2146
pub fn id(&self) -> &str {
2247
&self.id
2348
}

src/docker/docker.rs

+3-35
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
use super::Container;
22

33
use anyhow::{ensure, Context, Result};
4-
use bollard::service::EventMessage;
5-
use futures::{
6-
future::{BoxFuture, Shared},
7-
FutureExt, StreamExt,
8-
};
94

105
pub struct Docker(bollard::Docker);
116

@@ -14,14 +9,10 @@ impl Docker {
149
Ok(Docker(bollard::Docker::connect_with_local_defaults()?))
1510
}
1611

17-
pub async fn get_container<T: AsRef<str>>(&self, name: T) -> Result<Container> {
12+
pub async fn get<T: AsRef<str>>(&self, name: T) -> Result<Container> {
1813
let response = self.0.inspect_container(name.as_ref(), None).await?;
1914
let id = response.id.context("Failed to obtain container ID")?;
20-
Ok(Container {
21-
id: id.clone(),
22-
docker: self.0.clone(),
23-
remove_event: container_removed_future(&self.0, id.clone()),
24-
})
15+
Container::new(&id, &self.0)
2516
}
2617

2718
pub async fn run<U: AsRef<str>, T: AsRef<[U]>>(&self, args: T) -> Result<Container> {
@@ -44,29 +35,6 @@ impl Docker {
4435
);
4536

4637
let id = String::from_utf8(output.stdout)?;
47-
self.get_container(id.trim()).await
38+
self.get(id.trim()).await
4839
}
4940
}
50-
51-
fn container_removed_future(
52-
docker: &bollard::Docker,
53-
id: String,
54-
) -> Shared<BoxFuture<'static, Option<EventMessage>>> {
55-
let options = bollard::system::EventsOptions {
56-
filters: [
57-
(String::from("container"), vec![id.clone()]),
58-
(String::from("type"), vec![String::from("container")]),
59-
(String::from("event"), vec![String::from("destroy")]),
60-
]
61-
.into(),
62-
..Default::default()
63-
};
64-
65-
let mut events = docker.events(Some(options));
66-
67-
// Spawn the future to start listening event.
68-
tokio::spawn(async move { events.next().await?.ok() })
69-
.map(|x| x.ok().flatten())
70-
.boxed()
71-
.shared()
72-
}

0 commit comments

Comments
 (0)