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 /src | |
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
Diffstat (limited to 'src')
-rw-r--r-- | src/database.rs | 29 | ||||
-rw-r--r-- | src/main.rs | 24 |
2 files changed, 42 insertions, 11 deletions
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 { |