acdphp/laravel-queued-events

将事件分发到队列中。这对于使用扇出队列的分布式系统事件特别有用。

v2.1.0 2024-01-03 11:01 UTC

This package is auto-updated.

Last update: 2024-09-03 12:25:44 UTC


README

Latest Stable Version

将事件分发到队列中。这对于使用扇出队列的分布式系统事件特别有用。

workflow

安装

  1. 安装包
    composer require acdphp/laravel-queued-events

用法

  1. QueuedEvent 扩展到您的事件中。

    use Acdphp\QueuedEvents\Events\QueuedEvent;
    
    class UserCreatedEvent extends QueuedEvent
    {
        // Remove the Dispatchable trait
    
        public function __construct(public $object)
        {
        }
    }
  2. 调用 dispatch()

    UserCreatedEvent::dispatch(['foo' => 'bar']);
    • 您可以指定队列连接和队列
    UserCreatedEvent::dispatch(['foo' => 'bar'])
        ->onConnection('your-fanout-queue-connection')
        ->onQueue('your-custom-queue');
    • 还提供了实用工具: dispatchIf()dispatchUnless()
    // Dispatches if $condition is true
    UserCreatedEvent::dispatchIf($condition, ['foo' => 'bar']);
    
    // Dispatches if $condition is false
    UserCreatedEvent::dispatchUnless($condition, ['foo' => 'bar']);
  3. 默认分发方法以 internal 前缀

    UserCreatedEvent::internalDispatch(['foo' => 'bar']);
    
    UserCreatedEvent::internalDispatchIf(['foo' => 'bar']);
    
    UserCreatedEvent::internalDispatchUnless(['foo' => 'bar']);

配置

php artisan vendor:publish --tag=queued-events-config

QUEUED_EVENTS_QUEUE_CONNECTION

默认队列连接将设置为您的 QUEUE_CONNECTION 所设置。您可以通过设置 QUEUED_EVENTS_QUEUE_CONNECTION 来覆盖此设置。

QUEUED_EVENTS_QUEUE

默认队列将是 default。您可以通过设置 QUEUED_EVENTS_QUEUE 来覆盖此设置。

注意

  • 使用 Laravel 辅助函数,如 event(...)app('events')->dispatch(...),将内部分发作业。仅使用 ::dispatch::dispatchIf::dispatchUnless 来在队列中分发。
  • 如果您只在一个单体应用程序中使用此功能,请使用 Laravel 的队列监听器。

许可证

MIT 许可证 (MIT)。有关更多信息,请参阅许可证文件