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 dotenvy::dotenv;
use std::env; use std::env;
use std::io::Write; use std::io::Write;
use tokio::time::{self, Duration}; use tokio::task;
use tokio::time::{self, sleep, Duration};
#[tokio::main] #[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> { async fn main() -> Result<(), Box<dyn std::error::Error>> {
@ -27,15 +28,20 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut count = 0; let mut count = 0;
let client = reqwest::Client::new(); let client = reqwest::Client::new();
let mut interval = time::interval(Duration::from_secs(1800)); //let mut interval = time::interval(Duration::from_secs(1800));
loop { loop {
print!("\nWaiting for next interval tick... "); let mut handles = vec![];
std::io::stdout().flush().unwrap(); std::io::stdout().flush().unwrap();
interval.tick().await; //interval.tick().await;
let date = Local::now(); let date = Local::now();
println!("Ticked on {}", &date.format("%Y.%m.%d %H:%M")); //println!("Ticked on {}", &date.format("%Y.%m.%d %H:%M"));
for station in &stations { 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; count += 1;
let req = req let req = req
.replace("TEMPLATE_ID", &station) .replace("TEMPLATE_ID", &station)
@ -45,9 +51,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
std::io::stdout().flush().unwrap(); std::io::stdout().flush().unwrap();
if let Ok(doc) = client if let Ok(doc) = client
.get(req.replace("TEMPLATE_DEPARR", "arr")) .get(req.replace("TEMPLATE_DEPARR", "arr"))
.header("Authorization", &token) //.header("Authorization", &token)
.send() .send()
.await? .await
.unwrap()
.json::<Root>() .json::<Root>()
.await .await
{ {
@ -60,9 +67,10 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
} }
if let Ok(doc) = client if let Ok(doc) = client
.get(req.replace("TEMPLATE_DEPARR", "dep")) .get(req.replace("TEMPLATE_DEPARR", "dep"))
.header("Authorization", &token) //.header("Authorization", &token)
.send() .send()
.await? .await
.unwrap()
.json::<Root>() .json::<Root>()
.await .await
{ {
@ -73,6 +81,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
} else { } else {
println!("\x1B[2K\rFailed to fetch departures for station {}", station); println!("\x1B[2K\rFailed to fetch departures for station {}", station);
} }
}));
}
for handle in handles {
handle.await?;
sleep(Duration::from_millis(100)).await;
} }
count = 0; count = 0;
} }

View file

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