Skip to content
Open
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
6 changes: 5 additions & 1 deletion app/Events/SendMessageEvent.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,21 @@ class SendMessageEvent
public $host;
public $channelSubscribe;
public $channelPriority;
public $channelName;
public $subscriberName;

/**
* Create a new event instance.
*
* @return void
*/
public function __construct(Message $message, $host, $channelSubscribe, $channelPriority)
public function __construct(Message $message, $host, $channelSubscribe, $channelPriority, $channelName, $subscriberName)
{
$this->message = $message;
$this->host = $host;
$this->channelSubscribe = $channelSubscribe;
$this->channelPriority = $channelPriority;
$this->channelName = $channelName;
$this->subscriberName = $subscriberName;
}
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/ChannelController.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ private function sendMessageTo($request, $channel)
foreach ($channel->subscribers as $subscriber) {
$relations = $this->channelSubscribeRepository->where($channel->id, $subscriber->id);
foreach ($relations as $relation) {
event(new SendMessageEvent($message, $subscriber->host, $relation, $channel->priority));
event(new SendMessageEvent($message, $subscriber->host, $relation, $channel->priority, $channel?->name ?? "", $subscriber->name));
}
}
return response()->json([
Expand All @@ -498,6 +498,7 @@ private function getCachedChannelByName($channelName)

$cacheData = (object)[
"id" => $channel->id,
"name" => $channel->name,
"subscribers" => [],
"priority" => $channel->priority
];
Expand Down
44 changes: 32 additions & 12 deletions app/Jobs/SendMessageJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;

use Illuminate\Support\Facades\Log;

class SendMessageJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

private $event;
private $body;

private $startTime;

/**
* The number of times the job may be attempted.
Expand Down Expand Up @@ -67,16 +67,36 @@ public function __construct(SendMessageEvent $event)
*/
public function handle(GuzzleService $guzzleService)
{
switch ($this->event->channelSubscribe->authentication) {
case Authentication::BASIC:
$guzzleService->sendWithBasicAuth($this->event);
break;
case Authentication::NONE:
$guzzleService->sendWithoutAuth($this->event);
break;
case Authentication::OAUTH2:
$guzzleService->sendWithOAuth2($this->event);
break;
try {
Log::withContext([
"subscriber" => $this->event->subscriberName,
"channel" => $this->event->channelName,
"priority" => $this->event->channelSubscribe->channelPriority,
"authentication" => $this->event->channelSubscribe->authentication,
]);

$this->startTime = microtime(true);
switch ($this->event->channelSubscribe->authentication) {
case Authentication::BASIC:
$guzzleService->sendWithBasicAuth($this->event);
break;
case Authentication::NONE:
$guzzleService->sendWithoutAuth($this->event);
break;
case Authentication::OAUTH2:
$guzzleService->sendWithOAuth2($this->event);
break;
}
Log::info("Message sent successfully", [
"time" => microtime(true) - $this->startTime,
]);
} catch (\Exception $e) {
Log::error('Message sending failed', [
"time" => microtime(true) - $this->startTime,
"attempts" => $this->attempts(),
"exception" => $e->getMessage(),
]);
throw $e;
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Feature/FailedJobTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ private function getJsonFragment(?FailedJob $failedJob = null, ?ChannelSubscribe
public function testSuccesfullyListFailedJob()
{
$channelSubscribe = factory(ChannelSubscribe::class)->create();
$sendMessageJob = new SendMessageJob(new SendMessageEvent(new Message('messaggio'), 'host', $channelSubscribe, 'highpriority'));
$sendMessageJob = new SendMessageJob(new SendMessageEvent(new Message('messaggio'), 'host', $channelSubscribe, 'highpriority', 'channelName', 'publisherName'));

$failedJob = factory(FailedJob::class)->make([
'payload' => json_encode(["data" => ["command" => serialize($sendMessageJob)]]),
Expand Down
Loading