Skip to content
This repository has been archived by the owner on Jul 12, 2022. It is now read-only.

Commit

Permalink
Use Redis::trottle to only dispatch one notification at a time
Browse files Browse the repository at this point in the history
  • Loading branch information
herpaderpaldent committed Jan 1, 2019
1 parent 398f423 commit 23c014d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 30 deletions.
3 changes: 2 additions & 1 deletion src/Notifications/BaseNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,13 @@
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Notification;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Seat\Web\Models\Group;

abstract class BaseNotification extends Notification implements ShouldQueue
{
use Queueable, SerializesModels;
use Queueable, SerializesModels, InteractsWithQueue;

/**
* @var array
Expand Down
54 changes: 34 additions & 20 deletions src/Notifications/RefreshTokenDeletedNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use Herpaderpaldent\Seat\SeatNotifications\Channels\Discord\DiscordMessage;
use Herpaderpaldent\Seat\SeatNotifications\Channels\Slack\SlackChannel;
use Herpaderpaldent\Seat\SeatNotifications\Channels\Slack\SlackMessage;
use Illuminate\Support\Facades\Redis;
use Seat\Eveapi\Models\Corporation\CorporationInfo;
use Seat\Eveapi\Models\RefreshToken;

Expand Down Expand Up @@ -48,26 +49,39 @@ public function __construct(RefreshToken $refresh_token)
*/
public function via($notifiable)
{
switch($notifiable->via) {
case 'discord':
$this->tags = [
'refresh_token',
'discord',
$notifiable->type === 'private' ? $notifiable->recipient() : 'channel',
];

return [DiscordChannel::class];
break;
case 'slack':
$this->tags = [
'refresh_token',
'slack',
$notifiable->type === 'private' ? $notifiable->recipient() : 'channel',
];

return [SlackChannel::class];
break;
}
Redis::funnel('seatnotification:channel_id' . $notifiable->channel_id)->limit(1)->then(function () use ($notifiable) {
switch($notifiable->via) {
case 'discord':
$this->tags = [
'refresh_token',
'discord',
$notifiable->type === 'private' ? $notifiable->recipient() : 'channel',
];

return [DiscordChannel::class];
break;
case 'slack':
$this->tags = [
'refresh_token',
'slack',
$notifiable->type === 'private' ? $notifiable->recipient() : 'channel',
];

return [SlackChannel::class];
break;
default:
return [''];
}
}, function () use ($notifiable) {

logger()->warning(' A notification on ' . $notifiable->via .
' for channel ' . $notifiable->channel_id .
' has already been dispateched. Removing the job from the queue');

$this->delete();
});

return [''];
}

public function toDiscord($notifiable)
Expand Down
22 changes: 13 additions & 9 deletions src/Observers/RefreshTokenObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,25 @@
use Herpaderpaldent\Seat\SeatNotifications\Notifications\RefreshTokenDeletedNotification;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Redis;
use Seat\Eveapi\Models\RefreshToken;

class RefreshTokenObserver
{
public function deleting(RefreshToken $refresh_token)
{
Log::info('SoftDelete detected of ' . $refresh_token->user->name);

$receipients = RefreshTokenNotification::all()
->filter(function ($recepient) {
return $recepient->shouldReceive();
});

Notification::send($receipients, (new RefreshTokenDeletedNotification($refresh_token)));

Redis::funnel('soft_delete:refresh_token_' . $refresh_token->user->name)->limit(1)->then(function () use ($refresh_token) {
Log::info('SoftDelete detected of ' . $refresh_token->user->name);

$receipients = RefreshTokenNotification::all()
->filter(function ($recepient) {
return $recepient->shouldReceive();
});

Notification::send($receipients, (new RefreshTokenDeletedNotification($refresh_token)));
}, function () use ($refresh_token) {
Log::info('A Soft-Delete job is already running for ' . $refresh_token->user->name);
});
}

public function test()
Expand Down

0 comments on commit 23c014d

Please sign in to comment.