Remove notify dependencies

This commit is contained in:
2018-10-11 17:26:40 +02:00
parent 07509cbf75
commit 3c79c4c113
4 changed files with 8 additions and 432 deletions

View File

@@ -3,21 +3,15 @@ extern crate pretty_env_logger;
#[macro_use]
extern crate log;
extern crate indicatif;
extern crate notify;
extern crate tempfile;
extern crate walkdir;
use git2::{Cred, Error, FetchOptions, Index, IndexEntry, IndexTime, Progress, PushOptions, RemoteCallbacks, Repository, Signature};
use std::fs::{File, create_dir_all, remove_file};
use std::path::{Path, PathBuf};
use std::string::String;
use std::str;
use std::sync::{Arc, Mutex};
use std::sync::mpsc::channel;
use std::thread::{spawn, JoinHandle};
use indicatif::{ProgressBar, ProgressStyle};
use notify::{raw_watcher, RecommendedWatcher, RawEvent, Op, RecursiveMode, Watcher};
use tempfile::TempDir;
use walkdir::WalkDir;
pub struct BenchmarkRepositoryInner
@@ -30,11 +24,7 @@ pub struct BenchmarkRepositoryInner
pub struct BenchmarkRepository
{
inner: Arc<Mutex<BenchmarkRepositoryInner>>,
autocommit_directory: Option<TempDir>,
autocommit_thread: Option<JoinHandle<()>>,
autocommit_watcher: Option<RecommendedWatcher>,
autocommit_locks: Arc<Mutex<usize>>,
inner: Arc<Mutex<BenchmarkRepositoryInner>>
}
pub struct TargetPath
@@ -168,10 +158,6 @@ impl BenchmarkRepository
BenchmarkRepository
{
inner: Arc::new(Mutex::new(benchmark_repository_inner)),
autocommit_directory: None,
autocommit_thread: None,
autocommit_watcher: None,
autocommit_locks: Arc::new(Mutex::new(0)),
};
benchmark_repository
@@ -393,168 +379,6 @@ impl BenchmarkRepository
Some(content.to_owned())
}
fn prepare_autocommit_directory(&mut self)
{
match self.autocommit_thread
{
Some(_) => (),
None =>
{
let tmp_dir = match TempDir::new()
{
Ok(value) => value,
Err(error) => panic!("Could not create autocommit directory: {}", error),
};
trace!("Created temporary autocommit directory in {}", tmp_dir.path().display());
let lock_file_path = tmp_dir.path().join(".lock");
if let Err(error) = File::create(&lock_file_path)
{
panic!("Could not create lock file “{}”: {}", lock_file_path.display(), error);
}
let (sender, receiver) = channel();
let mut watcher = match raw_watcher(sender)
{
Ok(value) => value,
Err(error) => panic!("Could not create filesystem watcher: {}", error),
};
if let Err(error) = watcher.watch(&tmp_dir, RecursiveMode::Recursive)
{
panic!("Could not initiate filesystem watcher: {}", error);
}
self.autocommit_watcher = Some(watcher);
self.autocommit_directory = Some(tmp_dir);
let autocommit_base_path = self.autocommit_directory.as_ref().unwrap().path().to_owned();
let inner = self.inner.clone();
let autocommit_locks = self.autocommit_locks.clone();
let thread = spawn(
move ||
{
trace!("Autocommit thread running");
let mut active = true;
loop
{
match receiver.recv()
{
Ok(RawEvent{path: Some(path), op: Ok(Op::REMOVE), cookie: _}) =>
{
if path == lock_file_path
{
trace!("Waiting for remaining autocommit locks to be released");
active = false;
}
else
{
trace!("Received “remove” event for path “{}”", path.display());
let stripped_path = match path.strip_prefix(&autocommit_base_path)
{
Ok(value) => value,
Err(_) => panic!("Cannot handle “remove” event for path “{}", path.display()),
};
let mut components = stripped_path.components();
let branch_name = components.next().unwrap();
let result_path = stripped_path.strip_prefix(&branch_name).unwrap().parent().unwrap();
info!("Autocommiting “{}”", result_path.display());
let inner = inner.lock().unwrap();
BenchmarkRepository::commit_directory(&inner, &TargetPath{source: path.parent().unwrap().to_owned(), destination: result_path.to_owned()}, &branch_name.as_os_str().to_str().unwrap());
let mut autocommit_locks = autocommit_locks.lock().unwrap();
*autocommit_locks -= 1;
}
},
Err(error) => panic!("Error handling notify event: {}", error),
_ => (),
}
let autocommit_locks = autocommit_locks.lock().unwrap();
if !active && *autocommit_locks == 0
{
break;
}
}
trace!("Autocommit thread finished");
});
self.autocommit_thread = Some(thread);
}
}
}
pub fn create_autocommit_directory(&mut self, directory_path: &Path, branch_name: &str) -> Result<PathBuf, String>
{
self.prepare_autocommit_directory();
let result_directory_path = self.autocommit_directory.as_ref().unwrap().path().join(branch_name).join(directory_path);
match create_dir_all(&result_directory_path)
{
Ok(_) => (),
Err(error) => panic!("Could not create result directory: {}", error),
}
{
let lock_file_path = result_directory_path.join(".lock");
if lock_file_path.exists()
{
panic!("Autocommit directory “{}” already locked", lock_file_path.display());
}
if let Err(error) = File::create(&lock_file_path)
{
panic!("Could not create temporary file “{}”: {}", lock_file_path.display(), error);
}
}
let mut autocommit_locks = self.autocommit_locks.lock().unwrap();
*autocommit_locks += 1;
Ok(result_directory_path)
}
pub fn wait_for_autocommit_thread(mut self)
{
if let None = self.autocommit_thread
{
panic!("No autocommit thread started");
}
let lock_file_path = self.autocommit_directory.as_ref().unwrap().path().join(".lock");
if let Err(error) = remove_file(&lock_file_path)
{
panic!("Could not remove lock file “{}”: {}", lock_file_path.display(), error);
}
info!("Removed lock file");
let autocommit_thread = self.autocommit_thread;
if let Err(_) = autocommit_thread.unwrap().join()
{
panic!("Could not join autocommit thread");
}
self.autocommit_thread = None;
}
}
#[cfg(test)]