Commit d0b9cbd9 authored by Stavros Korokithakis's avatar Stavros Korokithakis

Streamline logging

parent 2f1cb46c
......@@ -4,8 +4,10 @@ version = "0.1.0"
authors = ["Stavros Korokithakis <hi@stavros.io>"]
[dependencies]
notify = "4.0.0"
xdg = "^2.1"
lazy_static = "0.2"
log = "0.4.1"
notify = "4.0.0"
rouille = "^1.0"
rustc-serialize = "0.3.24"
simple_logger = "0.5.0"
xdg = "^2.1"
#[macro_use]
extern crate lazy_static;
#[macro_use]
extern crate log;
extern crate notify;
#[macro_use]
extern crate rouille;
extern crate rustc_serialize;
extern crate simple_logger;
extern crate xdg;
mod views;
......@@ -60,12 +63,13 @@ lazy_static! {
}
static IPFS_EXECUTABLE: &str = "ipfs";
static HTTP_INTERFACE: &str = "127.0.0.1:26484";
fn kill_ipfs_and_exit() {
// Terminate Hearth after one second.
thread::spawn(|| {
fn kill_ipfs_and_exit(exit_code: i32) {
// Send a SIGTERM to the IPFS daemon and terminate Hearth one second later.
thread::spawn(move || {
thread::sleep(Duration::from_secs(1));
process::exit(1);
process::exit(exit_code);
});
let ipfs_pid = { IPFS_PID.lock().unwrap() };
......@@ -76,7 +80,7 @@ fn kill_ipfs_and_exit() {
}
fn add_files_to_ipfs() {
println!("Adding files to IPFS...");
info!("Adding files to IPFS...");
let output = Command::new(IPFS_EXECUTABLE)
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["add", "-r", HEARTH_DIR.to_str().unwrap()])
......@@ -109,11 +113,11 @@ fn notify_ipfs(add_to_ipfs: bool) {
if add_to_ipfs {
add_files_to_ipfs();
println!("Finished adding files to IPFS.\n");
info!("Finished adding files to IPFS.\n");
*debouncing_done = true;
} else if *debouncing_done == true {
println!("Change detected in filesystem.");
info!("Change detected in filesystem.");
*debouncing_done = false;
}
}
......@@ -135,26 +139,27 @@ fn files_changed(event: notify::DebouncedEvent) {
}
fn start_ipfs() {
println!("Initializing IPFS...");
info!("Initializing IPFS directory...");
Command::new(IPFS_EXECUTABLE)
.env("IPFS_PATH", &*IPFS_PATH)
.args(&["init"])
.output()
.unwrap_or_else(|_| {
println!("Could not initialize IPFS.");
warn!("Could not initialize IPFS.");
process::exit(1)
});
println!("Starting IPFS daemon...");
info!("Starting IPFS daemon...");
// Print the correct message to the console because we'll add some files.
notify_ipfs(false);
// Add everything, because files might have been changed while we weren't looking.
// This is done in a new thread, as the command to run the IPFS daemon farther down will block.
thread::spawn(|| {
let timeout = 60;
println!("Waiting {} seconds for daemon to start up...", timeout);
info!("Waiting {} seconds for daemon to start up...", timeout);
let sys_time = SystemTime::now();
while sys_time
......@@ -164,18 +169,18 @@ fn start_ipfs() {
{
let conn = TcpStream::connect_timeout(&IPFS_API_HOST, Duration::from_secs(1));
if conn.is_ok() {
println!("Daemon is up, continuing...");
info!("Daemon is up, continuing...");
thread::sleep(Duration::from_secs(1));
notify_ipfs(true);
return;
}
}
// Daemon didn't start up in a minute, kill it and exit.
println!(
warn!(
"IPFS daemon failed to become responsive in {} seconds, exiting...",
timeout
);
kill_ipfs_and_exit();
kill_ipfs_and_exit(1);
});
let mut child = Command::new(IPFS_EXECUTABLE)
......@@ -183,7 +188,7 @@ fn start_ipfs() {
.args(&["daemon", "--writable", "--enable-gc", "--migrate"])
.spawn()
.unwrap_or_else(|_| {
println!("Could not launch IPFS daemon.");
warn!("Could not launch IPFS daemon.");
process::exit(1)
});
......@@ -194,7 +199,7 @@ fn start_ipfs() {
}
child.wait().expect("Failed to wait on IPFS daemon.");
println!("IPFS daemon died, exiting...");
warn!("IPFS daemon died, exiting...");
process::exit(1)
}
......@@ -208,20 +213,23 @@ fn create_debounced_watcher() {
loop {
match rx.recv() {
Ok(e) => files_changed(e),
Err(e) => println!("watch error: {:?}", e),
Err(e) => warn!("watch error: {:?}", e),
}
}
}
fn main() {
println!("Starting hearth...");
simple_logger::init().unwrap();
info!("Starting hearth...");
thread::spawn(start_ipfs);
info!("Starting API server on {}...", HTTP_INTERFACE);
thread::spawn(start_api);
match fs::create_dir(&*HEARTH_DIR) {
Err(ref e) if e.kind() != io::ErrorKind::AlreadyExists => {
println!("Could not create the Hearth directory, for some reason.");
warn!("Could not create the Hearth directory, for some reason.");
process::exit(1)
}
_ => {}
......
use rouille;
use rouille::Response;
use HTTP_INTERFACE;
use IPFS_HASHES;
use kill_ipfs_and_exit;
......@@ -17,12 +18,10 @@ struct HashResponse {
}
pub fn start_api() {
let interface = "127.0.0.1:26484";
println!("Starting API server on {}...", interface);
rouille::start_server(interface, move |request| {
rouille::start_server(HTTP_INTERFACE, move |request| {
router!(request,
(POST) (/api/exit/) => {
kill_ipfs_and_exit();
kill_ipfs_and_exit(0);
Response::json(&ExitResponse{result: "ok".to_owned()})
},
(GET) (/api/path/) => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment