feat(rust): ping-collector, phase 1 of the Rust rewrite
CI / Format & lint (push) Successful in 19s
CI / Test suite (push) Successful in 47s
Docs / Validate documentation (push) Successful in 19s
Rust / fmt, clippy, test, build (push) Successful in 7m35s
Security / Dependency vulnerabilities (push) Successful in 1m37s
Security / Static analysis (push) Successful in 17s
Security / License report (push) Successful in 1m30s
CI / Format & lint (push) Successful in 19s
CI / Test suite (push) Successful in 47s
Docs / Validate documentation (push) Successful in 19s
Rust / fmt, clippy, test, build (push) Successful in 7m35s
Security / Dependency vulnerabilities (push) Successful in 1m37s
Security / Static analysis (push) Successful in 17s
Security / License report (push) Successful in 1m30s
rust/collector replaces the collection half of the Python web app: RID hourly gauges (with the late-report semantics: store everything, retry every 10 min while RID is behind, re-fetch yesterday's holes, startup gap fill), HII rain + water level, RID dams (today + yesterday) and Open-Meteo rain. Same tables and upsert semantics, one UNNEST statement per table, Bangkok wall-clock time regardless of the server time zone. Python side: COLLECTION_ENABLED=false makes the API leader stop collecting and instead re-forecast + notify when the newest RID reading in the database advances (polled every FORECAST_POLL_SECONDS) and once per clock hour; /scraping/status then serves the collector's status file; /scrape/trigger answers 409. Verified: 20 Rust tests (parsers, gap/schedule logic, database tests against PostgreSQL); live parity run of both collectors into two empty databases: all 9 tables row-for-row identical (scripts/collector_parity.py); the Python API served health, measurements, stats, status and model forecasts from a database filled only by ping-collector. 177 Python tests pass. reqwest uses native-tls: app.rid.go.th offers only TLS 1.2 CBC suites, which rustls refuses. Deploy/cutover/rollback: scripts/install_collector.sh, scripts/ping-collector.service; CI: .gitea/workflows/rust.yml (fmt, clippy, tests with a PostgreSQL service, release artifact).
This commit is contained in:
1 parent
e3694d7591
commit
4e619aa2ae
26 files changed
+5836
-15
No files matched your search
@@ -0,0 +1,273 @@
|
||||
//! The long-running collection loop and the one-shot commands.
|
||||
|
||||
use crate::config::{Config, USER_AGENT};
|
||||
use crate::dams::DamsClient;
|
||||
use crate::db;
|
||||
use crate::hii::HiiClient;
|
||||
use crate::rain::OpenMeteoClient;
|
||||
use crate::rid::{self, CycleOutcome, RidClient};
|
||||
use crate::stations::Stations;
|
||||
use crate::util::{bangkok_now, floor_hour};
|
||||
use anyhow::Result;
|
||||
use chrono::{Duration, NaiveDateTime};
|
||||
use serde_json::json;
|
||||
use sqlx::PgPool;
|
||||
|
||||
pub struct Collector {
|
||||
pub cfg: Config,
|
||||
pub pool: PgPool,
|
||||
pub stations: Stations,
|
||||
pub rid: RidClient,
|
||||
hii: HiiClient,
|
||||
dams: DamsClient,
|
||||
rain: OpenMeteoClient,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
struct Stats {
|
||||
total_runs: u64,
|
||||
successful_runs: u64,
|
||||
failed_runs: u64,
|
||||
last_run: Option<NaiveDateTime>,
|
||||
next_run: Option<NaiveDateTime>,
|
||||
feed_current: Option<bool>,
|
||||
latest_reading: Option<NaiveDateTime>,
|
||||
readings_saved: usize,
|
||||
}
|
||||
|
||||
fn iso(t: Option<NaiveDateTime>) -> serde_json::Value {
|
||||
t.map_or(serde_json::Value::Null, |t| {
|
||||
json!(t.format("%Y-%m-%dT%H:%M:%S").to_string())
|
||||
})
|
||||
}
|
||||
|
||||
impl Collector {
|
||||
pub fn new(cfg: Config, pool: PgPool, stations: Stations) -> Result<Self> {
|
||||
let http = reqwest::Client::builder()
|
||||
.user_agent(USER_AGENT)
|
||||
.connect_timeout(std::time::Duration::from_secs(20))
|
||||
.build()?;
|
||||
Ok(Self {
|
||||
rid: RidClient::new(http.clone(), cfg.rid_api_url.clone()),
|
||||
hii: HiiClient::new(http.clone(), cfg.hii_api_base.clone()),
|
||||
dams: DamsClient::new(http.clone(), cfg.dams_url.clone()),
|
||||
rain: OpenMeteoClient::new(http, cfg.openmeteo_url.clone()),
|
||||
cfg,
|
||||
pool,
|
||||
stations,
|
||||
})
|
||||
}
|
||||
|
||||
/// HII rain + water level, RID dams (today and yesterday), Open-Meteo.
|
||||
/// Each source is failure-isolated.
|
||||
pub async fn collect_hourly(&self, now: NaiveDateTime) {
|
||||
if self.cfg.hii_enabled {
|
||||
match self.hii.fetch_rain(self.cfg.hii_basin_code).await {
|
||||
Ok(r) => match db::save_hii_rain(&self.pool, &r, now).await {
|
||||
Ok(n) => tracing::info!("HII rainfall: {n} rows saved"),
|
||||
Err(e) => tracing::error!("HII rainfall save failed: {e:#}"),
|
||||
},
|
||||
Err(e) => tracing::error!("HII rainfall fetch failed: {e:#}"),
|
||||
}
|
||||
match self.hii.fetch_waterlevel(self.cfg.hii_basin_code).await {
|
||||
Ok(r) => match db::save_hii_waterlevel(&self.pool, &r, now).await {
|
||||
Ok(n) => tracing::info!("HII water level: {n} rows saved"),
|
||||
Err(e) => tracing::error!("HII water level save failed: {e:#}"),
|
||||
},
|
||||
Err(e) => tracing::error!("HII water level fetch failed: {e:#}"),
|
||||
}
|
||||
}
|
||||
if self.cfg.dams_enabled {
|
||||
for date in [None, Some(now.date() - Duration::days(1))] {
|
||||
match self.dams.fetch_day(date).await {
|
||||
Ok(r) => match db::save_dams(&self.pool, &r, now).await {
|
||||
Ok(n) => tracing::info!("RID dams {date:?}: {n} dam-day rows saved"),
|
||||
Err(e) => tracing::error!("RID dams save failed: {e:#}"),
|
||||
},
|
||||
Err(e) => tracing::error!("RID dams fetch {date:?} failed: {e:#}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
if self.cfg.rain_enabled {
|
||||
match self.rain.fetch_forecast(5, 2).await {
|
||||
Ok(r) => match db::save_rain(&self.pool, &r).await {
|
||||
Ok(n) => tracing::info!("Open-Meteo rain: {n} hourly rows upserted"),
|
||||
Err(e) => tracing::error!("Open-Meteo save failed: {e:#}"),
|
||||
},
|
||||
Err(e) => tracing::error!("Open-Meteo fetch failed: {e:#}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn write_status(&self, stats: &Stats, running: bool) {
|
||||
let Some(path) = &self.cfg.status_file else {
|
||||
return;
|
||||
};
|
||||
let body = json!({
|
||||
"is_running": running,
|
||||
"total_runs": stats.total_runs,
|
||||
"successful_runs": stats.successful_runs,
|
||||
"failed_runs": stats.failed_runs,
|
||||
"last_run": iso(stats.last_run),
|
||||
"next_run": iso(stats.next_run),
|
||||
"feed_current": stats.feed_current,
|
||||
"latest_reading": iso(stats.latest_reading),
|
||||
"readings_saved": stats.readings_saved,
|
||||
"collector": concat!("ping-collector ", env!("CARGO_PKG_VERSION")),
|
||||
});
|
||||
let tmp = path.with_extension("json.tmp");
|
||||
let result =
|
||||
std::fs::write(&tmp, body.to_string()).and_then(|_| std::fs::rename(&tmp, path));
|
||||
if let Err(e) = result {
|
||||
tracing::warn!("could not write status file {}: {e}", path.display());
|
||||
}
|
||||
}
|
||||
|
||||
/// The service: startup gap fill, then RID polls on the hour (+offset)
|
||||
/// with retries while RID is behind, and the hourly sources once per
|
||||
/// clock hour. Returns when a shutdown signal arrives.
|
||||
pub async fn run(&self) -> Result<()> {
|
||||
db::ensure_schema(&self.pool).await?;
|
||||
let shutdown = shutdown_signal();
|
||||
tokio::pin!(shutdown);
|
||||
|
||||
if self.cfg.gap_fill_days_on_start > 0 {
|
||||
let days = Some(self.cfg.gap_fill_days_on_start);
|
||||
tokio::select! {
|
||||
r = rid::fill_gaps(&self.rid, &self.pool, &self.stations, days, bangkok_now()) => {
|
||||
if let Err(e) = r { tracing::error!("startup gap fill failed: {e:#}") }
|
||||
}
|
||||
_ = &mut shutdown => return Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
let mut stats = Stats::default();
|
||||
let mut last_hourly_slot: Option<NaiveDateTime> = None;
|
||||
loop {
|
||||
let start = bangkok_now();
|
||||
self.write_status(&stats, true);
|
||||
let outcome: CycleOutcome =
|
||||
rid::run_cycle(&self.rid, &self.pool, &self.stations, start).await;
|
||||
stats.total_runs += 1;
|
||||
stats.last_run = Some(start);
|
||||
if outcome.current {
|
||||
stats.successful_runs += 1;
|
||||
} else {
|
||||
stats.failed_runs += 1;
|
||||
}
|
||||
stats.feed_current = Some(outcome.current);
|
||||
stats.readings_saved = outcome.saved;
|
||||
if outcome.latest.is_some() {
|
||||
stats.latest_reading = outcome.latest.max(stats.latest_reading);
|
||||
}
|
||||
|
||||
let slot = floor_hour(bangkok_now());
|
||||
if last_hourly_slot != Some(slot) {
|
||||
last_hourly_slot = Some(slot);
|
||||
self.collect_hourly(bangkok_now()).await;
|
||||
}
|
||||
|
||||
let now = bangkok_now();
|
||||
let next = rid::next_poll_time(
|
||||
now,
|
||||
outcome.current,
|
||||
self.cfg.poll_offset_minutes,
|
||||
self.cfg.retry_minutes,
|
||||
);
|
||||
stats.next_run = Some(next);
|
||||
self.write_status(&stats, false);
|
||||
tracing::info!(
|
||||
"next RID poll at {}{}",
|
||||
next.format("%H:%M"),
|
||||
if outcome.current {
|
||||
""
|
||||
} else {
|
||||
" (current hour not published yet)"
|
||||
}
|
||||
);
|
||||
let wait = (next - now)
|
||||
.to_std()
|
||||
.unwrap_or_default()
|
||||
.max(std::time::Duration::from_secs(30));
|
||||
tokio::select! {
|
||||
_ = tokio::time::sleep(wait) => {}
|
||||
_ = &mut shutdown => {
|
||||
tracing::info!("shutdown signal received");
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// One cycle of every source; with `dry_run` nothing is written and the
|
||||
/// RID fetch is compared against what the database already holds.
|
||||
pub async fn once(&self, dry_run: bool) -> Result<()> {
|
||||
let now = bangkok_now();
|
||||
if !dry_run {
|
||||
db::ensure_schema(&self.pool).await?;
|
||||
let o = rid::run_cycle(&self.rid, &self.pool, &self.stations, now).await;
|
||||
println!(
|
||||
"RID: reports {:?}, {} fetched, {} saved, latest {:?}, current hour in: {}{}",
|
||||
o.dates,
|
||||
o.fetched,
|
||||
o.saved,
|
||||
o.latest,
|
||||
o.current,
|
||||
o.error.map(|e| format!(", error: {e}")).unwrap_or_default()
|
||||
);
|
||||
self.collect_hourly(now).await;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let dates = rid::cycle_dates(&self.pool, now).await;
|
||||
let (readings, error) = rid::fetch_dates(&self.rid, &dates).await;
|
||||
let stored = db::count_stored_rid(&self.pool, &readings).await?;
|
||||
let latest = readings.iter().map(|r| r.timestamp).max();
|
||||
let db_latest = db::latest_rid_timestamp(&self.pool).await?;
|
||||
println!(
|
||||
"RID (dry run): reports {dates:?}: {} readings fetched, {stored} already stored, {} would be added; latest fetched {latest:?}, latest stored {db_latest:?}{}",
|
||||
readings.len(),
|
||||
readings.len() as i64 - stored,
|
||||
error.map(|e| format!(", error: {e}")).unwrap_or_default()
|
||||
);
|
||||
if self.cfg.hii_enabled {
|
||||
match self.hii.fetch_rain(self.cfg.hii_basin_code).await {
|
||||
Ok(r) => println!("HII rainfall (dry run): {} Ping-basin stations", r.len()),
|
||||
Err(e) => println!("HII rainfall (dry run): {e:#}"),
|
||||
}
|
||||
match self.hii.fetch_waterlevel(self.cfg.hii_basin_code).await {
|
||||
Ok(r) => println!("HII water level (dry run): {} Ping-basin stations", r.len()),
|
||||
Err(e) => println!("HII water level (dry run): {e:#}"),
|
||||
}
|
||||
}
|
||||
if self.cfg.dams_enabled {
|
||||
match self.dams.fetch_day(None).await {
|
||||
Ok(r) => println!("RID dams (dry run): {} dams today", r.len()),
|
||||
Err(e) => println!("RID dams (dry run): {e:#}"),
|
||||
}
|
||||
}
|
||||
if self.cfg.rain_enabled {
|
||||
match self.rain.fetch_forecast(5, 2).await {
|
||||
Ok(r) => println!("Open-Meteo (dry run): {} hourly rows", r.len()),
|
||||
Err(e) => println!("Open-Meteo (dry run): {e:#}"),
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
async fn shutdown_signal() {
|
||||
#[cfg(unix)]
|
||||
{
|
||||
use tokio::signal::unix::{SignalKind, signal};
|
||||
let mut term = signal(SignalKind::terminate()).expect("SIGTERM handler");
|
||||
tokio::select! {
|
||||
_ = tokio::signal::ctrl_c() => {}
|
||||
_ = term.recv() => {}
|
||||
}
|
||||
}
|
||||
#[cfg(not(unix))]
|
||||
{
|
||||
let _ = tokio::signal::ctrl_c().await;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user