mirror of
https://github.com/alexgo-io/bitcoin-indexer.git
synced 2026-01-12 22:43:06 +08:00
feat: add retry
This commit is contained in:
@@ -213,7 +213,7 @@ pub async fn scan_bitcoin_chainstate_via_http_using_predicate(
|
||||
|
||||
info!(
|
||||
ctx.expect_logger(),
|
||||
"Processing block #{} through {} predicate {}",
|
||||
"Processing block #{} through {} predicate (inscriptions revealed: [{}])",
|
||||
cursor,
|
||||
predicate_spec.uuid,
|
||||
inscriptions_revealed.join(", ")
|
||||
@@ -325,7 +325,7 @@ pub async fn execute_predicates_action<'a>(
|
||||
actions_triggered += 1;
|
||||
match action {
|
||||
BitcoinChainhookOccurrence::Http(request) => {
|
||||
send_request(request, &ctx).await?
|
||||
send_request(request, 3, 1, &ctx).await?
|
||||
}
|
||||
BitcoinChainhookOccurrence::File(path, bytes) => {
|
||||
file_append(path, bytes, &ctx)?
|
||||
|
||||
@@ -170,7 +170,7 @@ pub async fn scan_stacks_chainstate_via_csv_using_predicate(
|
||||
Ok(action) => {
|
||||
actions_triggered += 1;
|
||||
let res = match action {
|
||||
StacksChainhookOccurrence::Http(request) => send_request(request, &ctx).await,
|
||||
StacksChainhookOccurrence::Http(request) => send_request(request, 3, 1, &ctx).await,
|
||||
StacksChainhookOccurrence::File(path, bytes) => file_append(path, bytes, &ctx),
|
||||
StacksChainhookOccurrence::Data(_payload) => unreachable!(),
|
||||
};
|
||||
|
||||
@@ -1025,7 +1025,7 @@ pub async fn start_observer_commands_handler(
|
||||
}
|
||||
|
||||
for request in requests.into_iter() {
|
||||
let _ = send_request(request, &ctx).await;
|
||||
let _ = send_request(request, 3, 1, &ctx).await;
|
||||
}
|
||||
|
||||
if let Some(ref tx) = observer_events_tx {
|
||||
@@ -1158,7 +1158,7 @@ pub async fn start_observer_commands_handler(
|
||||
request
|
||||
)
|
||||
});
|
||||
let _ = send_request(request, &ctx).await;
|
||||
let _ = send_request(request, 3, 1, &ctx).await;
|
||||
}
|
||||
|
||||
if let Some(ref tx) = observer_events_tx {
|
||||
|
||||
@@ -139,30 +139,53 @@ impl AbstractBlock for BitcoinBlockData {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn send_request(request: RequestBuilder, ctx: &Context) -> Result<(), ()> {
|
||||
match request.send().await {
|
||||
Ok(res) => {
|
||||
if res.status().is_success() {
|
||||
ctx.try_log(|logger| slog::info!(logger, "Trigger {} successful", res.url()));
|
||||
return Ok(());
|
||||
} else {
|
||||
pub async fn send_request(
|
||||
request_builder: RequestBuilder,
|
||||
attempts_max: u16,
|
||||
attempts_interval_sec: u16,
|
||||
ctx: &Context,
|
||||
) -> Result<(), ()> {
|
||||
let mut retry = 0;
|
||||
loop {
|
||||
let request_builder = match request_builder.try_clone() {
|
||||
Some(rb) => rb,
|
||||
None => {
|
||||
ctx.try_log(|logger| slog::warn!(logger, "unable to clone request builder"));
|
||||
return Err(());
|
||||
}
|
||||
};
|
||||
match request_builder.send().await {
|
||||
Ok(res) => {
|
||||
if res.status().is_success() {
|
||||
ctx.try_log(|logger| slog::info!(logger, "Trigger {} successful", res.url()));
|
||||
return Ok(());
|
||||
} else {
|
||||
retry += 1;
|
||||
ctx.try_log(|logger| {
|
||||
slog::warn!(
|
||||
logger,
|
||||
"Trigger {} failed with status {}",
|
||||
res.url(),
|
||||
res.status()
|
||||
)
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
retry += 1;
|
||||
ctx.try_log(|logger| {
|
||||
slog::warn!(
|
||||
logger,
|
||||
"Trigger {} failed with status {}",
|
||||
res.url(),
|
||||
res.status()
|
||||
)
|
||||
slog::warn!(logger, "unable to send request {}", e.to_string())
|
||||
});
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
if retry >= attempts_max {
|
||||
ctx.try_log(|logger| {
|
||||
slog::warn!(logger, "unable to build and send request {}", e.to_string())
|
||||
slog::error!(logger, "unable to send request after several retries")
|
||||
});
|
||||
return Err(());
|
||||
}
|
||||
std::thread::sleep(std::time::Duration::from_secs(attempts_interval_sec.into()));
|
||||
}
|
||||
Err(())
|
||||
}
|
||||
|
||||
pub fn file_append(path: String, bytes: Vec<u8>, ctx: &Context) -> Result<(), ()> {
|
||||
|
||||
Reference in New Issue
Block a user