Skip to content

Commit

Permalink
feat: async event instead of rabbitmq
Browse files Browse the repository at this point in the history
  • Loading branch information
ZhongYue authored and lscar committed Mar 28, 2023
1 parent 17fd799 commit 7acfe5a
Show file tree
Hide file tree
Showing 18 changed files with 152 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use App\Models\PushDeerMessage;
use Carbon\Carbon;

class CleanOldPush extends Command
class CleanOldPushCommand extends Command
{
/**
* The name and signature of the console command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use App\Models\QueueExchangeEnum;
use Illuminate\Console\Command;

class SendNotificationApnApp extends Command
class SendNotificationApnAppCommand extends Command
{
use SendNotificationTrait;
use SendNotificationCommandTrait;

/**
* The name and signature of the console command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use App\Models\QueueExchangeEnum;
use Illuminate\Console\Command;

class SendNotificationApnClip extends Command
class SendNotificationApnClipCommand extends Command
{
use SendNotificationTrait;
use SendNotificationCommandTrait;

/**
* The name and signature of the console command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace App\Console\Commands;

use App\Services\Push\PushService;
use App\Services\Push\PushServiceInterface;
use App\Services\QueueService;
use PhpAmqpLib\Connection\AbstractConnection as AMQPConnection;
use PhpAmqpLib\Message\AMQPMessage;

trait SendNotificationTrait
trait SendNotificationCommandTrait
{
/**
* Execute the console command.
Expand All @@ -24,7 +24,7 @@ public function handle(): void
$channel->basic_consume(
queue: $this->queue->value,
callback: function (AMQPMessage $message) {
if (app(PushService::class)->sendBatch(unserialize($message->body))) {
if (app(PushServiceInterface::class)->sendBatch(unserialize($message->body))) {
$message->ack();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use App\Models\QueueExchangeEnum;
use Illuminate\Console\Command;

class SendNotificationFcmApp extends Command
class SendNotificationFcmAppCommand extends Command
{
use SendNotificationTrait;
use SendNotificationCommandTrait;

/**
* The name and signature of the console command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use App\Models\QueueExchangeEnum;
use Illuminate\Console\Command;

class SendNotificationFcmClip extends Command
class SendNotificationFcmClipCommand extends Command
{
use SendNotificationTrait;
use SendNotificationCommandTrait;

/**
* The name and signature of the console command.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use GuzzleHttp\RequestOptions;
use Illuminate\Console\Command;

class UpdateApnCertificate extends Command
class UpdateApnCertificateCommand extends Command
{
/**
* The name and signature of the console command.
Expand Down
24 changes: 12 additions & 12 deletions app/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

namespace App\Console;

use App\Console\Commands\CleanOldPush;
use App\Console\Commands\SendNotificationApnApp;
use App\Console\Commands\SendNotificationApnClip;
use App\Console\Commands\SendNotificationFcmApp;
use App\Console\Commands\SendNotificationFcmClip;
use App\Console\Commands\UpdateApnCertificate;
use App\Console\Commands\CleanOldPushCommand;
use App\Console\Commands\SendNotificationApnAppCommand;
use App\Console\Commands\SendNotificationApnClipCommand;
use App\Console\Commands\SendNotificationFcmAppCommand;
use App\Console\Commands\SendNotificationFcmClipCommand;
use App\Console\Commands\UpdateApnCertificateCommand;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Foundation\Console\Kernel as ConsoleKernel;

Expand All @@ -21,12 +21,12 @@ class Kernel extends ConsoleKernel
*/
protected function schedule(Schedule $schedule): void
{
$schedule->command(SendNotificationFcmApp::class)->everyMinute();
$schedule->command(SendNotificationFcmClip::class)->everyMinute();
$schedule->command(SendNotificationApnApp::class)->everyMinute();
$schedule->command(SendNotificationApnClip::class)->everyMinute();
$schedule->command(CleanOldPush::class)->hourly();
$schedule->command(UpdateApnCertificate::class)->daily();
// $schedule->command(SendNotificationFcmApp::class)->everyMinute();
// $schedule->command(SendNotificationFcmClip::class)->everyMinute();
// $schedule->command(SendNotificationApnApp::class)->everyMinute();
// $schedule->command(SendNotificationApnClip::class)->everyMinute();
$schedule->command(CleanOldPushCommand::class)->hourly();
$schedule->command(UpdateApnCertificateCommand::class)->daily();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@

namespace App\Events;

use App\Listeners\SendDeviceNotification;
use App\Listeners\SendNotificationListener;
use App\Models\PushDeerDevice;
use App\Models\PushDeerMessage;
use Hhxsv5\LaravelS\Swoole\Task\Event;

class DeviceNotificationProcessed extends Event
class SendNotificationEvent extends Event
{
private PushDeerDevice $device;
private PushDeerMessage $message;

protected $listeners = [
// 监听器列表
SendDeviceNotification::class,
SendNotificationListener::class,
];

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,21 @@

namespace App\Listeners;

use App\Events\DeviceNotificationProcessed;
use App\Events\SendNotificationEvent;
use App\Services\Push\ApnAppService;
use App\Services\Push\ApnClipService;
use App\Services\Push\FcmAppService;
use App\Services\Push\FcmClipService;
use App\Services\Push\PushService;
use Hhxsv5\LaravelS\Swoole\Task\Event;
use Hhxsv5\LaravelS\Swoole\Task\Listener;

class SendDeviceNotification extends Listener
class SendNotificationListener extends Listener
{
/**
* Create the event listener.
*
* @return void
*/
public function __construct()
{
//
}

/**
* Handle the event.
*
* @param DeviceNotificationProcessed $event
* @param SendNotificationEvent $event
* @return void
*/
public function handle(Event $event): void
Expand All @@ -36,6 +27,7 @@ public function handle(Event $event): void
'ios' => $device->is_clip ? ApnClipService::class : ApnAppService::class,
'android' => $device->is_clip ? FcmClipService::class : FcmAppService::class,
};
app($channel)->send($device, $message);

app(PushService::class, ['service' => app($channel)])->send($device, $message);
}
}
42 changes: 17 additions & 25 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

namespace App\Providers;

use App\Console\Commands\SendNotificationApnApp;
use App\Console\Commands\SendNotificationApnClip;
use App\Console\Commands\SendNotificationFcmApp;
use App\Console\Commands\SendNotificationFcmClip;
use App\Console\Commands\SendNotificationApnAppCommand;
use App\Console\Commands\SendNotificationApnClipCommand;
use App\Console\Commands\SendNotificationFcmAppCommand;
use App\Console\Commands\SendNotificationFcmClipCommand;
use App\Http\ReturnCode;
use App\Services\AppleService;
use App\Services\MessageService;
use App\Services\Push\ApnAppService;
use App\Services\Push\ApnClipService;
use App\Services\Push\FcmAppService;
use App\Services\Push\FcmClipService;
use App\Services\Push\PushService;
use App\Services\Push\PushServiceInterface;
use App\Services\PushDeerDeviceService;
use App\Services\PushDeerKeyService;
use App\Services\PushDeerMessageService;
Expand Down Expand Up @@ -87,41 +87,33 @@ public function boot(): void
$this->app->singleton(ApnAppService::class, function () {
return new ApnAppService();
});
$this->app->when(SendNotificationApnApp::class)
->needs(PushService::class)
->give(function () {
return $this->app->make(ApnAppService::class);
});
$this->app->when(SendNotificationApnAppCommand::class)
->needs(PushServiceInterface::class)
->give(ApnAppService::class);

// push-apn-clip
$this->app->singleton(ApnClipService::class, function () {
return new ApnClipService();
});
$this->app->when(SendNotificationApnClip::class)
->needs(PushService::class)
->give(function () {
return $this->app->make(ApnClipService::class);
});
$this->app->when(SendNotificationApnClipCommand::class)
->needs(PushServiceInterface::class)
->give(ApnClipService::class);

// push-fcm-app
$this->app->singleton(FcmAppService::class, function () {
return new FcmAppService();
});
$this->app->when(SendNotificationFcmApp::class)
->needs(PushService::class)
->give(function () {
return $this->app->make(FcmAppService::class);
});
$this->app->when(SendNotificationFcmAppCommand::class)
->needs(PushServiceInterface::class)
->give(FcmAppService::class);

// push-fcm-clip
$this->app->singleton(FcmClipService::class, function () {
return new FcmClipService();
});
$this->app->when(SendNotificationFcmClip::class)
->needs(PushService::class)
->give(function () {
return $this->app->make(FcmClipService::class);
});
$this->app->when(SendNotificationFcmClipCommand::class)
->needs(PushServiceInterface::class)
->give(FcmClipService::class);

}
}
2 changes: 0 additions & 2 deletions app/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace App\Providers;

use App\Events\DeviceNotificationProcessed;
use App\Listeners\SendDeviceNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use Laravel\Telescope\Telescope;
Expand Down
5 changes: 2 additions & 3 deletions app/Services/Push/ApnServiceAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ protected function addNotification(array $packages): Client
*/
public function send(PushDeerDevice $device, PushDeerMessage $message, int $tries = 1): bool
{
$client = $this->addNotification([
$client = $this->addNotification([[
'message' => $message,
'device' => $device,
]);

]]);
return $this->executor($client, $tries);
}

Expand Down
1 change: 0 additions & 1 deletion app/Services/Push/PushService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use App\Models\PushDeerDevice;
use App\Models\PushDeerMessage;
use PhpAmqpLib\Message\AMQPMessage;

class PushService implements PushServiceInterface
{
Expand Down
43 changes: 23 additions & 20 deletions app/Services/PushDeerMessageService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@

namespace App\Services;

use App\Events\SendNotificationEvent;
use App\Http\ReturnCode;
use App\Models\PushDeerDevice;
use App\Models\PushDeerKey;
use App\Models\PushDeerMessage;
use App\Models\PushDeerUser;
use App\Models\QueueExchangeEnum;
use App\Models\RoutingKeyEnum;
use Hhxsv5\LaravelS\Swoole\Task\Event;
use Illuminate\Support\Facades\Auth;
use Str;

Expand Down Expand Up @@ -44,7 +46,8 @@ public function push(array $data): void
$result = [];

$queueMessages = [];
$keys->each(function (PushDeerKey $key) use (&$data, &$queueMessages) {
$queueMessageCount = 0;
$keys->each(function (PushDeerKey $key) use (&$data, &$queueMessages, &$queueMessageCount) {
if ($key->userDevices->count() < 1) {
$this->message->addMessage(ReturnCode::ARGS, $key->name . ' 没有可用的设备,请先注册');
}
Expand All @@ -59,30 +62,30 @@ public function push(array $data): void
$message->save();

$message->text = $message->type == 'image' ? '[图片]' : $message->text;
$key->userDevices->each(function (PushDeerDevice $device) use (&$queueMessages, $message) {
$key->userDevices->each(function (PushDeerDevice $device) use (&$queueMessages, $message, &$queueMessageCount) {
// 方式一:异步事件,Swoole进程通信
// Event::fire((new DeviceNotificationProcessed($device, $message))->setTries(3));
$routingKey = match ($device->type) {
'ios' => $device->is_clip ? RoutingKeyEnum::NOTIFICATION_APN_CLIP : RoutingKeyEnum::NOTIFICATION_APN_APP,
'android' => $device->is_clip ? RoutingKeyEnum::NOTIFICATION_FCM_CLIP : RoutingKeyEnum::NOTIFICATION_FCM_APP,
};
$queueMessages[$routingKey->value][] = [
'device' => $device,
'message' => $message,
];
Event::fire((new SendNotificationEvent($device, $message))->setTries(3));
$queueMessageCount += 1;
// $routingKey = match ($device->type) {
// 'ios' => $device->is_clip ? RoutingKeyEnum::NOTIFICATION_APN_CLIP : RoutingKeyEnum::NOTIFICATION_APN_APP,
// 'android' => $device->is_clip ? RoutingKeyEnum::NOTIFICATION_FCM_CLIP : RoutingKeyEnum::NOTIFICATION_FCM_APP,
// };
// $queueMessages[$routingKey->value][] = [
// 'device' => $device,
// 'message' => $message,
// ];
});
});

// 方式二:异步队列,RabbitMQ
$queueMessageCount = 0;
foreach ($queueMessages as $routingKey => $items) {
$queueMessageCount += count($items);
app(QueueService::class)->publish(
data: $items,
exchange: QueueExchangeEnum::NOTIFICATION_EXCHANGE,
routingKey: RoutingKeyEnum::from($routingKey)
);
}
// foreach ($queueMessages as $routingKey => $items) {
// $queueMessageCount += count($items);
// app(QueueService::class)->publish(
// data: $items,
// exchange: QueueExchangeEnum::NOTIFICATION_EXCHANGE,
// routingKey: RoutingKeyEnum::from($routingKey)
// );
// }

$result[] = json_encode([
'counts' => $queueMessageCount,
Expand Down
Loading

0 comments on commit 7acfe5a

Please sign in to comment.