diff options
author | Jakob Stendahl <jakob.stendahl@outlook.com> | 2022-02-14 12:45:55 +0100 |
---|---|---|
committer | Jakob Stendahl <jakob.stendahl@outlook.com> | 2022-02-14 12:45:55 +0100 |
commit | 2096790f3904371c9ff6090f1ff846593c7df9c4 (patch) | |
tree | 96bf58bc2324675b0b9a7cdf9527bfbe7262ccbc | |
parent | f4a5b478ad103c1fd53d229fc18f2c17a95c8bf1 (diff) | |
download | RSS-watcher-2096790f3904371c9ff6090f1ff846593c7df9c4.tar.gz RSS-watcher-2096790f3904371c9ff6090f1ff846593c7df9c4.zip |
:children_crossing: Catch and deal with database connection error(s)v0.2.0
-rw-r--r-- | Cargo.lock | 45 | ||||
-rw-r--r-- | Readme.md | 5 | ||||
-rw-r--r-- | src/database.rs | 29 | ||||
-rw-r--r-- | src/main.rs | 24 |
4 files changed, 49 insertions, 54 deletions
@@ -38,12 +38,6 @@ dependencies = [ ] [[package]] -name = "arrayvec" -version = "0.7.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6" - -[[package]] name = "atty" version = "0.2.14" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -176,7 +170,7 @@ dependencies = [ "libc", "num-integer", "num-traits", - "time 0.1.44", + "time", "winapi", ] @@ -1037,11 +1031,14 @@ dependencies = [ "pem", "percent-encoding", "rustls", + "rustls-pemfile", "serde", "serde_json", "socket2", "twox-hash", "url", + "webpki", + "webpki-roots", ] [[package]] @@ -1066,7 +1063,6 @@ dependencies = [ "num-traits", "rand 0.8.4", "regex", - "rust_decimal", "saturating", "serde", "serde_json", @@ -1075,7 +1071,6 @@ dependencies = [ "smallvec", "subprocess", "thiserror", - "time 0.3.6", ] [[package]] @@ -1166,15 +1161,6 @@ dependencies = [ ] [[package]] -name = "num_threads" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "71a1eb3a36534514077c1e079ada2fb170ef30c47d203aa6916138cf882ecd52" -dependencies = [ - "libc", -] - -[[package]] name = "once_cell" version = "1.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1523,17 +1509,6 @@ dependencies = [ ] [[package]] -name = "rust_decimal" -version = "1.20.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e0593ce4677e3800ddafb3de917e8397b1348e06e688128ade722d88fbe11ebf" -dependencies = [ - "arrayvec", - "num-traits", - "serde", -] - -[[package]] name = "rustc-hash" version = "1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1843,16 +1818,6 @@ dependencies = [ ] [[package]] -name = "time" -version = "0.3.6" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c8d54b9298e05179c335de2b9645d061255bcd5155f843b3e328d2cfe0a5b413" -dependencies = [ - "libc", - "num_threads", -] - -[[package]] name = "tinyvec" version = "1.5.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -2244,5 +2209,5 @@ dependencies = [ "log", "mac", "markup5ever", - "time 0.1.44", + "time", ] @@ -6,7 +6,8 @@ and pushes those to Gotify. This can be run using docker or locally, to run with docker you can ``` $ run -it --rm -e DB_HOST=<database host> -e DB_USER=<database user> \ - -e DB_PASS=<database password> -e DB_BASE=<database name> jakobst1n/rss-watcher + -e DB_PASS=<database password> -e DB_BASE=<database name> \ + --restart=unless-stopped jakobst1n/rss-watcher ``` To run locally you need to set all those environment variables, and then you can run it with @@ -32,5 +33,3 @@ will poll for new changes (in ms). ## Todo - Extract more RSS fields. - Deal with multiple links. -- Add error handling for database connection, currently the process exits (which is why you might want to use `--restart=unless-stopped` -- Add proper error handling for gotify failures diff --git a/src/database.rs b/src/database.rs index 95115dc..21699e2 100644 --- a/src/database.rs +++ b/src/database.rs @@ -1,6 +1,6 @@ use std::process; use std::env; -use log::{info, warn, error}; +use log::{debug, info, warn, error}; use mysql::*; use mysql::prelude::*; @@ -30,13 +30,13 @@ fn build_opts() -> Opts { .db_name(Some(db_base))); } -pub fn new_conn() -> Conn { +pub fn new_conn() -> Option<Conn> { let conn_res = Conn::new(build_opts()); if let Err(ref x) = conn_res { error!("Could not connect to database ({:#?})...", x); - process::exit(1); + return None; } - return conn_res.unwrap(); + return Some(conn_res.unwrap()); } /** @@ -120,7 +120,7 @@ pub fn bootstrap() { /** * This will fetch all feeds from the database and return them as a Vector. */ -pub fn get_feeds(conn: &mut Conn) -> Vec<FeedConf> { +pub fn get_feeds(conn: &mut Conn) -> Option<Vec<FeedConf>> { let q = "SELECT `id`, \ `url`, \ `last_fetch`, \ @@ -133,15 +133,28 @@ pub fn get_feeds(conn: &mut Conn) -> Vec<FeedConf> { let res = conn.query_map(q, |(id,url,last_fetch,title,message,push_url,push_token)| { FeedConf{id,url,last_fetch,title,message,push_url,push_token} - },).unwrap(); - return res; + },); + debug!("{:#?}", res); + match res { + Ok(r) => return Some(r), + Err(e) => { + error!("Could not get feeds from database ({:?})", e); + return None; + } + } } /** * Method that updates the last fetch time timestamp in the database */ pub fn update_last_fetch(feed_id: u32, last_fetch: i64, conn: &mut Conn) { - let mut tx = conn.start_transaction(TxOpts::default()).unwrap(); + let res_tx = conn.start_transaction(TxOpts::default()); + if let Err(x) = res_tx { + error!("Could not create transaction for updating last fetch time! {:#?}", x); + return; + } + let mut tx = res_tx.unwrap(); + let q = "UPDATE `rss-watcher-feeds` SET last_fetch=? WHERE id=?"; if let Err(x) = tx.exec_drop(q, (last_fetch,feed_id,)) { warn!("Could not update last fetch time...! ({:#?}", x); diff --git a/src/main.rs b/src/main.rs index 705fe20..02b386f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -117,6 +117,7 @@ async fn fetch_feed(feed_conf: &FeedConf, last_fetch_time: DateTime<Utc>) -> Res Ok(None) } else { let feed = parser::parse(&resp.bytes().await?[..])?; + debug!("{:#?}", feed); Ok(Some(feed)) } } @@ -177,9 +178,26 @@ async fn get_feed(feed_conf: &FeedConf) -> bool { * This gets all feeds from the database and fetches them once. */ async fn main_loop() { - let mut conn = database::new_conn(); - info!("== Checking for new feed entries now") - for feed in database::get_feeds(&mut conn) { + info!("========== Checking for new feed entries now"); + + let res_conn = database::new_conn(); + if let None = res_conn { + error!("Could not open database connection, waiting until next iteration before trying again!"); + return; + }; + let mut conn = res_conn.unwrap(); + + let res_feeds = database::get_feeds(&mut conn); + + if let None = res_feeds { + error!("Could not get feeds, waiting until next iteration before trying again!"); + return; + } + + let feeds = res_feeds.unwrap(); + info!(" Got {} feeds to check", feeds.len()); + + for feed in feeds { let time_now = Utc::now(); let res = get_feed(&feed).await; if res { |