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,7 +94,9 @@ fn main() {
let file_path = std::path::Path::new(path);
if !file_path.exists() {
warn!("database file \"{}\" doesn't exist.", path);
tracker::TorrentTracker::new(cfg.get_mode().clone())
}
else {
let mut input_file = match std::fs::File::open(file_path) {
Ok(v) => v,
Err(err) => {
@ -110,6 +112,7 @@ fn main() {
}
}
}
}
None => tracker::TorrentTracker::new(cfg.get_mode().clone()),
};
@ -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");
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() {
if let Some(thread) = threads.pop() {
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.
pub fn set_torrent_flag(&self, info_hash: &InfoHash, is_flagged: bool) {
if let Some(mut entry) = self
if let Some(entry) = self
.database
.torrent_peers
.write()
@ -369,7 +369,7 @@ impl TorrentTracker {
serde_json::to_writer(compressor, &db)
}
fn cleanup(&mut self) {
fn cleanup(&self) {
use std::ops::Add;
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
self.cleanup();
// save db.
match std::fs::File::open(db_path) {
match std::fs::File::create(db_path) {
Err(err) => {
error!("failed to open file '{}': {}", db_path, err);
return;

View file

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