coolify/app/Jobs/SendMessageToPushoverJob.php

52 lines
1.4 KiB
PHP
Raw Normal View History

2024-12-11 14:54:11 +00:00
<?php
namespace App\Jobs;
use App\Notifications\Dto\PushoverMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
2025-01-07 13:52:08 +00:00
use RuntimeException;
2024-12-11 14:54:11 +00:00
class SendMessageToPushoverJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The number of times the job may be attempted.
*
* @var int
*/
public $tries = 5;
public $backoff = 10;
/**
* The maximum number of unhandled exceptions to allow before failing.
*/
public int $maxExceptions = 5;
public function __construct(
2025-01-07 13:52:08 +00:00
public PushoverMessage $pushoverMessage,
2024-12-11 14:54:11 +00:00
public string $token,
public string $user,
) {
$this->onQueue('high');
}
/**
* Execute the job.
*/
public function handle(): void
{
2025-01-07 13:52:08 +00:00
$response = Http::post('https://api.pushover.net/1/messages.json', $this->pushoverMessage->toPayload($this->token, $this->user));
2024-12-11 14:54:11 +00:00
if ($response->failed()) {
2025-01-07 13:52:08 +00:00
throw new RuntimeException('Pushover notification failed with '.$response->status().' status code.'.$response->body());
2024-12-11 14:54:11 +00:00
}
}
}