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);
|
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())
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
let mut input_file = match std::fs::File::open(file_path) {
|
let mut input_file = match std::fs::File::open(file_path) {
|
||||||
Ok(v) => v,
|
Ok(v) => v,
|
||||||
Err(err) => {
|
Err(err) => {
|
||||||
|
@ -110,6 +112,7 @@ fn main() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
None => tracker::TorrentTracker::new(cfg.get_mode().clone()),
|
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");
|
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();
|
||||||
|
|
|
@ -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;
|
||||||
|
|
Loading…
Reference in a new issue