Commit 2f1cb46c authored by Stavros Korokithakis's avatar Stavros Korokithakis

Improve detection of IPFS daemon startup

parent 2dfba491
......@@ -9,17 +9,19 @@ extern crate xdg;
mod views;
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
use views::start_api;
use std::fs;
use std::io;
use std::sync::Mutex;
use std::collections::HashMap;
use std::env;
use std::time::Duration;
use std::sync::mpsc::channel;
use std::thread;
use std::ffi::OsStr;
use std::fs;
use std::io;
use std::net::{IpAddr, Ipv4Addr, SocketAddr, TcpStream};
use std::process;
use std::process::Command;
use std::sync::mpsc::channel;
use std::sync::Mutex;
use std::thread;
use std::time::{Duration, SystemTime};
use views::start_api;
lazy_static! {
static ref DEBOUNCING_DONE: Mutex<bool> = Mutex::new(true);
......@@ -51,8 +53,28 @@ lazy_static! {
};
}
lazy_static! {
static ref IPFS_API_HOST: SocketAddr = SocketAddr::new(
IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 5001
);
}
static IPFS_EXECUTABLE: &str = "ipfs";
fn kill_ipfs_and_exit() {
// Terminate Hearth after one second.
thread::spawn(|| {
thread::sleep(Duration::from_secs(1));
process::exit(1);
});
let ipfs_pid = { IPFS_PID.lock().unwrap() };
process::Command::new("kill")
.args(&[OsStr::new(&*ipfs_pid.to_string())])
.output()
.expect("Could not kill IPFS daemon.");
}
fn add_files_to_ipfs() {
println!("Adding files to IPFS...");
let output = Command::new(IPFS_EXECUTABLE)
......@@ -131,9 +153,29 @@ fn start_ipfs() {
// Add everything, because files might have been changed while we weren't looking.
thread::spawn(|| {
// Wait for the daemon to start up...
thread::sleep(Duration::from_millis(5000));
notify_ipfs(true);
let timeout = 60;
println!("Waiting {} seconds for daemon to start up...", timeout);
let sys_time = SystemTime::now();
while sys_time
.duration_since(sys_time)
.expect("SystemTime::duration_since failed")
.as_secs() < timeout
{
let conn = TcpStream::connect_timeout(&IPFS_API_HOST, Duration::from_secs(1));
if conn.is_ok() {
println!("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!(
"IPFS daemon failed to become responsive in {} seconds, exiting...",
timeout
);
kill_ipfs_and_exit();
});
let mut child = Command::new(IPFS_EXECUTABLE)
......
use rouille;
use rouille::Response;
use std::process;
use std::ffi::OsStr;
use std::{thread, time};
use IPFS_HASHES;
use IPFS_PID;
use kill_ipfs_and_exit;
#[derive(RustcEncodable)]
struct ExitResponse {
......@@ -25,18 +22,7 @@ pub fn start_api() {
rouille::start_server(interface, move |request| {
router!(request,
(POST) (/api/exit/) => {
// Terminate the server after one second.
thread::spawn(|| {
thread::sleep(time::Duration::from_secs(1));
process::exit(1);
});
let ipfs_pid = {
IPFS_PID.lock().unwrap()
};
process::Command::new("kill")
.args(&[OsStr::new(&*ipfs_pid.to_string())])
.output()
.expect("Could not kill IPFS daemon.");
kill_ipfs_and_exit();
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