dongshipang8094 2019-03-04 08:22
浏览 151
已采纳

如何在Lumen中使用多个SQS(队列服务)实例?

I wanted to push messages to multiple SQS queues parallel or one after another, but it should be dynamic and when I start the worker it should fetch the messages from both the queues and differentiate.
How can I achieve this in Lumen?
UPDATE
How to use multiple worker for different queues with different amazon SQS instances?

  • 写回答

1条回答 默认 最新

  • du9843 2019-03-04 08:31
    关注

    As far as I can see Lumen and Laravel use the exact same code to handle queues so here's something that might work, though I haven't tested it.

    Run the queue worker as:

     php artisan queue:work --queue=queue1,queue2 
    

    This will mean that jobs in queue1 are processed before jobs in queue2 (unfortunately this is the only way to listen to multiple queues)

    Then in your job:

    class MyJob implements ShouldQueue
    {
        use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
    
       public function handle()
       {
           if ($this->job->getQueue() === 'queue1') {
              //Things
           } else {
              // different things
           }
       }
    

    If you need to use multiple connections you can't do that using a single worker, however you can use multiple workers at a time. First configure your connections e.g. in your config/queue.php

    'connections' => [
          'sqs' => [
            'driver' => 'sqs',
            'key' => 'your-public-key',
            'secret' => 'your-secret-key',
            'prefix' => 'https://sqs.us-east-1.amazonaws.com/your-account-id',
            'queue' => 'your-queue-name',
            'region' => 'us-east-1',
        ],
        'sqs2' => [
            'driver' => 'sqs',
            'key' => 'your-other-public-key',
            'secret' => 'your-other-secret-key',
            'prefix' => 'https://sqs.us-east-1.amazonaws.com/your-other-account-id',
            'queue' => 'your-other-queue-name',
            'region' => 'us-east-1',
        ],
    ]
    

    If you're using supervisor then setup your supervisor configuration, if not you'll have to start both workers manually. Here's a supervisor configuration you can use:

    [program:laravel-sqs-worker]
    process_name=%(program_name)s_%(process_num)02d
    command=php /home/forge/app.com/artisan queue:work sqs --queue=queue1
    autostart=true
    autorestart=true
    user=www-data 
    numprocs=1
    redirect_stderr=true
    stdout_logfile=/home/forge/app.com/worker.log
    
    [program:laravel-sqs2-worker]
    process_name=%(program_name)s_%(process_num)02d
    command=php /home/forge/app.com/artisan queue:work sqs2 --queue=queue2
    autostart=true
    autorestart=true
    user=www-data
    numprocs=1
    redirect_stderr=true
    stdout_logfile=/home/forge/app.com/worker.log
    

    Alter the paths and user settings according to your app.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行