Skip to content

Commit

Permalink
Use cron format for jobs (stats saving/digest) (#93)
Browse files Browse the repository at this point in the history
* Use cron-syntax for save/digest times

* fix tasks

* Add abort handle to pool
  • Loading branch information
Jerboa-app authored May 20, 2024
1 parent 4a03bfa commit 3d21c89
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 130 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "busser"
version = "0.2.1"
version = "0.2.2"
authors = ["Jerboa"]

edition="2021"
Expand All @@ -26,6 +26,7 @@ libflate = "2"
quick-xml = "0.31.0"
indicatif = "0.17.8"
uuid = { version = "1.8.0", features = ["v4", "fast-rng", "macro-diagnostics"]}
cron = "0.12.1"

[profile.dev]
opt-level = 0
Expand Down
15 changes: 6 additions & 9 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,20 @@ use serde::{Serialize, Deserialize};
use crate::{filesystem::file::read_file_utf8, integrations::webhook::Webhook};

/// Configure the stats collection
/// - ```save_period_seconds```: periodically save to disc
/// - ```path```: where to save to disc (time-stamped files)
/// - ```hit_cooloff_seconds```: cooloff period after which the same IP is counted as a new hit
/// - ```clear_period_seconds```: periodcially clear data in memory
/// - ```digest_period_seconds```: periodically send a digts to a Discord webhook
/// - ```log_files_clear_period_seconds```:archive and clear stats log files periodically
/// - ```save_schedule```: periodically save to disc, cron format: "sec min hour day-of-month month day-of-week year"
/// - ```digest_schedule```: periodically send a digts to a Discord webhook, cron format: "sec min hour day-of-month month day-of-week year"
/// - ```ignore_regexes```: collect, but do not report, hits on these regexes
/// - ```top_n_digest```: top n listing of pages and resources in API/discord default is 3
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StatsConfig
{
pub save_period_seconds: u64,
pub path: String,
pub hit_cooloff_seconds: u64,
pub digest_period_seconds: u64,
pub log_files_clear_period_seconds: u64,
pub save_schedule: Option<String>,
pub digest_schedule: Option<String>,
pub ignore_regexes: Option<Vec<String>>,
pub top_n_digest: Option<usize>
}
Expand All @@ -31,11 +29,10 @@ impl StatsConfig
{
StatsConfig
{
save_period_seconds: 86400,
path: "stats".to_string(),
hit_cooloff_seconds: 60,
digest_period_seconds: 86400,
log_files_clear_period_seconds: 2419200,
save_schedule: None,
digest_schedule: None,
ignore_regexes: None,
top_n_digest: None
}
Expand Down
7 changes: 5 additions & 2 deletions src/filesystem/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,11 @@ pub trait Observed

pub fn write_file_bytes(path: &str, data: &[u8])
{
let mut file = fs::File::create(path).unwrap();
file.write_all(data).unwrap();
match fs::File::create(path)
{
Ok(mut file) => file.write_all(data).unwrap(),
Err(e) => {crate::debug(format!("Error {} creating file {}", e, path), None)}
}
}

pub fn read_file_utf8(path: &str) -> Option<String>
Expand Down
26 changes: 20 additions & 6 deletions src/server/https.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::
{
config::{read_config, Config, CONFIG_PATH}, content::{filter::ContentFilter, sitemap::SiteMap, Content}, server::throttle::{handle_throttle, IpThrottler}, task::TaskPool, CRAB
config::{read_config, Config, CONFIG_PATH}, content::{filter::ContentFilter, sitemap::SiteMap, Content}, server::throttle::{handle_throttle, IpThrottler}, task::{schedule_from_option, TaskPool}, CRAB
};

use std::{collections::HashMap, net::{IpAddr, Ipv4Addr, SocketAddr}};
use std::net::{IpAddr, Ipv4Addr, SocketAddr};
use std::path::PathBuf;
use std::sync::Arc;
use tokio::sync::Mutex;
Expand All @@ -15,7 +15,7 @@ use axum::
};
use axum_server::tls_rustls::RustlsConfig;

use super::{api::{stats::StatsDigest, ApiRequest}, stats::{digest::Digest, hits::{log_stats, HitStats}, StatsSaveTask, StatsDigestTask}};
use super::{api::{stats::StatsDigest, ApiRequest}, stats::{hits::{log_stats, HitStats}, StatsSaveTask, StatsDigestTask}};

/// An https server that reads a directory configured with [Config]
/// ```.html``` pages and resources, then serves them.
Expand Down Expand Up @@ -138,18 +138,32 @@ impl Server
{
addr: SocketAddr::new(IpAddr::V4(Ipv4Addr::new(a,b,c,d)), config.port_https),
router,
config,
config: config.clone(),
tasks: TaskPool::new()
};

server.tasks.add
(
Box::new(StatsSaveTask { state: stats.clone(), last_run: chrono::offset::Utc::now() })
Box::new
(
StatsSaveTask::new
(
stats.clone(),
schedule_from_option(config.stats.digest_schedule.clone())
)
)
);

server.tasks.add
(
Box::new(StatsDigestTask { state: stats.clone(), last_run: chrono::offset::Utc::now() })
Box::new
(
StatsDigestTask::new
(
stats.clone(),
schedule_from_option(config.stats.digest_schedule.clone())
)
)
);

server
Expand Down
6 changes: 0 additions & 6 deletions src/server/stats/hits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ pub struct Hit
pub struct HitStats
{
pub hits: HashMap<[u8; 64], Hit>,
pub last_save: DateTime<chrono::Utc>,
pub last_digest: DateTime<chrono::Utc>,
pub last_clear: DateTime<chrono::Utc>,
pub summary: Digest
}

Expand All @@ -37,9 +34,6 @@ impl HitStats
HitStats
{
hits: HashMap::new(),
last_save: chrono::offset::Utc::now(),
last_digest: chrono::offset::Utc::now(),
last_clear: chrono::offset::Utc::now(),
summary: Digest::new()
}
}
Expand Down
Loading

0 comments on commit 3d21c89

Please sign in to comment.