async hell

This commit is contained in:
theBreadCompany 2025-02-03 19:36:29 +01:00
parent e7cfc8217a
commit 16f369d73a
12 changed files with 54 additions and 41 deletions

View file

@ -6,7 +6,8 @@ use sqlx::postgres::PgPoolOptions;
use dotenvy::dotenv;
use std::env;
use std::io::Write;
use tokio::time::{self, Duration};
use tokio::task;
use tokio::time::{self, sleep, Duration};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -27,52 +28,64 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut count = 0;
let client = reqwest::Client::new();
let mut interval = time::interval(Duration::from_secs(1800));
//let mut interval = time::interval(Duration::from_secs(1800));
loop {
print!("\nWaiting for next interval tick... ");
let mut handles = vec![];
std::io::stdout().flush().unwrap();
interval.tick().await;
//interval.tick().await;
let date = Local::now();
println!("Ticked on {}", &date.format("%Y.%m.%d %H:%M"));
for station in &stations {
count += 1;
let req = req
.replace("TEMPLATE_ID", &station)
.replace("TEMPLATE_DATE", &date.format("%Y%m%d").to_string())
.replace("TEMPLATE_TIME", &date.format("%H%M").to_string());
print!("\x1B[2K\rFetching {}/{} ({}) ", count, total, station);
std::io::stdout().flush().unwrap();
if let Ok(doc) = client
.get(req.replace("TEMPLATE_DEPARR", "arr"))
.header("Authorization", &token)
.send()
.await?
.json::<Root>()
.await
{
match insert_document(doc, &mut conn).await {
Ok(_) => {}
Err(e) => println!("\x1B[2K\rError inserting document: {}", e),
//println!("Ticked on {}", &date.format("%Y.%m.%d %H:%M"));
for station in &stations {
let station = station.clone();
let req = req.clone();
let client = client.clone();
let mut conn = conn.clone();
handles.push(task::spawn(async move {
count += 1;
let req = req
.replace("TEMPLATE_ID", &station)
.replace("TEMPLATE_DATE", &date.format("%Y%m%d").to_string())
.replace("TEMPLATE_TIME", &date.format("%H%M").to_string());
print!("\x1B[2K\rFetching {}/{} ({}) ", count, total, station);
std::io::stdout().flush().unwrap();
if let Ok(doc) = client
.get(req.replace("TEMPLATE_DEPARR", "arr"))
//.header("Authorization", &token)
.send()
.await
.unwrap()
.json::<Root>()
.await
{
match insert_document(doc, &mut conn).await {
Ok(_) => {}
Err(e) => println!("\x1B[2K\rError inserting document: {}", e),
}
} else {
println!("\x1B[2K\rFailed to fetch arrivals for station {}", station);
}
} else {
println!("\x1B[2K\rFailed to fetch arrivals for station {}", station);
}
if let Ok(doc) = client
.get(req.replace("TEMPLATE_DEPARR", "dep"))
.header("Authorization", &token)
.send()
.await?
.json::<Root>()
.await
{
match insert_document(doc, &mut conn).await {
Ok(_) => {}
Err(e) => println!("\x1B[2K\rError inserting document: {}", e),
if let Ok(doc) = client
.get(req.replace("TEMPLATE_DEPARR", "dep"))
//.header("Authorization", &token)
.send()
.await
.unwrap()
.json::<Root>()
.await
{
match insert_document(doc, &mut conn).await {
Ok(_) => {}
Err(e) => println!("\x1B[2K\rError inserting document: {}", e),
}
} else {
println!("\x1B[2K\rFailed to fetch departures for station {}", station);
}
} else {
println!("\x1B[2K\rFailed to fetch departures for station {}", station);
}));
}
for handle in handles {
handle.await?;
sleep(Duration::from_millis(100)).await;
}
count = 0;
}

View file

@ -19,7 +19,7 @@ async fn main() -> Result<(), Box<dyn Error>> {
.connect(&url)
.await?;
let src = "input";
let src = "input/xslt_dm_request";
let total = ReadDirStream::new(fs::read_dir(src).await?).count().await;
let mut stream = ReadDirStream::new(fs::read_dir(src).await?);
let mut count = 0;