-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor how loading the mock data is done
- Loading branch information
1 parent
4060940
commit f0a81f3
Showing
16 changed files
with
186 additions
and
160 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
use log::info; | ||
|
||
use std::process::Stdio; | ||
|
||
use neo4rs::query; | ||
|
||
use pubky_nexus::{ | ||
db::connectors::redis::get_redis_conn, get_neo4j_graph, reindex, Config, StackManager, | ||
}; | ||
|
||
#[tokio::main] | ||
async fn main() { | ||
let config = Config::from_env(); | ||
StackManager::setup(&config).await; | ||
info!("Running mock db sync"); | ||
let args: Vec<String> = std::env::args().collect(); | ||
match args.get(1).map(String::as_str) { | ||
Some("graph") => { | ||
MockDB::sync_graph().await; | ||
} | ||
Some("redis") => { | ||
MockDB::sync_redis().await; | ||
} | ||
None => { | ||
MockDB::sync_graph().await; | ||
MockDB::sync_redis().await; | ||
} | ||
Some(_) => { | ||
panic!("Invalid argument. Use 'graph' or 'redis'"); | ||
} | ||
} | ||
} | ||
|
||
pub struct MockDB {} | ||
|
||
impl MockDB { | ||
async fn sync_graph() { | ||
let graph = get_neo4j_graph().expect("Failed to get Neo4j graph connection"); | ||
|
||
// drop and run the queries again | ||
let drop_all_query = query("MATCH (n) DETACH DELETE n;"); | ||
graph | ||
.lock() | ||
.await | ||
.run(drop_all_query) | ||
.await | ||
.expect("Could not drop graph nodes."); | ||
|
||
let graph_env = std::env::var("GRAPH_CONTAINER_NAME").unwrap_or("neo4j".to_string()); | ||
// Run the run-queries.sh script on the Docker host using docker exec | ||
tokio::process::Command::new("docker") | ||
.args(&[ | ||
"exec", | ||
graph_env.as_str(), | ||
"bash", | ||
"/db-graph/run-queries.sh", | ||
]) | ||
.stdout(Stdio::inherit()) | ||
.stderr(Stdio::inherit()) | ||
.status() | ||
.await | ||
.expect("Failed to run run-queries.sh"); | ||
} | ||
|
||
async fn sync_redis() { | ||
// Drop all keys in Redis | ||
let mut redis_conn = get_redis_conn() | ||
.await | ||
.expect("Could not get the redis connection"); | ||
|
||
redis::cmd("FLUSHALL") | ||
.exec_async(&mut redis_conn) | ||
.await | ||
.expect("Failed to flush Redis"); | ||
|
||
// Reindex | ||
info!("Starting reindexing process."); | ||
reindex().await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,47 +1,53 @@ | ||
use crate::config::Config; | ||
use crate::db::connectors::{ | ||
neo4j::{Neo4jConnector, NEO4J_CONNECTOR}, | ||
redis::{RedisConnector, REDIS_CONNECTOR}, | ||
}; | ||
use crate::db::graph::setup::setup_graph; | ||
use crate::{ | ||
db::connectors::{ | ||
neo4j::{Neo4jConnector, NEO4J_CONNECTOR}, | ||
redis::{RedisConnector, REDIS_CONNECTOR}, | ||
}, | ||
Config, | ||
}; | ||
use log::{debug, info}; | ||
|
||
async fn setup_redis(config: &Config) { | ||
let redis_connector = RedisConnector::new_connection(&config.redis_uri()) | ||
.await | ||
.expect("Failed to connect to Redis"); | ||
pub struct StackManager {} | ||
|
||
match REDIS_CONNECTOR.set(redis_connector) { | ||
Err(e) => debug!("RedisConnector was already set: {:?}", e), | ||
Ok(()) => info!("RedisConnector successfully set"), | ||
} | ||
} | ||
impl StackManager { | ||
async fn setup_redis(config: &Config) { | ||
let redis_connector = RedisConnector::new_connection(&config.redis_uri()) | ||
.await | ||
.expect("Failed to connect to Redis"); | ||
|
||
async fn setup_neo4j(config: &Config) { | ||
let neo4j_connector = Neo4jConnector::new_connection( | ||
&config.neo4j_uri(), | ||
&config.neo4j_username, | ||
&config.neo4j_password, | ||
) | ||
.await | ||
.expect("Failed to connect to Neo4j"); | ||
|
||
match NEO4J_CONNECTOR.set(neo4j_connector) { | ||
Err(e) => debug!("Neo4jConnector was already set: {:?}", e), | ||
Ok(()) => info!("Neo4jConnector successfully set"), | ||
match REDIS_CONNECTOR.set(redis_connector) { | ||
Err(e) => debug!("RedisConnector was already set: {:?}", e), | ||
Ok(()) => info!("RedisConnector successfully set"), | ||
} | ||
} | ||
|
||
// Set Neo4J graph data constraints | ||
setup_graph().await.unwrap_or_default(); | ||
} | ||
async fn setup_neo4j(config: &Config) { | ||
let neo4j_connector = Neo4jConnector::new_connection( | ||
&config.neo4j_uri(), | ||
&config.neo4j_username, | ||
&config.neo4j_password, | ||
) | ||
.await | ||
.expect("Failed to connect to Neo4j"); | ||
|
||
match NEO4J_CONNECTOR.set(neo4j_connector) { | ||
Err(e) => debug!("Neo4jConnector was already set: {:?}", e), | ||
Ok(()) => info!("Neo4jConnector successfully set"), | ||
} | ||
|
||
pub async fn setup(config: &Config) { | ||
match env_logger::try_init() { | ||
Ok(_) => info!("Env logger initiated"), | ||
Err(err) => debug!("Env logger was already set: {}", err), | ||
// Set Neo4J graph data constraints | ||
setup_graph().await.unwrap_or_default(); | ||
} | ||
|
||
// Initialize Redis and Neo4j | ||
setup_redis(config).await; | ||
setup_neo4j(config).await; | ||
pub async fn setup(config: &Config) { | ||
match env_logger::try_init() { | ||
Ok(_) => info!("Env logger initiated"), | ||
Err(err) => debug!("Env logger was already set: {}", err), | ||
} | ||
|
||
// Initialize Redis and Neo4j | ||
Self::setup_redis(config).await; | ||
Self::setup_neo4j(config).await; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.