feat: ability to update stacks db from cli + fix caching logic

This commit is contained in:
Ludo Galabru
2023-06-09 10:08:35 -04:00
parent 93d35976f8
commit 3ea9f597af
3 changed files with 38 additions and 10 deletions

View File

@@ -197,14 +197,14 @@ pub async fn download_stacks_dataset_if_required(config: &mut Config, ctx: &Cont
};
let should_download = match (local_sha_file, remote_sha_file) {
(Ok(local), Ok(remote_response)) => {
let cache_invalidated = remote_response.starts_with(&local[0..32]) == false;
if cache_invalidated {
let cache_not_expired = remote_response.starts_with(&local[0..32]) == false;
if cache_not_expired {
info!(
ctx.expect_logger(),
"More recent Stacks archive file detected"
);
}
cache_invalidated
cache_not_expired == false
}
(_, _) => {
info!(

View File

@@ -1,3 +1,4 @@
use crate::archive::download_stacks_dataset_if_required;
use crate::block::DigestingCommand;
use crate::config::generator::generate_config;
use crate::config::{Config, PredicatesApi};
@@ -238,6 +239,9 @@ enum StacksDbCommand {
/// Check integrity
#[clap(name = "check", bin_name = "check")]
Check(CheckDbCommand),
/// Update database using latest Stacks archive file
#[clap(name = "update", bin_name = "update")]
Update(UpdateDbCommand),
}
#[derive(Subcommand, PartialEq, Clone, Debug)]
@@ -374,6 +378,13 @@ struct CheckDbCommand {
pub config_path: Option<String>,
}
#[derive(Parser, PartialEq, Clone, Debug)]
struct UpdateDbCommand {
/// Load config file path
#[clap(long = "config-path")]
pub config_path: Option<String>,
}
#[derive(Parser, PartialEq, Clone, Debug)]
struct InitHordDbCommand {
/// Load config file path
@@ -869,6 +880,10 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
unimplemented!()
}
},
Command::Stacks(StacksCommand::Db(StacksDbCommand::Update(cmd))) => {
let mut config = Config::default(false, false, false, &cmd.config_path)?;
download_stacks_dataset_if_required(&mut config, &ctx).await;
}
Command::Stacks(StacksCommand::Db(StacksDbCommand::Check(cmd))) => {
let config = Config::default(false, false, false, &cmd.config_path)?;
// Delete data, if any
@@ -891,7 +906,10 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
}
}
if missing_blocks.is_empty() {
info!(ctx.expect_logger(), "Stacks db successfully checked ({min}, {max})");
info!(
ctx.expect_logger(),
"Stacks db successfully checked ({min}, {max})"
);
} else {
warn!(
ctx.expect_logger(),

View File

@@ -370,14 +370,20 @@ pub fn update_predicate_status(
predicates_db_conn: &mut Connection,
ctx: &Context,
) {
let serialized_status = json!(status).to_string();
if let Err(e) =
predicates_db_conn.hset::<_, _, _, ()>(&predicate_key, "status", json!(status).to_string())
predicates_db_conn.hset::<_, _, _, ()>(&predicate_key, "status", &serialized_status)
{
error!(
ctx.expect_logger(),
"Error updating status: {}",
e.to_string()
);
} else {
info!(
ctx.expect_logger(),
"Updating predicate {predicate_key} status: {serialized_status}"
);
}
}
@@ -387,16 +393,20 @@ pub fn update_predicate_spec(
predicates_db_conn: &mut Connection,
ctx: &Context,
) {
if let Err(e) = predicates_db_conn.hset::<_, _, _, ()>(
&predicate_key,
"specification",
json!(spec).to_string(),
) {
let serialized_spec = json!(spec).to_string();
if let Err(e) =
predicates_db_conn.hset::<_, _, _, ()>(&predicate_key, "specification", &serialized_spec)
{
error!(
ctx.expect_logger(),
"Error updating status: {}",
e.to_string()
);
} else {
info!(
ctx.expect_logger(),
"Updating predicate {predicate_key} with spec: {serialized_spec}"
);
}
}