Skip to content

Instantly share code, notes, and snippets.

@boxdot
Created April 2, 2019 14:10
Show Gist options
  • Save boxdot/6af7b75f00044ba7c023d3f5d00673b0 to your computer and use it in GitHub Desktop.
Save boxdot/6af7b75f00044ba7c023d3f5d00673b0 to your computer and use it in GitHub Desktop.
use actix::prelude::*;
use actix_web::{middleware, web, App, HttpResponse, HttpServer};
use log::info;
use std::io;
use std::thread;
use std::time::Duration;
fn my_handler() -> HttpResponse {
"Hello".into()
}
fn list_threads() -> Vec<String> {
let pid = std::process::id();
let tasks = std::fs::read_dir(format!("/proc/{}/task/", pid)).unwrap();
tasks
.filter_map(|entry| {
if let Ok(entry) = entry {
let path = entry.path();
let pid = path.file_name().unwrap();
if let Ok(name) = std::fs::read_to_string(path.join("comm")) {
Some(format!("{}:{}", pid.to_string_lossy(), name.trim()))
} else {
None
}
} else {
None
}
})
.collect()
}
fn main() -> Result<(), io::Error> {
std::env::set_var("RUST_LOG", "info");
env_logger::init();
info!(
"sleeping 1 sec on system thread; threads: \n{}",
list_threads().join("\n")
);
thread::sleep(Duration::from_secs(1));
let sys = System::new("example");
info!(
"sleeping 1 sec on system thread; threads: \n{}",
list_threads().join("\n")
);
thread::sleep(Duration::from_secs(1));
info!("creating http; threads: \n{}", list_threads().join("\n"));
HttpServer::new(move || {
App::new()
.wrap(middleware::Logger::default())
.service(web::resource("/").to(my_handler))
})
.workers(1)
.disable_signals()
.bind("127.0.0.1:8080")
.unwrap()
.start();
info!(
"sleeping 1 sec on system thread; threads: \n{}",
list_threads().join("\n")
);
thread::sleep(Duration::from_secs(1));
info!("creating stopper; threads: \n{}", list_threads().join("\n"));
let _ = thread::Builder::new().name("stopper".to_string()).spawn({
let sys = System::current();
move || {
for i in (1..=5).rev() {
info!(
"will stop server and system in {}; threads: \n{}",
i,
list_threads().join("\n")
);
thread::sleep(Duration::from_secs(1));
}
info!(
"sleeping 1 sec on stopper thread; threads: \n{}",
list_threads().join("\n")
);
thread::sleep(Duration::from_secs(1));
info!(
"stopping system {}; threads: \n{}",
sys.id(),
list_threads().join("\n")
);
sys.stop();
}
});
sys.run().unwrap();
info!("system stopped; threads: \n{}", list_threads().join("\n"));
for i in (1..=5).rev() {
info!(
"will stop in {}; threads: \n{}",
i,
list_threads().join("\n")
);
thread::sleep(Duration::from_secs(1));
}
Ok(())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment