38 lines
1.1 KiB
PHP
38 lines
1.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 = config('services.ntfy.threshold');
|
|
$ntfyTopic = config('services.ntfy.topic');
|
|
|
|
if ($ntfyUrl === null || $ntfyThreshold === 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()]);
|
|
}
|
|
}
|
|
}
|