Refactor access of current time
This commit is contained in:
parent
00d0195595
commit
504184e69b
|
@ -1,10 +1,8 @@
|
|||
use crate::error;
|
||||
use crate::queries::*;
|
||||
use sqlite::*;
|
||||
use std::fs::{self, OpenOptions};
|
||||
use std::io;
|
||||
use std::path::{Path, PathBuf};
|
||||
use std::time::SystemTime;
|
||||
|
||||
pub struct DatasetMetadata {
|
||||
pub last_sync: Timestamp,
|
||||
|
@ -239,18 +237,17 @@ pub fn get_metadata(
|
|||
.and_then(Result::ok))
|
||||
}
|
||||
|
||||
pub fn update_last_sync(connection: &Connection, dataset: &str) -> sqlite::Result<()> {
|
||||
pub fn update_last_sync(
|
||||
connection: &Connection,
|
||||
dataset: &str,
|
||||
current_time: Timestamp,
|
||||
) -> sqlite::Result<()> {
|
||||
let query = "UPDATE datasets SET last_sync = :sync WHERE name = :dataset";
|
||||
|
||||
let current_time = SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap_or_else(|_| error("System time is before the Unix epoch (1970)!", 2))
|
||||
.as_secs();
|
||||
|
||||
connection
|
||||
.prepare(query)?
|
||||
.into_iter()
|
||||
.bind((":sync", current_time as i64))?
|
||||
.bind((":sync", current_time.0 as i64))?
|
||||
.bind((":dataset", dataset))?
|
||||
.try_for_each(|x| x.map(|_| ()))
|
||||
}
|
||||
|
|
|
@ -170,7 +170,7 @@ fn dataset_list() {
|
|||
print!(" - \x1b[33mRun 'startrnr sync' to update!\x1b[0m");
|
||||
} else {
|
||||
print!(
|
||||
" - \x1b[33mRun 'startrnr sync \"{}\"' to update!\x1b[0m",
|
||||
" - \x1b[33mRun 'startrnr sync {:?}' to update!\x1b[0m",
|
||||
name
|
||||
);
|
||||
}
|
||||
|
@ -529,15 +529,18 @@ fn sync(datasets: Vec<String>, all: bool, auth_token: Option<String>) {
|
|||
datasets
|
||||
};
|
||||
|
||||
let current_time = current_time();
|
||||
|
||||
for dataset in datasets {
|
||||
let dataset_config = get_metadata(&connection, &dataset)
|
||||
.expect("Error communicating with SQLite")
|
||||
.unwrap_or_else(|| error(&format!("Dataset {} does not exist!", dataset), 1));
|
||||
|
||||
sync_dataset(&connection, &dataset, dataset_config, &auth)
|
||||
sync_dataset(&connection, &dataset, dataset_config, current_time, &auth)
|
||||
.expect("Error communicating with SQLite");
|
||||
// .unwrap_or_else(|_| error("Error communicating with SQLite", 2));
|
||||
|
||||
update_last_sync(&connection, &dataset).expect("Error communicating with SQLite");
|
||||
update_last_sync(&connection, &dataset, current_time)
|
||||
.expect("Error communicating with SQLite");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -289,6 +289,7 @@ pub fn sync_dataset(
|
|||
connection: &Connection,
|
||||
dataset: &str,
|
||||
metadata: DatasetMetadata,
|
||||
current_time: Timestamp,
|
||||
auth: &str,
|
||||
) -> sqlite::Result<()> {
|
||||
let events = get_tournament_events(&metadata, auth)
|
||||
|
|
12
src/util.rs
12
src/util.rs
|
@ -1,9 +1,10 @@
|
|||
use sqlite::*;
|
||||
use std::io::{self, Write};
|
||||
use std::process::exit;
|
||||
use std::time::SystemTime;
|
||||
|
||||
use crate::database::*;
|
||||
use crate::queries::{PlayerData, PlayerId};
|
||||
use crate::queries::{PlayerData, PlayerId, Timestamp};
|
||||
|
||||
pub const SECS_IN_HR: u64 = 3600;
|
||||
pub const SECS_IN_DAY: u64 = SECS_IN_HR * 24;
|
||||
|
@ -19,6 +20,15 @@ pub fn issue(msg: &str, code: i32) -> ! {
|
|||
exit(code)
|
||||
}
|
||||
|
||||
pub fn current_time() -> Timestamp {
|
||||
Timestamp(
|
||||
SystemTime::now()
|
||||
.duration_since(SystemTime::UNIX_EPOCH)
|
||||
.unwrap_or_else(|_| error("System time is before the Unix epoch (1970)!", 2))
|
||||
.as_secs(),
|
||||
)
|
||||
}
|
||||
|
||||
pub fn read_string() -> String {
|
||||
let mut line = String::new();
|
||||
io::stdout()
|
||||
|
|
Loading…
Reference in a new issue