keep already started containers, multi threaded async
This commit is contained in:
@ -22,5 +22,5 @@ num-traits = "0.2.19"
|
||||
regex = "1.12.3"
|
||||
serde = "1.0.228"
|
||||
thiserror = "2.0.18"
|
||||
tokio = { version = "1.52.1", features = ["net", "rt", "macros", "signal", "fs"] }
|
||||
tokio = { version = "1.52.1", features = ["net", "rt", "macros", "signal", "fs", "rt-multi-thread"] }
|
||||
zip = "8.6.0"
|
||||
|
||||
@ -6,7 +6,7 @@
|
||||
/* By: tomoron <tomoron@student.42angouleme.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2026/05/29 21:22:17 by tomoron #+# #+# */
|
||||
/* Updated: 2026/06/11 23:41:49 by tomoron ### ########.fr */
|
||||
/* Updated: 2026/06/12 12:02:28 by tomoron ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
@ -65,7 +65,7 @@ fn cli() -> Command {
|
||||
.subcommand(stop::create_subcommand())
|
||||
}
|
||||
|
||||
#[tokio::main(flavor = "current_thread")]
|
||||
#[tokio::main(flavor = "multi_thread")]
|
||||
async fn main() -> Result<(), String> {
|
||||
let arg = cli().get_matches();
|
||||
let pool = get_connection_pool();
|
||||
|
||||
@ -8,10 +8,6 @@ pub const MIGRATIONS: EmbeddedMigrations = embed_migrations!("./migrations");
|
||||
|
||||
pub fn run_migrations(pool: &DbPool) -> Result<(), Box<dyn Error + Send + Sync + 'static>> {
|
||||
|
||||
// This will run the necessary migrations.
|
||||
//
|
||||
// See the documentation for `MigrationHarness` for
|
||||
// all available methods.
|
||||
let connection = &mut pool.get().unwrap();
|
||||
connection.run_pending_migrations(MIGRATIONS)?;
|
||||
|
||||
|
||||
@ -1,45 +1,27 @@
|
||||
use bollard::{Docker, query_parameters::{ListContainersOptionsBuilder, StopContainerOptionsBuilder}};
|
||||
use bollard::Docker;
|
||||
use chrono::{TimeDelta, Utc};
|
||||
|
||||
use crate::{ DbPool, dockermgr, models::{self, Servers}, schema, status::ServerStatus };
|
||||
|
||||
use async_std::task;
|
||||
|
||||
use std::{collections::HashMap, time::Duration};
|
||||
use std::time::Duration;
|
||||
|
||||
use diesel::prelude::*;
|
||||
|
||||
|
||||
async fn server_init(pool: &DbPool, docker: &Docker) -> Result<(), String> {
|
||||
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");
|
||||
let _ = diesel::update(servers).filter(
|
||||
simple_redirect.eq(false)
|
||||
.and(status.eq(ServerStatus::Stopping)
|
||||
.or(status.eq(ServerStatus::Starting))
|
||||
))
|
||||
.set(status.eq(ServerStatus::Stopped)).execute(conn);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user