trove/app/Services/PollAlertService.php

38 lines
1 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Services;
use Exception;
use Illuminate\Support\Facades\Http;
use Lvl0\FediDiscover\Models\Instance;
class PollAlertService
{
public function recordFailure(Instance $instance, string $message): void
{
$instance->increment('consecutive_poll_failures');
$instance->refresh();
$ntfyUrl = config('services.ntfy.url');
$ntfyThreshold = (int) config('services.ntfy.threshold');
$ntfyTopic = config('services.ntfy.topic');
if ($ntfyUrl === null || $ntfyThreshold === 0 || $ntfyTopic === null) {
return;
}
if ($instance->consecutive_poll_failures < $ntfyThreshold) {
return;
}
try {
Http::timeout(5)
->withBody($instance->url . ' - ' . $message, 'text/plain')
->post(rtrim($ntfyUrl, '/') . '/' . $ntfyTopic);
} catch (Exception $e) {
logger()->warning('ntfy alert failed', ['instance' => $instance->url, 'error' => $e->getMessage()]);
}
}
}