:)
This commit is contained in:
		
							
								
								
									
										15
									
								
								src/db.rs
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								src/db.rs
									
									
									
									
									
								
							@@ -1,21 +1,17 @@
 | 
				
			|||||||
use metrics::counter;
 | 
					use metrics::counter;
 | 
				
			||||||
 | 
					use std::fmt::Debug;
 | 
				
			||||||
use serde::{Deserialize, Serialize};
 | 
					use serde::{Deserialize, Serialize};
 | 
				
			||||||
use std::{fmt::Debug, sync::LazyLock, time::Instant};
 | 
					 | 
				
			||||||
use surrealdb::{
 | 
					use surrealdb::{
 | 
				
			||||||
    engine::remote::ws::{Client, Ws},
 | 
					    engine::remote::ws::{Client, Ws},
 | 
				
			||||||
    opt::auth::Root,
 | 
					    opt::auth::Root,
 | 
				
			||||||
    sql::Thing,
 | 
					    sql::Thing,
 | 
				
			||||||
    Surreal,
 | 
					    Surreal,
 | 
				
			||||||
};
 | 
					};
 | 
				
			||||||
use tokio::sync::Mutex;
 | 
					 | 
				
			||||||
use tracing::{error, instrument, trace};
 | 
					use tracing::{error, instrument, trace};
 | 
				
			||||||
use url::Url;
 | 
					use url::Url;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use crate::Config;
 | 
					use crate::Config;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
static LOCK: LazyLock<Mutex<bool>> = LazyLock::new(|| Mutex::new(true));
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
const TIME_SPENT_ON_LOCK: &str = "surql_lock_waiting_ms";
 | 
					 | 
				
			||||||
const STORE: &str = "surql_store_calls";
 | 
					const STORE: &str = "surql_store_calls";
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Hash)]
 | 
					#[derive(Serialize, Deserialize, Clone, Eq, PartialEq, Hash)]
 | 
				
			||||||
@@ -57,14 +53,6 @@ impl Website {
 | 
				
			|||||||
        counter!(STORE).increment(1);
 | 
					        counter!(STORE).increment(1);
 | 
				
			||||||
        let mut things = Vec::with_capacity(all.len());
 | 
					        let mut things = Vec::with_capacity(all.len());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        // TODO this only allows for one thread to be in the database at a time.
 | 
					 | 
				
			||||||
        // This is currently required since otherwise we get write errors.
 | 
					 | 
				
			||||||
        // If the default `crawled` is set to false, we might not need to write any more
 | 
					 | 
				
			||||||
        // than just the name. `accessed_at` is fun but not needed.
 | 
					 | 
				
			||||||
        let now = Instant::now();
 | 
					 | 
				
			||||||
        let lock = LOCK.lock().await;
 | 
					 | 
				
			||||||
        counter!(TIME_SPENT_ON_LOCK).increment(now.elapsed().as_millis() as u64);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
        match db
 | 
					        match db
 | 
				
			||||||
            .query(
 | 
					            .query(
 | 
				
			||||||
                "INSERT INTO website $array
 | 
					                "INSERT INTO website $array
 | 
				
			||||||
@@ -85,7 +73,6 @@ impl Website {
 | 
				
			|||||||
                error!("{:?}", err);
 | 
					                error!("{:?}", err);
 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        drop(lock);
 | 
					 | 
				
			||||||
        things
 | 
					        things
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,7 @@
 | 
				
			|||||||
use std::{ffi::OsStr, path::PathBuf};
 | 
					use std::{ffi::OsStr, path::PathBuf};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
use tokio::fs;
 | 
					use tokio::fs;
 | 
				
			||||||
use tracing::{debug, error, info, instrument, trace, warn};
 | 
					use tracing::{debug, error, instrument, trace, warn};
 | 
				
			||||||
use url::Url;
 | 
					use url::Url;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
#[instrument(skip(data))]
 | 
					#[instrument(skip(data))]
 | 
				
			||||||
@@ -21,7 +21,7 @@ pub async fn store(data: &str, url: &Url) {
 | 
				
			|||||||
        (url_path.clone(), "index.html".into())
 | 
					        (url_path.clone(), "index.html".into())
 | 
				
			||||||
    };
 | 
					    };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    info!("Writing at: {:?} {:?}", basepath, filename);
 | 
					    debug!("Writing at: {:?} {:?}", basepath, filename);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // create the folders
 | 
					    // create the folders
 | 
				
			||||||
    if let Err(err) = fs::create_dir_all(&basepath).await {
 | 
					    if let Err(err) = fs::create_dir_all(&basepath).await {
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user