From 912d0d0cdeb744eb6f13e4a03ec14fa650cc7b9b Mon Sep 17 00:00:00 2001 From: Jon Baker Date: Fri, 7 Aug 2020 11:04:27 -0500 Subject: [PATCH] Enable selection of what queue the tracking events are dispatched to --- README.md | 2 +- config/mail-tracker.php | 7 ++++++- src/MailTrackerController.php | 6 ++++-- src/SNSController.php | 9 ++++++--- tests/MailTrackerTest.php | 26 ++++++++++++++++++-------- 5 files changed, 35 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 9f1babc..aba1bd7 100644 --- a/README.md +++ b/README.md @@ -87,7 +87,7 @@ When you are in a dev environment (i.e. using the `.test` domain with Valet, or ## Events -When an email is sent, viewed, or a link is clicked, its tracking information is counted in the database using the jdavidbakr\MailTracker\Model\SentEmail model. This processing is done via dispatched jobs to the default queue in order to prevent the database from being overwhelmed is an email blast situation. +When an email is sent, viewed, or a link is clicked, its tracking information is counted in the database using the jdavidbakr\MailTracker\Model\SentEmail model. This processing is done via dispatched jobs to the queue in order to prevent the database from being overwhelmed in an email blast situation. You may choose the queue that these events are dispatched via the `mail-tracker.tracker-queue` config setting, or leave it `null` to use the default queue. You may want to do additional processing on these events, so an event is fired in these cases: diff --git a/config/mail-tracker.php b/config/mail-tracker.php index de43686..365ad86 100644 --- a/config/mail-tracker.php +++ b/config/mail-tracker.php @@ -71,6 +71,11 @@ /** * Determines whether or not the body of the email is logged in the sent_emails table */ - 'log-content' => true + 'log-content' => true, + + /** + * What queue should we dispatch our tracking jobs to? Null will use the default queue. + */ + 'tracker-queue' => null, ]; diff --git a/src/MailTrackerController.php b/src/MailTrackerController.php index 1d30028..4cc4f93 100644 --- a/src/MailTrackerController.php +++ b/src/MailTrackerController.php @@ -31,7 +31,8 @@ public function getT($hash) $tracker = Model\SentEmail::where('hash', $hash) ->first(); if ($tracker) { - RecordTrackingJob::dispatch($tracker); + RecordTrackingJob::dispatch($tracker) + ->onQueue(config('mail-tracker.tracker-queue')); } return $response; @@ -58,7 +59,8 @@ protected function linkClicked($url, $hash) $tracker = Model\SentEmail::where('hash', $hash) ->first(); if ($tracker) { - RecordLinkClickJob::dispatch($tracker, $url); + RecordLinkClickJob::dispatch($tracker, $url) + ->onQueue(config('mail-tracker.tracker-queue')); return redirect($url); } diff --git a/src/SNSController.php b/src/SNSController.php index d218239..2869172 100644 --- a/src/SNSController.php +++ b/src/SNSController.php @@ -70,16 +70,19 @@ protected function process_notification($message) protected function process_delivery($message) { - RecordDeliveryJob::dispatch($message); + RecordDeliveryJob::dispatch($message) + ->onQueue(config('mail-tracker.tracker-queue')); } public function process_bounce($message) { - RecordBounceJob::dispatch($message); + RecordBounceJob::dispatch($message) + ->onQueue(config('mail-tracker.tracker-queue')); } public function process_complaint($message) { - RecordComplaintJob::dispatch($message); + RecordComplaintJob::dispatch($message) + ->onQueue(config('mail-tracker.tracker-queue')); } } diff --git a/tests/MailTrackerTest.php b/tests/MailTrackerTest.php index b0874f9..0192a4a 100644 --- a/tests/MailTrackerTest.php +++ b/tests/MailTrackerTest.php @@ -191,6 +191,7 @@ public function it_doesnt_track_if_told_not_to() */ public function testPing() { + Config::set('mail-tracker.tracker-queue', 'alt-queue'); Bus::fake(); $track = \jdavidbakr\MailTracker\Model\SentEmail::create([ 'hash' => Str::random(32), @@ -203,12 +204,14 @@ public function testPing() $response->assertSuccessful(); Bus::assertDispatched(RecordTrackingJob::class, function ($e) use ($track) { - return $e->sentEmail->id == $track->id; + return $e->sentEmail->id == $track->id && + $e->queue == 'alt-queue'; }); } public function testLegacyLink() { + Config::set('mail-tracker.tracker-queue', 'alt-queue'); Bus::fake(); $track = \jdavidbakr\MailTracker\Model\SentEmail::create([ 'hash' => Str::random(32), @@ -225,12 +228,14 @@ public function testLegacyLink() $response->assertRedirect($redirect); Bus::assertDispatched(RecordLinkClickJob::class, function ($job) use ($track, $redirect) { return $job->sentEmail->id == $track->id && - $job->url == $redirect; + $job->url == $redirect && + $job->queue == 'alt-queue'; }); } public function testLink() { + Config::set('mail-tracker.tracker-queue', 'alt-queue'); Bus::fake(); $track = \jdavidbakr\MailTracker\Model\SentEmail::create([ 'hash' => Str::random(32), @@ -246,7 +251,8 @@ public function testLink() $response->assertRedirect($redirect); Bus::assertDispatched(RecordLinkClickJob::class, function ($job) use ($track, $redirect) { return $job->sentEmail->id == $track->id && - $job->url == $redirect; + $job->url == $redirect && + $job->queue == 'alt-queue'; }); } @@ -514,7 +520,7 @@ public function it_ignores_invalid_topic() */ public function it_processes_a_delivery() { - $this->disableExceptionHandling(); + Config::set('mail-tracker.tracker-queue', 'alt-queue'); Bus::fake(); $message = [ 'notificationType' => 'Delivery', @@ -535,7 +541,8 @@ public function it_processes_a_delivery() $response->assertSee('notification processed'); Bus::assertDispatched(RecordDeliveryJob::class, function ($job) use ($message) { - return $job->message == (object)$message; + return $job->message == (object)$message && + $job->queue == 'alt-queue'; }); } @@ -544,6 +551,7 @@ public function it_processes_a_delivery() */ public function it_processes_a_bounce() { + Config::set('mail-tracker.tracker-queue', 'alt-queue'); Bus::fake(); $message = [ 'notificationType' => 'Bounce', @@ -564,7 +572,8 @@ public function it_processes_a_bounce() $response->assertSee('notification processed'); Bus::assertDispatched(RecordBounceJob::class, function ($job) use ($message) { - return $job->message == (object)$message; + return $job->message == (object)$message && + $job->queue == 'alt-queue'; }); } @@ -573,7 +582,7 @@ public function it_processes_a_bounce() */ public function it_processes_a_complaint() { - $this->disableExceptionHandling(); + Config::set('mail-tracker.tracker-queue', 'alt-queue'); Bus::fake(); $message = [ 'notificationType' => 'Complaint', @@ -594,7 +603,8 @@ public function it_processes_a_complaint() $response->assertSee('notification processed'); Bus::assertDispatched(RecordComplaintJob::class, function ($job) use ($message) { - return $job->message == (object)$message; + return $job->message == (object)$message && + $job->queue == 'alt-queue'; }); }