我正在尝试用woocommerce实现webhook重试。我看到他们在哪里发布了webhook通知https://github.com/woocommerce/woocommerce/blob/5ba2cdafa58c5a02d44eebbab1e8fe26f295e4aa/includes/class-wc-webhook.php#L353, 但没有提及$this
. 是否可以通过id获取webhook对象?然后我可以将其保存在内存中,并在延迟一段时间后重试,和/或将某些内容放入数据库以供重试,等等。
老实说,这应该都是内置的,但我希望能够处理服务器漏洞等,并确保我的订阅web挂钩最终在没有手动干预的情况下完成。
编辑
我想出了这个代码,我希望得到任何反馈:
<?php
/*
Plugin Name: Custom Stuff
Description: Custom Functions, etc.
*/
// make sure woocommerce never disables a webhook
function overrule_webhook_disable_limit($number)
{
return 999999999999; //very high number hopefully you\'ll never reach.
}
add_filter(\'woocommerce_max_webhook_delivery_failures\', \'overrule_webhook_disable_limit\');
// woocommerce webhook retry queue
function woocommerce_webhook_retry($id, $arg)
{
do_action(\'woocommerce_deliver_webhook_async\', $id, $arg);
}
// add listener for woocommerce web hooks
function woocommerce_webhook_listener_custom($http_args, $response, $duration, $arg, $id)
{
$responseCode = wp_remote_retrieve_response_code($response);
if ($responseCode < 200 || $responseCode > 299)
{
// re-queue web-hook for another attempt
$timestamp = new DateTime(\'+1 minutes\');
WC()->queue()->schedule_single($timestamp, \'woocommerce_webhook_retry\', $args = array($id, $arg), $group = \'wc_webhook_retry\');
}
}
add_action(\'woocommerce_webhook_delivery\', \'woocommerce_webhook_listener_custom\', 10, 5);
SO网友:jjxtra
这似乎做到了,它每5分钟重试一次,并使用内置的woocommerce操作名称。
<?php
/*
Plugin Name: Custom Stuff
Description: Custom Functions, etc.
*/
// make sure woocommerce never disables a webhook
function overrule_webhook_disable_limit($number)
{
return 999999999999; //very high number hopefully you\'ll never reach.
}
add_filter(\'woocommerce_max_webhook_delivery_failures\', \'overrule_webhook_disable_limit\');
// add listener for woocommerce web hooks
function woocommerce_webhook_listener_custom($http_args, $response, $duration, $arg, $id)
{
$responseCode = wp_remote_retrieve_response_code($response);
if ($responseCode < 200 || $responseCode > 299)
{
// re-queue web-hook for another attempt, retry every 5 minutes until success
$timestamp = new DateTime(\'+5 minutes\');
$argsArray = array(\'webhook_id\' => $id, \'arg\' => $arg);
WC()->queue()->schedule_single($timestamp, \'woocommerce_deliver_webhook_async\', $args = $argsArray, $group = \'woocommerce-webhooks\');
}
}
add_action(\'woocommerce_webhook_delivery\', \'woocommerce_webhook_listener_custom\', 10, 5);