Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error handling #107

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 30 additions & 6 deletions src/api/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use reqwest::{Client, Response};
use serde_json::json;
use solana_client::rpc_client::RpcClient;
use solana_sdk::pubkey::Pubkey;
use std::sync::atomic::{Ordering};
use std::thread;
use std::time::{Duration, Instant};

Expand All @@ -13,6 +14,7 @@ use crate::api::models::{
VerifyResponse,
};
use crate::solana_program::get_program_pda;
use crate::SIGNAL_RECEIVED;
use crate::{get_genesis_hash, MAINNET_GENESIS_HASH};

// URL for the remote server
Expand All @@ -27,8 +29,16 @@ fn loading_animation(receiver: Receiver<bool>) {

let pb = ProgressBar::new_spinner();
pb.set_style(spinner_style);
pb.enable_steady_tick(Duration::from_millis(100));
pb.set_message("Request sent. Awaiting server response. This may take a moment... ⏳");

loop {
// Check if interrupt signal was received
if SIGNAL_RECEIVED.load(Ordering::Relaxed) {
pb.finish_with_message("❌ Operation interrupted by user.");
break;
}

match receiver.try_recv() {
Ok(result) => {
if result {
Expand All @@ -42,13 +52,16 @@ fn loading_animation(receiver: Receiver<bool>) {
}
break;
}

Err(_) => {
pb.inc(1);
thread::sleep(Duration::from_millis(100));
if SIGNAL_RECEIVED.load(Ordering::Relaxed) {
pb.finish_with_message("❌ Operation interrupted by user.");
break;
}
thread::sleep(Duration::from_millis(10));
}
}
}
pb.abandon(); // Ensure the progress bar is cleaned up
}

fn print_verification_status(
Expand Down Expand Up @@ -163,16 +176,28 @@ pub async fn handle_submission_response(
let request_id = status_response.request_id;
println!("Verification request sent with request id: {}", request_id);
println!("Verification in progress... ⏳");

// Span new thread for polling the server for status
// Create a channel for communication between threads
let (sender, receiver) = unbounded();

let handle = thread::spawn(move || loading_animation(receiver));
// Poll the server for status

loop {
// Check for interrupt signal before polling
if SIGNAL_RECEIVED.load(Ordering::Relaxed) {
let _ = sender.send(false);
handle.join().unwrap();
break; // Exit the loop and continue with normal error handling
}

let status = check_job_status(&client, &request_id).await?;
match status.status {
JobStatus::InProgress => {
if SIGNAL_RECEIVED.load(Ordering::Relaxed) {
let _ = sender.send(false);
handle.join().unwrap();
break;
}
thread::sleep(Duration::from_secs(10));
}
JobStatus::Completed => {
Expand All @@ -197,7 +222,6 @@ pub async fn handle_submission_response(
}
JobStatus::Failed => {
let _ = sender.send(false);

handle.join().unwrap();
let status_response: JobVerificationResponse = status.respose.unwrap();
println!("Program {} has not been verified. ❌", program_id);
Expand Down
Loading
Loading