CSV Worker function that processes phone numbers from the queue
(
work_rx: async_channel::Receiver<WorkerMessage>,
result_tx: async_channel::Sender<ResultMessage>,
counters: Arc<Counters>,
subnet: String,
lookup_type: LookupType
)
| 35 | |
| 36 | // CSV Worker function that processes phone numbers from the queue |
| 37 | pub async fn csv_worker( |
| 38 | work_rx: async_channel::Receiver<WorkerMessage>, |
| 39 | result_tx: async_channel::Sender<ResultMessage>, |
| 40 | counters: Arc<Counters>, |
| 41 | subnet: String, |
| 42 | lookup_type: LookupType |
| 43 | ) { |
| 44 | let mut client = crate::utils::create_client(Some(&subnet), ""); |
| 45 | let mut last_auth_refresh = std::time::Instant::now(); |
| 46 | let auth_refresh_interval = Duration::from_secs(8 * 60 * 60); // Refresh auth every 8 hours |
| 47 | |
| 48 | while let Ok(message) = work_rx.recv().await { |
| 49 | match message { |
| 50 | WorkerMessage::CheckPhone { record_id, phone, identifier: _identifier, first_name, last_name, pending_counter } => { |
| 51 | // Get a reference to the counter for decrementing when done |
| 52 | let decrement_counter = || { |
| 53 | if let Some(counter) = &pending_counter { |
| 54 | counter.fetch_sub(1, Ordering::SeqCst); |
| 55 | } |
| 56 | }; |
| 57 | |
| 58 | // Check if we need to refresh authentication |
| 59 | if last_auth_refresh.elapsed() >= auth_refresh_interval { |
| 60 | if let Ok(_) = auth::get_auth_credentials().await { |
| 61 | last_auth_refresh = std::time::Instant::now(); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | // Skip processing for completion marker |
| 66 | if phone.starts_with("COMPLETION_MARKER_") { |
| 67 | decrement_counter(); |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | // Process the phone number |
| 72 | counters.requests.fetch_add(1, Ordering::Relaxed); |
| 73 | |
| 74 | // Validate phone number |
| 75 | let parsed_number = match format!("+{}", phone).parse::<phonenumber::PhoneNumber>() { |
| 76 | Ok(number) => number, |
| 77 | Err(_) => { |
| 78 | counters.success.fetch_add(1, Ordering::Relaxed); |
| 79 | decrement_counter(); |
| 80 | continue; |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | if !phonenumber::is_valid(&parsed_number) { |
| 85 | counters.success.fetch_add(1, Ordering::Relaxed); |
| 86 | decrement_counter(); |
| 87 | continue; |
| 88 | } |
| 89 | |
| 90 | // Similar to the original worker function but streamlined for CSV mode |
| 91 | for attempt in 0..3 { // Limited retries |
| 92 | let lookup_result = match lookup_type { |
| 93 | LookupType::Js => js::lookup(&client, &phone, &first_name, &last_name).await, |
| 94 | LookupType::NoJS => nojs::lookup(&client, &phone, &first_name, &last_name).await, |
no test coverage detected