56 lines
1.8 KiB
Rust
56 lines
1.8 KiB
Rust
use bollard::{Docker, query_parameters::{ListContainersOptionsBuilder, StopContainerOptionsBuilder}};
|
|
|
|
use crate::{ DbPool, dockermgr, models, schema, status::ServerStatus };
|
|
|
|
use async_std::task;
|
|
|
|
use std::{collections::HashMap, time::Duration};
|
|
|
|
use diesel::prelude::*;
|
|
|
|
|
|
async fn server_init(pool: &DbPool, docker: &Docker) -> Result<(), String> {
|
|
use schema::servers::dsl::*;
|
|
let conn = &mut pool.get().unwrap();
|
|
|
|
println!("init start");
|
|
|
|
let _ = diesel::update(servers).filter(simple_redirect.eq(false)).set(status.eq(ServerStatus::Stopped)).execute(conn);
|
|
|
|
let mut filters = HashMap::new();
|
|
filters.insert("name", vec!["minecraft-"]);
|
|
let container_list_options = ListContainersOptionsBuilder::new()
|
|
.all(true)
|
|
.filters(&filters)
|
|
.build();
|
|
let containers = docker.list_containers(Some(container_list_options)).await.map_err(|e| format!("Failed get current containers : {}", e))?;
|
|
|
|
if containers.len() != 0 {
|
|
println!("stopping {} stray container(s)", containers.len())
|
|
}
|
|
|
|
for container in containers {
|
|
let container_name = &container.names.unwrap()[0][1..];
|
|
println!("stopping {}", container_name);
|
|
let stop_options = StopContainerOptionsBuilder::new()
|
|
.t(60)
|
|
.build();
|
|
docker.stop_container(container_name, Some(stop_options)).await.map_err(|e| format!("Failed to stop container {}, : {}", container_name, e))?;
|
|
println!("{} stopped", container_name);
|
|
}
|
|
println!("init done");
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn server_manager_loop(pool: DbPool, docker: Docker)
|
|
{
|
|
match server_init(&pool, &docker).await {
|
|
Err(e) => { eprintln!("init failed , ignoring because idgaf : {}", e); },
|
|
Ok(_) => {},
|
|
};
|
|
|
|
loop {
|
|
task::sleep(Duration::from_mins(5)).await;
|
|
}
|
|
}
|