Retry failed outgoing webhook events safely and quietly.
<?php
add_action( 'groundhogg/event/failed', 'gh_retry_failed_post_request_events', 10, 2 );
const GH_FAILED_REQUEST_MAX_RETRIES = 3;
const GH_FAILED_REQUEST_RETRY_DELAY = 60 * 3; //3 minutes
/**
* Hooks in after an event fails and if the error code is http_request_failed retry the event in a few minutes
*
* @param \Groundhogg\Event $event
*
* @return void
*/
function gh_retry_failed_post_request_events( \Groundhogg\Event $event, WP_Error $error ) {
// not a failed request
if ( $error->get_error_code() !== 'http_request_failed' ) {
return;
}
$retries = $event->get_arg( 'http_retries', 0 );
if ( $retries >= GH_FAILED_REQUEST_MAX_RETRIES ) {
return;
}
$event->update([
'status' => \Groundhogg\Event::WAITING,
'time' => time() + GH_FAILED_REQUEST_RETRY_DELAY,
]);
$event->set_args( [ 'http_retries' => $retries + 1 ] );
}
Was this helpful?
Let us know if this document answered your question. That’s the only way we can improve.
