Overview
Comment: | tweak logging, simplify connection objects, use async_std primitives |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk | v0.4.4 |
Files: | files | file ages | folders |
SHA3-256: |
c6d3e97290af14d66ff2ac496b426767 |
User & Date: | arcade on 2025-07-01 11:13:24.551 |
Other Links: | manifest | tags |
Context
2025-07-09
| ||
05:35 | bump and switch error handling check-in: 44575a91d3 user: arcade tags: trunk, v0.4.5 | |
2025-07-01
| ||
11:13 | tweak logging, simplify connection objects, use async_std primitives check-in: c6d3e97290 user: arcade tags: trunk, v0.4.4 | |
2025-06-29
| ||
19:04 | fix error formatting check-in: 5a4aab7687 user: arcade tags: trunk, v0.4.3 | |
Changes
Modified Cargo.lock
from [5165f92f08]
to [29f8c1785d].
︙ | ︙ | |||
994 995 996 997 998 999 1000 | "futures-core", "js-sys", "wasm-bindgen", ] [[package]] name = "h2" | | | | 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 | "futures-core", "js-sys", "wasm-bindgen", ] [[package]] name = "h2" version = "0.4.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "17da50a276f1e01e0ba6c029e47b7100754904ee8a278f886546e98575380785" dependencies = [ "atomic-waker", "bytes", "fnv", "futures-core", "futures-sink", "http", |
︙ | ︙ | |||
2001 2002 2003 2004 2005 2006 2007 | name = "regex-syntax" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "reqwest" | | | | 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 | name = "regex-syntax" version = "0.8.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c" [[package]] name = "reqwest" version = "0.12.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "4c8cea6b35bcceb099f30173754403d2eba0a5dc18cea3630fccd88251909288" dependencies = [ "async-compression", "base64", "bytes", "encoding_rs", "futures-core", "futures-util", |
︙ | ︙ | |||
2096 2097 2098 2099 2100 2101 2102 | "derive_builder", "never", "quick-xml", ] [[package]] name = "rsstg" | | | 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 | "derive_builder", "never", "quick-xml", ] [[package]] name = "rsstg" version = "0.4.4" dependencies = [ "anyhow", "async-std", "atom_syndication", "chrono", "config", "futures", |
︙ | ︙ | |||
2302 2303 2304 2305 2306 2307 2308 | "itoa", "ryu", "serde", ] [[package]] name = "serde_with" | | | | | | 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 | "itoa", "ryu", "serde", ] [[package]] name = "serde_with" version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f2c45cd61fefa9db6f254525d46e392b852e0e61d9a1fd36e5bd183450a556d5" dependencies = [ "serde", "serde_derive", "serde_with_macros", ] [[package]] name = "serde_with_macros" version = "3.14.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "de90945e6565ce0d9a25098082ed4ee4002e047cb59892c318d66821e14bb30f" dependencies = [ "darling", "proc-macro2", "quote", "syn 2.0.104", ] |
︙ | ︙ |
Modified Cargo.toml
from [3b85882dc8]
to [fed847982a].
1 2 | [package] name = "rsstg" | | | 1 2 3 4 5 6 7 8 9 10 | [package] name = "rsstg" version = "0.4.4" authors = ["arcade"] edition = "2021" [dependencies] anyhow = "1.0.86" async-std = { version = "1.12.0", features = [ "attributes", "tokio1" ] } atom_syndication = { version = "0.12.4", features = [ "with-serde" ] } |
︙ | ︙ |
Modified src/core.rs
from [28430d049a]
to [a915a95027].
1 2 3 4 5 6 7 8 9 10 11 | use crate::{ command, sql::Db, }; use std::{ borrow::Cow, collections::{ BTreeMap, HashSet, }, | < < < < | > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | use crate::{ command, sql::Db, }; use std::{ borrow::Cow, collections::{ BTreeMap, HashSet, }, }; use anyhow::{ anyhow, bail, Result, }; use async_std::{ task, sync::{ Arc, Mutex }, }; use chrono::DateTime; use lazy_static::lazy_static; use regex::Regex; use tgbot::{ api::Client, handler::UpdateHandler, types::{ |
︙ | ︙ | |||
114 115 116 117 118 119 120 | } pub async fn check (&self, id: i32, real: bool) -> Result<String> { let mut posted: i32 = 0; let mut conn = self.db.begin().await?; let id = { | | | 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | } pub async fn check (&self, id: i32, real: bool) -> Result<String> { let mut posted: i32 = 0; let mut conn = self.db.begin().await?; let id = { let mut set = self.sources.lock_arc().await; match set.get(&id) { Some(id) => id.clone(), None => { let id = Arc::new(id); set.insert(id.clone()); id.clone() }, |
︙ | ︙ | |||
219 220 221 222 223 224 225 | Ok(Some(source)) => source.to_string(), Ok(None) => "Source not found in database?".to_string(), Err(err) => format!("Failed to fetch source data:\n{err}"), } }; task::spawn(async move { if let Err(err) = clone.check(source_id, true).await { | | | 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | Ok(Some(source)) => source.to_string(), Ok(None) => "Source not found in database?".to_string(), Err(err) => format!("Failed to fetch source data:\n{err}"), } }; task::spawn(async move { if let Err(err) = clone.check(source_id, true).await { if let Err(err) = clone.send(&format!("{source}\n\nš {}", encode(&err.to_string())), None, Some(ParseMode::MarkdownV2)).await { eprintln!("Check error: {err:?}"); // clone.disable(&source_id, owner).await.unwrap(); }; }; }); } } else if next_fetch - now < delay { |
︙ | ︙ |
Modified src/sql.rs
from [ae3b49a367]
to [e784f243af].
︙ | ︙ | |||
31 32 33 34 35 36 37 | pub url: String, pub iv_hash: Option<String>, pub url_re: Option<String>, } impl fmt::Display for List { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { | | | 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | pub url: String, pub iv_hash: Option<String>, pub url_re: Option<String>, } impl fmt::Display for List { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> { write!(f, "#{} \\*ļøā£ `{}` {}\nš `{}`", self.source_id, self.channel, match self.enabled { true => "š enabled", false => "ā disabled", }, self.url)?; if let Some(iv_hash) = &self.iv_hash { write!(f, "\nIV: `{iv_hash}`")?; } |
︙ | ︙ | |||
63 64 65 66 67 68 69 | pub struct Queue { pub source_id: Option<i32>, pub next_fetch: Option<DateTime<Local>>, pub owner: Option<i64>, } #[derive(Clone)] | | | < | < < < | | | | | | | < < | | | | | | | | | | | | | | | | 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 | pub struct Queue { pub source_id: Option<i32>, pub next_fetch: Option<DateTime<Local>>, pub owner: Option<i64>, } #[derive(Clone)] pub struct Db ( Arc<Mutex<sqlx::Pool<sqlx::Postgres>>>, ); impl Db { pub fn new (pguri: &str) -> Result<Db> { Ok(Db ( Arc::new(Mutex::new(PgPoolOptions::new() .max_connections(5) .acquire_timeout(std::time::Duration::new(300, 0)) .idle_timeout(std::time::Duration::new(60, 0)) .connect_lazy(pguri)?)), )) } pub async fn begin(&self) -> Result<Conn> { let pool = self.0.lock_arc().await; let conn = Conn ( pool.acquire().await? ); Ok(conn) } } pub struct Conn ( PoolConnection<Postgres>, ); impl Conn { pub async fn add_post (&mut self, source_id: i32, date: &DateTime<FixedOffset>, post_url: &str) -> Result<()> { sqlx::query("insert into rsstg_post (source_id, posted, url) values ($1, $2, $3);") .bind(source_id) .bind(date) .bind(post_url) .execute(&mut *self.0).await?; Ok(()) } pub async fn clean <I> (&mut self, source_id: i32, owner: I) -> Result<Cow<'_, str>> where I: Into<i64> { match sqlx::query("delete from rsstg_post p using rsstg_source s where p.source_id = $1 and owner = $2 and p.source_id = s.source_id;") .bind(source_id) .bind(owner.into()) .execute(&mut *self.0).await?.rows_affected() { 0 => { Ok("No data found found.".into()) }, x => { Ok(format!("{x} posts purged.").into()) }, } } pub async fn delete <I> (&mut self, source_id: i32, owner: I) -> Result<Cow<'_, str>> where I: Into<i64> { match sqlx::query("delete from rsstg_source where source_id = $1 and owner = $2;") .bind(source_id) .bind(owner.into()) .execute(&mut *self.0).await?.rows_affected() { 0 => { Ok("No data found found.".into()) }, x => { Ok(format!("{} sources removed.", x).into()) }, } } pub async fn disable <I> (&mut self, source_id: i32, owner: I) -> Result<&str> where I: Into<i64> { match sqlx::query("update rsstg_source set enabled = false where source_id = $1 and owner = $2") .bind(source_id) .bind(owner.into()) .execute(&mut *self.0).await?.rows_affected() { 1 => { Ok("Source disabled.") }, 0 => { Ok("Source not found.") }, _ => { bail!("Database error.") }, } } pub async fn enable <I> (&mut self, source_id: i32, owner: I) -> Result<&str> where I: Into<i64> { match sqlx::query("update rsstg_source set enabled = true where source_id = $1 and owner = $2") .bind(source_id) .bind(owner.into()) .execute(&mut *self.0).await?.rows_affected() { 1 => { Ok("Source enabled.") }, 0 => { Ok("Source not found.") }, _ => { bail!("Database error.") }, } } pub async fn exists <I> (&mut self, post_url: &str, id: I) -> Result<Option<bool>> where I: Into<i64> { let row = sqlx::query("select exists(select true from rsstg_post where url = $1 and source_id = $2) as exists;") .bind(post_url) .bind(id.into()) .fetch_one(&mut *self.0).await?; let exists: Option<bool> = row.try_get("exists")?; Ok(exists) } pub async fn get_queue (&mut self) -> Result<Vec<Queue>> { let block: Vec<Queue> = sqlx::query_as("select source_id, next_fetch, owner from rsstg_order natural left join rsstg_source where next_fetch < now() + interval '1 minute';") .fetch_all(&mut *self.0).await?; Ok(block) } pub async fn get_list <I> (&mut self, owner: I) -> Result<Vec<List>> where I: Into<i64> { let source: Vec<List> = sqlx::query_as("select source_id, channel, enabled, url, iv_hash, url_re from rsstg_source where owner = $1 order by source_id") .bind(owner.into()) .fetch_all(&mut *self.0).await?; Ok(source) } pub async fn get_one <I> (&mut self, owner: I, id: i32) -> Result<Option<List>> where I: Into<i64> { let source: Option<List> = sqlx::query_as("select source_id, channel, enabled, url, iv_hash, url_re from rsstg_source where owner = $1 and source_id = $2") .bind(owner.into()) .bind(id) .fetch_optional(&mut *self.0).await?; Ok(source) } pub async fn get_source <I> (&mut self, id: i32, owner: I) -> Result<Source> where I: Into<i64> { let source: Source = sqlx::query_as("select channel_id, url, iv_hash, owner, url_re from rsstg_source where source_id = $1 and owner = $2") .bind(id) .bind(owner.into()) .fetch_one(&mut *self.0).await?; Ok(source) } pub async fn set_scrape <I> (&mut self, id: I) -> Result<()> where I: Into<i64> { sqlx::query("update rsstg_source set last_scrape = now() where source_id = $1;") .bind(id.into()) .execute(&mut *self.0).await?; Ok(()) } pub async fn update <I> (&mut self, update: Option<i32>, channel: &str, channel_id: i64, url: &str, iv_hash: Option<&str>, url_re: Option<&str>, owner: I) -> Result<&str> where I: Into<i64> { match match update { Some(id) => { sqlx::query("update rsstg_source set channel_id = $2, url = $3, iv_hash = $4, owner = $5, channel = $6, url_re = $7 where source_id = $1") .bind(id) }, None => { sqlx::query("insert into rsstg_source (channel_id, url, iv_hash, owner, channel, url_re) values ($1, $2, $3, $4, $5, $6)") }, } .bind(channel_id) .bind(url) .bind(iv_hash) .bind(owner.into()) .bind(channel) .bind(url_re) .execute(&mut *self.0).await { Ok(_) => Ok(match update { Some(_) => "Channel updated.", None => "Channel added.", }), Err(sqlx::Error::Database(err)) => { match err.downcast::<sqlx::postgres::PgDatabaseError>().routine() { |
︙ | ︙ |