Selaa lähdekoodia

处理 405 以外的异常

luolongfei 1 vuosi sitten
vanhempi
sitoutus
89f6fe0bbd
3 muutettua tiedostoa jossa 31 lisäystä ja 10 poistoa
  1. 29 10
      app/helpers.php
  2. 1 0
      resources/lang/en.php
  3. 1 0
      resources/lang/zh.php

+ 29 - 10
app/helpers.php

@@ -1933,27 +1933,46 @@ if (!function_exists('autoRetry')) {
     function autoRetry($func, $maxRetryCount = 3, $params = [])
     {
         $retryCount = 0;
-        while ($retryCount <= $maxRetryCount) {
+        while (true) {
             try {
                 return call_user_func_array($func, $params);
             } catch (\Exception $e) {
-                if (stripos($e->getMessage(), '405') === false) {
-                    break;
-                }
-
                 $retryCount++;
                 if ($retryCount > $maxRetryCount) {
                     throw $e;
                 }
 
-                $sleepTime = $retryCount * 4;
-                if ($sleepTime < 20) { // 最小休眠 20 秒
-                    $sleepTime = 20;
+                $sleepTime = getSleepTime($retryCount);
+
+                if (stripos($e->getMessage(), '405') !== false) {
+                    system_log(sprintf(lang('exception_msg.34520015'), $sleepTime, $maxRetryCount, $retryCount, $maxRetryCount));
+                } else {
+                    system_log(sprintf(lang('exception_msg.34520016'), $e->getMessage(), $sleepTime, $maxRetryCount, $retryCount, $maxRetryCount));
                 }
-                system_log(sprintf(lang('exception_msg.34520015'), $sleepTime, $maxRetryCount, $retryCount, $maxRetryCount));
 
                 sleep($sleepTime);
             }
         }
     }
-}
+}
+
+if (!function_exists('getSleepTime')) {
+    /**
+     * 获取睡眠秒数
+     *
+     * @param int $i
+     * @param int $magRatio
+     * @param int $minSleepTime
+     *
+     * @return int
+     */
+    function getSleepTime($i, $magRatio = 4, $minSleepTime = 20)
+    {
+        $sleepTime = $i * $magRatio;
+        if ($sleepTime < $minSleepTime) { // 最小休眠 $minSleepTime 秒
+            return $minSleepTime;
+        }
+
+        return $sleepTime;
+    }
+}

+ 1 - 0
resources/lang/en.php

@@ -23,6 +23,7 @@ return [
         '34520013' => 'Error getting domain status, error message: %s',
         '34520014' => 'No domain name found under your account %s (%s)',
         '34520015' => 'The request has been intercepted for human verification and will be retried in %d seconds, with a maximum of %d retries. [%d/%d]',
+        '34520016' => 'Request failed: %s, will retry in %d seconds, up to %d times. [%d/%d]',
     ],
     'messages' => [
         '100001' => 'The cookie value named WHMCSZH5eHTGhfvzP could not be obtained, so this login is not valid, please check if your account or password is correct',

+ 1 - 0
resources/lang/zh.php

@@ -23,6 +23,7 @@ return [
         '34520013' => '获取域名状态页面出错,错误信息:%s',
         '34520014' => '你的账户 %s 名下没有发现域名,可能不存在域名。(%s)',
         '34520015' => '请求被人机验证拦截,将在 %d 秒后重试,最多重试 %d 次。[%d/%d]',
+        '34520016' => '请求失败,原因为 %s,将在 %d 秒后重试,最多重试 %d 次。[%d/%d]',
     ],
     'messages' => [
         '100001' => '未能取得名为 WHMCSZH5eHTGhfvzP 的 cookie 值,故本次登录无效,请检查你的账户或密码是否正确。',