-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: stop and delete containers in tokio task
- Loading branch information
1 parent
7c7ebeb
commit 9a8738e
Showing
6 changed files
with
84 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,51 @@ | ||
use crate::{docker::container::DockerContainer, traits::Callback}; | ||
use crate::{ | ||
docker::container::DockerContainer, | ||
events::{Key, Message, Transition}, | ||
traits::Callback, | ||
}; | ||
use async_trait::async_trait; | ||
use color_eyre::eyre::Result; | ||
use tokio::sync::mpsc::Sender; | ||
|
||
#[derive(Debug)] | ||
pub struct DeleteContainer { | ||
docker: bollard::Docker, | ||
container: DockerContainer, | ||
force: bool, | ||
tx: Sender<Message<Key, Transition>>, | ||
} | ||
|
||
impl DeleteContainer { | ||
pub fn new(docker: bollard::Docker, container: DockerContainer, force: bool) -> Self { | ||
pub fn new( | ||
docker: bollard::Docker, | ||
container: DockerContainer, | ||
force: bool, | ||
tx: Sender<Message<Key, Transition>>, | ||
) -> Self { | ||
Self { | ||
docker, | ||
container, | ||
force, | ||
tx, | ||
} | ||
} | ||
} | ||
#[async_trait] | ||
impl Callback for DeleteContainer { | ||
async fn call(&self) -> Result<()> { | ||
let _ = self.container.delete(&self.docker, self.force).await?; | ||
let container = self.container.clone(); | ||
let docker = self.docker.clone(); | ||
let force = self.force; | ||
let tx = self.tx.clone(); | ||
tokio::spawn(async move { | ||
let message = if container.delete(&docker, force).await.is_ok() { | ||
Message::Tick | ||
} else { | ||
let msg = format!("Failed to delete container {}", container.id); | ||
Message::Error(msg) | ||
}; | ||
let _ = tx.send(message).await; | ||
}); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters