Skip to content

Commit

Permalink
fixed database connection closing after 1,8 ks
Browse files Browse the repository at this point in the history
  • Loading branch information
9FS committed Sep 9, 2024
1 parent 62419f8 commit 5b5849f
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 7 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT"
name = "nhentai_archivist"
readme = "readme.md"
repository = "https://github.com/9-FS/nhentai_archivist"
version = "3.0.0"
version = "3.0.1"

[dependencies]
chrono = { version = "^0.4.0", features = ["serde"] }
Expand Down
6 changes: 4 additions & 2 deletions src/connect_to_db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ pub async fn connect_to_db(database_url: &str) -> Result<sqlx::sqlite::SqlitePoo

db = sqlx::sqlite::SqlitePoolOptions::new()
.max_connections(1) // only 1 connection to database at the same time, otherwise concurrent writers fail
.max_lifetime(None) // keep connection open indefinitely otherwise database locks up after lifetime, closing and reconnecting manually
.connect(database_url).await?; // connect to database
db.set_connect_options(sqlx::sqlite::SqliteConnectOptions::new()
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal) // use write-ahead journal for better performance
.locking_mode(sqlx::sqlite::SqliteLockingMode::Exclusive) // do not release file lock until all transactions are complete
.synchronous(sqlx::sqlite::SqliteSynchronous::Normal));
.synchronous(sqlx::sqlite::SqliteSynchronous::Normal)); // ensure data is written to disk after each transaction for consistent state
log::info!("Connected to database at \"{}\".", database_url);

sqlx::query(CREATE_DB_QUERY_STRING).execute(&db).await?; // initialise database by creating tables
Expand All @@ -68,11 +69,12 @@ pub async fn connect_to_db(database_url: &str) -> Result<sqlx::sqlite::SqlitePoo
{
db = sqlx::sqlite::SqlitePoolOptions::new()
.max_connections(1) // only 1 connection to database at the same time, otherwise concurrent writers fail
.max_lifetime(None) // keep connection open indefinitely otherwise database locks up after lifetime, closing and reconnecting manually
.connect(database_url).await?; // connect to database
db.set_connect_options(sqlx::sqlite::SqliteConnectOptions::new()
.journal_mode(sqlx::sqlite::SqliteJournalMode::Wal) // use write-ahead journal for better performance
.locking_mode(sqlx::sqlite::SqliteLockingMode::Exclusive) // do not release file lock until all transactions are complete
.synchronous(sqlx::sqlite::SqliteSynchronous::Normal));
.synchronous(sqlx::sqlite::SqliteSynchronous::Normal)); // ensure data is written to disk after each transaction for consistent state
log::info!("Connected to database at \"{}\".", database_url);
}

Expand Down
6 changes: 3 additions & 3 deletions src/main_inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub async fn main_inner(config: Config) -> Result<()>
{
const NHENTAI_HENTAI_SEARCH_URL: &str="https://nhentai.net/api/gallery/"; // nhentai search by id api url
const NHENTAI_TAG_SEARCH_URL: &str="https://nhentai.net/api/galleries/search"; // nhentai search by tag api url
let db: sqlx::sqlite::SqlitePool; // database containing all metadata from nhentai.net api
let mut db: sqlx::sqlite::SqlitePool; // database containing all metadata from nhentai.net api
let f0 = scaler::Formatter::new()
.set_scaling(scaler::Scaling::None)
.set_rounding(scaler::Rounding::Magnitude(0)); // formatter
Expand All @@ -23,8 +23,6 @@ pub async fn main_inner(config: Config) -> Result<()>
let timeout: std::time::Duration = std::time::Duration::from_secs(30); // connection timeout


db = connect_to_db(&config.DATABASE_URL).await?; // connect to database

{
let mut headers: reqwest::header::HeaderMap = reqwest::header::HeaderMap::new(); // headers
headers.insert(reqwest::header::USER_AGENT, reqwest::header::HeaderValue::from_str(&config.USER_AGENT).unwrap_or_else
Expand Down Expand Up @@ -59,6 +57,7 @@ pub async fn main_inner(config: Config) -> Result<()>

loop // keep running for server mode
{
db = connect_to_db(&config.DATABASE_URL).await?; // connect to database
hentai_id_list = get_hentai_id_list
(
std::path::Path::new(config.DOWNLOADME_FILEPATH.as_str()),
Expand Down Expand Up @@ -117,6 +116,7 @@ pub async fn main_inner(config: Config) -> Result<()>
{
log::error!("Deleting \"{}\" failed with: {e}", config.DOWNLOADME_FILEPATH);
}
db.close().await; // close database connection

log::info!("Sleeping for {}s...", f4.format(config.SLEEP_INTERVAL.unwrap_or_default() as f64));
tokio::time::sleep(std::time::Duration::from_secs(config.SLEEP_INTERVAL.unwrap_or_default())).await; // if in server mode: sleep for interval until next check
Expand Down

0 comments on commit 5b5849f

Please sign in to comment.