This commit is contained in:
hashbro
2026-09-16 05:35:38 +08:00
parent 26bccaf2b9
commit 7f2fc33449
7 changed files with 174 additions and 10 deletions
+34 -8
View File
@@ -3,16 +3,19 @@
namespace App\Jobs;
use App\Services\TelegramNotifier;
use App\Support\SafeLog;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Queue\Queueable;
use RuntimeException;
use Throwable;
class SendTelegramMessage implements ShouldQueue
{
use Queueable;
public int $tries = 3;
public int $tries = 4;
public int $timeout = 20;
public int $timeout = 25;
public function __construct(
public string $chatId,
@@ -20,15 +23,38 @@ class SendTelegramMessage implements ShouldQueue
public ?string $token = null,
) {}
/** @return list<int> */
public function backoff(): array
{
return [10, 60, 300];
}
public function handle(TelegramNotifier $telegram): void
{
$result = $telegram->sendToChat($this->chatId, $this->text, $this->token);
if (! $result['ok']) {
\App\Support\SafeLog::channel('telegram')->warning('SendTelegramMessage job failed', [
'chat_id' => $this->chatId,
'error' => $result['error'] ?? 'unknown',
'text_preview' => mb_substr($this->text, 0, 80),
]);
if ($result['ok']) {
return;
}
$error = (string) ($result['error'] ?? 'unknown');
SafeLog::channel('telegram')->warning('SendTelegramMessage job failed', [
'chat_id' => $this->chatId,
'error' => $error,
'text_preview' => mb_substr($this->text, 0, 80),
'retryable' => TelegramNotifier::isRetryableSendError($error),
]);
if (TelegramNotifier::isRetryableSendError($error)) {
throw new RuntimeException('telegram send failed: '.$error);
}
}
public function failed(?Throwable $e): void
{
SafeLog::channel('telegram')->error('SendTelegramMessage given up after retries', [
'chat_id' => $this->chatId,
'error' => $e?->getMessage(),
'text_preview' => mb_substr($this->text, 0, 80),
]);
}
}
+30
View File
@@ -184,6 +184,36 @@ class TelegramNotifier
}
}
/**
* Timeouts, transport errors, 429 and 5xx are worth another queue attempt.
* Chat-not-found / kicked / unauthorized are permanent.
*/
public static function isRetryableSendError(?string $error): bool
{
$error = trim((string) $error);
if ($error === '') {
return true;
}
$e = strtolower($error);
if (str_contains($e, 'too many requests') || str_contains($e, 'retry after') || str_contains($e, '429')) {
return true;
}
if (preg_match('/\bhttp\s*5\d\d\b/', $e) === 1) {
return true;
}
if (str_contains($e, 'curl error')
|| str_contains($e, 'timed out')
|| str_contains($e, 'timeout')
|| str_contains($e, 'connection')
|| str_contains($e, 'resolve host')
|| str_contains($e, 'ssl')) {
return true;
}
return false;
}
/**
* @return array{ok: bool, error: ?string}
*/