loading and saving databases
This commit is contained in:
parent
6541921d0c
commit
0f0210f6b8
22
src/main.rs
22
src/main.rs
|
@ -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();
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in a new issue