|
| 1 | +use clap::{Arg, ArgMatches, Command}; |
| 2 | +use color_eyre::eyre::Result; |
| 3 | +use std::sync::Arc; |
| 4 | +use tokio::sync::Mutex; |
| 5 | +use tracing::{error, info}; |
| 6 | +use waypoint::{ |
| 7 | + backfill::root_parent::{RootParentBackfill, RootParentBackfillConfig, backfill_url_parents}, |
| 8 | + config::Config, |
| 9 | + hub::client::Hub, |
| 10 | +}; |
| 11 | + |
| 12 | +/// Register root_parent backfill commands |
| 13 | +pub fn register_commands(app: Command) -> Command { |
| 14 | + app.about("Backfill root_parent columns for existing casts") |
| 15 | + .arg_required_else_help(true) |
| 16 | + .arg( |
| 17 | + Arg::new("hash-parents") |
| 18 | + .long("hash-parents") |
| 19 | + .help("Backfill casts with hash-based parents (requires Hub traversal)") |
| 20 | + .action(clap::ArgAction::SetTrue), |
| 21 | + ) |
| 22 | + .arg( |
| 23 | + Arg::new("url-parents") |
| 24 | + .long("url-parents") |
| 25 | + .help("Backfill casts with URL parents (simple SQL update)") |
| 26 | + .action(clap::ArgAction::SetTrue), |
| 27 | + ) |
| 28 | + .arg( |
| 29 | + Arg::new("all") |
| 30 | + .long("all") |
| 31 | + .help("Backfill both hash-based and URL parents") |
| 32 | + .action(clap::ArgAction::SetTrue), |
| 33 | + ) |
| 34 | + .arg( |
| 35 | + Arg::new("batch-size") |
| 36 | + .long("batch-size") |
| 37 | + .value_name("SIZE") |
| 38 | + .help("Number of casts to process per batch (hash-parents only)") |
| 39 | + .default_value("100") |
| 40 | + .value_parser(clap::value_parser!(usize)), |
| 41 | + ) |
| 42 | + .arg( |
| 43 | + Arg::new("max-depth") |
| 44 | + .long("max-depth") |
| 45 | + .value_name("DEPTH") |
| 46 | + .help("Maximum parent chain depth to traverse (hash-parents only)") |
| 47 | + .default_value("100") |
| 48 | + .value_parser(clap::value_parser!(usize)), |
| 49 | + ) |
| 50 | + .arg( |
| 51 | + Arg::new("batch-delay") |
| 52 | + .long("batch-delay") |
| 53 | + .value_name("MS") |
| 54 | + .help("Delay between batches in milliseconds (hash-parents only)") |
| 55 | + .default_value("100") |
| 56 | + .value_parser(clap::value_parser!(u64)), |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +/// Handle root_parent backfill command |
| 61 | +pub async fn handle_command(matches: &ArgMatches, config: &Config) -> Result<()> { |
| 62 | + let hash_parents = matches.get_flag("hash-parents"); |
| 63 | + let url_parents = matches.get_flag("url-parents"); |
| 64 | + let all = matches.get_flag("all"); |
| 65 | + |
| 66 | + let batch_size = *matches.get_one::<usize>("batch-size").unwrap(); |
| 67 | + let max_depth = *matches.get_one::<usize>("max-depth").unwrap(); |
| 68 | + let batch_delay = *matches.get_one::<u64>("batch-delay").unwrap(); |
| 69 | + |
| 70 | + if !hash_parents && !url_parents && !all { |
| 71 | + println!("Please specify one of: --all, --hash-parents, or --url-parents"); |
| 72 | + return Ok(()); |
| 73 | + } |
| 74 | + |
| 75 | + let do_hash = hash_parents || all; |
| 76 | + let do_url = url_parents || all; |
| 77 | + |
| 78 | + info!("Initializing resources for root_parent backfill..."); |
| 79 | + |
| 80 | + // Initialize database |
| 81 | + let database = waypoint::database::client::Database::new(&config.database).await?; |
| 82 | + let pool = &database.pool; |
| 83 | + |
| 84 | + // Run URL parents backfill (simple SQL, no Hub needed) |
| 85 | + if do_url { |
| 86 | + info!("Backfilling URL parents..."); |
| 87 | + match backfill_url_parents(pool).await { |
| 88 | + Ok(count) => { |
| 89 | + info!("URL parents backfill complete: {} casts updated", count); |
| 90 | + }, |
| 91 | + Err(e) => { |
| 92 | + error!("URL parents backfill failed: {}", e); |
| 93 | + if !do_hash { |
| 94 | + std::process::exit(1); |
| 95 | + } |
| 96 | + }, |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + // Run hash parents backfill (requires Hub traversal) |
| 101 | + if do_hash { |
| 102 | + info!("Backfilling hash-based parents (this may take a while)..."); |
| 103 | + |
| 104 | + // Initialize Hub client |
| 105 | + let hub = Arc::new(Mutex::new(Hub::new(config.hub.clone())?)); |
| 106 | + |
| 107 | + // Connect to Hub |
| 108 | + { |
| 109 | + let mut hub_guard = hub.lock().await; |
| 110 | + if !hub_guard.check_connection().await.unwrap_or(false) { |
| 111 | + error!("Failed to connect to Hub - required for hash-based backfill"); |
| 112 | + std::process::exit(1); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + let backfill_config = |
| 117 | + RootParentBackfillConfig { batch_size, max_depth, batch_delay_ms: batch_delay }; |
| 118 | + |
| 119 | + let backfiller = RootParentBackfill::new(pool.clone(), hub, backfill_config); |
| 120 | + |
| 121 | + match backfiller.run().await { |
| 122 | + Ok(stats) => { |
| 123 | + info!("Hash parents backfill complete:"); |
| 124 | + info!(" Casts processed: {}", stats.casts_processed); |
| 125 | + info!(" Roots found: {}", stats.roots_found); |
| 126 | + info!(" Broken chains: {}", stats.chains_broken); |
| 127 | + info!(" Errors: {}", stats.errors); |
| 128 | + |
| 129 | + if stats.errors > 0 { |
| 130 | + std::process::exit(1); |
| 131 | + } |
| 132 | + }, |
| 133 | + Err(e) => { |
| 134 | + error!("Hash parents backfill failed: {}", e); |
| 135 | + std::process::exit(1); |
| 136 | + }, |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + info!("Root parent backfill completed successfully!"); |
| 141 | + Ok(()) |
| 142 | +} |
0 commit comments