mirror of
https://github.com/mii443/rs-docker-bot.git
synced 2025-08-22 16:15:40 +00:00
take contaienr from pool
This commit is contained in:
34
src/container_pool.rs
Normal file
34
src/container_pool.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use std::sync::Arc;
|
||||
|
||||
use tokio::sync::Mutex;
|
||||
|
||||
use crate::{docker::Container, language::Language};
|
||||
|
||||
pub struct ContainerPool {
|
||||
pub containers: Arc<Mutex<Vec<Container>>>,
|
||||
}
|
||||
|
||||
impl ContainerPool {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
containers: Arc::new(Mutex::new(vec![])),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_container(&mut self, language: Language) -> Container {
|
||||
let mut pool = self.containers.lock().await;
|
||||
if let Some(i) = pool.iter().position(|container| {
|
||||
if let Some(l) = &container.language {
|
||||
l.image == language.image
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}) {
|
||||
let container = pool[i].clone();
|
||||
pool.remove(i);
|
||||
container
|
||||
} else {
|
||||
Container::from_language(language).await
|
||||
}
|
||||
}
|
||||
}
|
@ -34,6 +34,7 @@ pub async fn docker_ps() -> Vec<ContainerSummary> {
|
||||
list.unwrap()
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub struct Container {
|
||||
pub id: String,
|
||||
pub name: String,
|
||||
|
@ -1,7 +1,7 @@
|
||||
use bollard::{container::Config, service::HostConfig};
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Serialize, Deserialize, Clone, Debug)]
|
||||
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq)]
|
||||
pub struct Language {
|
||||
pub name: String,
|
||||
pub code: Vec<String>,
|
||||
|
Reference in New Issue
Block a user