Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve notification jobs #4629

Draft
wants to merge 8 commits into
base: next
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Notifications\Channels;
namespace App\Interfaces\Notifications;

interface SendsDiscord
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Notifications\Channels;
namespace App\Interfaces\Notifications;

interface SendsEmail
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Notifications\Channels;
namespace App\Interfaces\Notifications;

interface SendsPushover
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Notifications\Channels;
namespace App\Interfaces\Notifications;

interface SendsSlack
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Notifications\Channels;
namespace App\Interfaces\Notifications;

interface SendsTelegram
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,39 @@
<?php

namespace App\Jobs;
namespace App\Jobs\Notifications;

use App\Notifications\Dto\DiscordMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Support\Facades\Http;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use App\Notifications\Dto\DiscordMessage;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;

class SendMessageToDiscordJob 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 $tries = 5;
public int $backoff = 10;
public int $maxExceptions = 5;

public function __construct(
public DiscordMessage $message,
public string $webhookUrl
private readonly DiscordMessage $message,
private readonly string $webhookUrl
) {
$this->onQueue('high');
}

/**
* Execute the job.
*/
public function handle(): void
{
Http::post($this->webhookUrl, $this->message->toPayload());
$response = Http::timeout(15)->post($this->webhookUrl, $this->message->toPayload());

if (! $response->successful()) {
throw new \RuntimeException(
"Discord webhook failed with status {$response->status()}: {$response->body()}"
);
}
}
}
48 changes: 48 additions & 0 deletions app/Jobs/Notifications/SendMessageToEmailJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace App\Jobs\Notifications;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Mail;

class SendMessageToEmailJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public int $tries = 5;

public int $backoff = 10;

public int $maxExceptions = 5;

public int $timeout = 30;

public function __construct(
private readonly MailMessage $message,
private readonly array $recipients
) {
$this->onQueue('high');
}

public function handle(): void
{
try {
Mail::html(
(string) $this->message->render(),
fn ($message) => $message
->to($this->recipients)
->subject($this->message->subject)
);
} catch (\Throwable $e) {
throw new \RuntimeException(
"Failed to send email the following error occurred: {$e->getMessage()}"
);
}
}
}
42 changes: 42 additions & 0 deletions app/Jobs/Notifications/SendMessageToPushoverJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace App\Jobs\Notifications;

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;

class SendMessageToPushoverJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public int $tries = 5;

public int $backoff = 10;

public int $maxExceptions = 5;

public function __construct(
private readonly PushoverMessage $message,
private readonly string $token,
private readonly string $user,
) {
$this->onQueue('high');
}

public function handle(): void
{
$response = Http::timeout(15)->post('https://api.pushover.net/1/messages.json', $this->message->toPayload($this->token, $this->user));

if (! $response->successful()) {
throw new \RuntimeException(
"Pushover notification failed with status {$response->status()}: {$response->body()}"
);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
<?php

namespace App\Jobs;
namespace App\Jobs\Notifications;

use App\Notifications\Dto\SlackMessage;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Http;
use Illuminate\Queue\SerializesModels;
use App\Notifications\Dto\SlackMessage;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Http;
use Illuminate\Contracts\Queue\ShouldBeEncrypted;

class SendMessageToSlackJob implements ShouldQueue
class SendMessageToSlackJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public int $tries = 5;
public int $backoff = 10;
public int $maxExceptions = 5;

public function __construct(
private SlackMessage $message,
private string $webhookUrl
private readonly SlackMessage $message,
private readonly string $webhookUrl
) {
$this->onQueue('high');
}

public function handle(): void
{
Http::post($this->webhookUrl, [
$response = Http::timeout(15)->post($this->webhookUrl, [
'blocks' => [
[
'type' => 'section',
Expand Down Expand Up @@ -55,5 +60,11 @@ public function handle(): void
],
],
]);

if (! $response->successful()) {
throw new \RuntimeException(
"Slack webhook failed with status {$response->status()}: {$response->body()}"
);
}
}
}
77 changes: 77 additions & 0 deletions app/Jobs/Notifications/SendMessageToTelegramJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<?php

namespace App\Jobs\Notifications;

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;
use Illuminate\Support\Str;

class SendMessageToTelegramJob implements ShouldBeEncrypted, ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

public int $tries = 5;

public int $backoff = 10;

public int $maxExceptions = 5;

public function __construct(
private readonly string $text,
private readonly array $buttons,
private readonly string $token,
private readonly string $chatId,
private readonly ?string $threadId = null,
) {
$this->onQueue('high');
}

public function handle(): void
{
$inlineButtons = [];
foreach ($this->buttons as $button) {
$buttonUrl = data_get($button, 'url');
$text = data_get($button, 'text', 'Click here');

if ($buttonUrl && Str::contains($buttonUrl, 'http://localhost')) {
$buttonUrl = str_replace('http://localhost', config('app.url'), $buttonUrl);
}

$inlineButtons[] = [
'text' => $text,
'url' => $buttonUrl,
];
}

$payload = [
'chat_id' => $this->chatId,
'text' => $this->text,
];

if (! empty($this->buttons)) {
$payload['reply_markup'] = json_encode([
'inline_keyboard' => [
$inlineButtons,
],
]);
}

if ($this->threadId) {
$payload['message_thread_id'] = $this->threadId;
}

$response = Http::timeout(15)
->post('https://api.telegram.org/bot'.$this->token.'/sendMessage', $payload);

if (! $response->successful()) {
throw new \RuntimeException(
"Telegram webhook failed with status {$response->status()}: {$response->body()}"
);
}
}
}
32 changes: 12 additions & 20 deletions app/Jobs/SendMessageToPushoverJob.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php

namespace App\Jobs;
namespace App\Jobs\Notifications;

use App\Notifications\Dto\PushoverMessage;
use Illuminate\Bus\Queueable;
Expand All @@ -15,36 +15,28 @@ 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 int $tries = 5;

public $backoff = 10;
public int $backoff = 10;

/**
* The maximum number of unhandled exceptions to allow before failing.
*/
public int $maxExceptions = 5;

public function __construct(
public PushoverMessage $message,
public string $token,
public string $user,
private readonly PushoverMessage $message,
private readonly string $token,
private readonly string $user,
) {
$this->onQueue('high');
}

/**
* Execute the job.
*/
public function handle(): void
{
$response = Http::post('https://api.pushover.net/1/messages.json', $this->message->toPayload($this->token, $this->user));
if ($response->failed()) {
throw new \RuntimeException('Pushover notification failed with '.$response->status().' status code.'.$response->body());
$response = Http::timeout(15)->post('https://api.pushover.net/1/messages.json', $this->message->toPayload($this->token, $this->user));

if (! $response->successful()) {
throw new \RuntimeException(
"Pushover notification failed with status {$response->status()}: {$response->body()}"
);
}
}
}
Loading