loading and saving databases

This commit is contained in:
Naim A 2018-12-09 01:19:04 +02:00
parent 6541921d0c
commit 0f0210f6b8
No known key found for this signature in database
GPG key ID: FD7948915D9EF8B9
3 changed files with 38 additions and 17 deletions

View file

@ -94,19 +94,22 @@ fn main() {
let file_path = std::path::Path::new(path); let file_path = std::path::Path::new(path);
if !file_path.exists() { if !file_path.exists() {
warn!("database file \"{}\" doesn't exist.", path); warn!("database file \"{}\" doesn't exist.", path);
tracker::TorrentTracker::new(cfg.get_mode().clone())
} }
let mut input_file = match std::fs::File::open(file_path) { else {
Ok(v) => v, let mut input_file = match std::fs::File::open(file_path) {
Err(err) => { Ok(v) => v,
error!("failed to open \"{}\". error: {}", path.as_str(), err); Err(err) => {
panic!("error opening file. check logs."); error!("failed to open \"{}\". error: {}", path.as_str(), err);
} panic!("error opening file. check logs.");
}; }
match tracker::TorrentTracker::load_database(cfg.get_mode().clone(), &mut input_file) { };
Ok(v) => v, match tracker::TorrentTracker::load_database(cfg.get_mode().clone(), &mut input_file) {
Err(err) => { Ok(v) => v,
error!("failed to load database. error: {}", err); Err(err) => {
panic!("failed to load database. check logs."); error!("failed to load database. error: {}", err);
panic!("failed to load database. check logs.");
}
} }
} }
} }
@ -124,7 +127,7 @@ fn main() {
}); });
} }
let udp_server = std::sync::Arc::new(server::UDPTracker::new(cfg, tracker.clone()).unwrap()); let udp_server = std::sync::Arc::new(server::UDPTracker::new(cfg.clone(), tracker.clone()).unwrap());
trace!("Waiting for UDP packets"); trace!("Waiting for UDP packets");
let logical_cpus = num_cpus::get(); let logical_cpus = num_cpus::get();
@ -142,6 +145,23 @@ fn main() {
})); }));
} }
match cfg.get_db_path() {
Some(db_path) => {
let db_p = db_path.clone();
let tracker_clone = tracker.clone();
std::thread::spawn(move || {
loop {
std::thread::sleep_ms(1000 * 120);
debug!("periodically saving database.");
tracker_clone.periodic_task(db_p.as_str());
debug!("database saved.");
}
});
},
None => {}
}
while !threads.is_empty() { while !threads.is_empty() {
if let Some(thread) = threads.pop() { if let Some(thread) = threads.pop() {
let _ = thread.join(); let _ = thread.join();

View file

@ -282,7 +282,7 @@ impl TorrentTracker {
/// flagged torrents will result in a tracking error. This is to allow enforcement against piracy. /// flagged torrents will result in a tracking error. This is to allow enforcement against piracy.
pub fn set_torrent_flag(&self, info_hash: &InfoHash, is_flagged: bool) { pub fn set_torrent_flag(&self, info_hash: &InfoHash, is_flagged: bool) {
if let Some(mut entry) = self if let Some(entry) = self
.database .database
.torrent_peers .torrent_peers
.write() .write()
@ -369,7 +369,7 @@ impl TorrentTracker {
serde_json::to_writer(compressor, &db) serde_json::to_writer(compressor, &db)
} }
fn cleanup(&mut self) { fn cleanup(&self) {
use std::ops::Add; use std::ops::Add;
let now = std::time::SystemTime::now(); let now = std::time::SystemTime::now();
@ -414,12 +414,12 @@ impl TorrentTracker {
} }
} }
pub fn periodic_task(&mut self, db_path: &str) { pub fn periodic_task(&self, db_path: &str) {
// cleanup db // cleanup db
self.cleanup(); self.cleanup();
// save db. // save db.
match std::fs::File::open(db_path) { match std::fs::File::create(db_path) {
Err(err) => { Err(err) => {
error!("failed to open file '{}': {}", db_path, err); error!("failed to open file '{}': {}", db_path, err);
return; return;

View file

@ -1,4 +1,5 @@
mode = "dynamic" mode = "dynamic"
db_path = "database.json.bz2"
[udp] [udp]
announce_interval = 120 # Two minutes announce_interval = 120 # Two minutes