cjieyan/laravel-queue-cmq

Queue Adapter for Tencent Qcloud CMQ SDK

6.0.0 2021-06-04 06:32 UTC

README

68747470733a2f2f696d6763616368652e71712e636f6d2f6f70656e5f70726f6a2f70726f6a5f71636c6f75645f76322f696e7465726e6174696f6e616c2f646f632f6373732f696d672f69636f6e2f69636f6e2d7a7a6a2e737667

Tencent Cloud Message Queue Driver for Laravel Queue

Software License Build Status Coverage Status Quality Score Packagist Version Total Downloads

FOSSA Status

Installation

composer require freyo/laravel-queue-cmq

Configure

Laravel 5.5+ uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.

  1. config/app.php:
'providers' => [
  // ...
  LaravelQueueCMQ\LaravelQueueCMQServiceProvider::class,
]
  1. .env:
QUEUE_DRIVER=cmq

CMQ_SECRET_KEY=
CMQ_SECRET_ID=

CMQ_QUEUE_HOST=https://cmq-queue-{region}.api.qcloud.com
CMQ_QUEUE=queue_name #default queue name
CMQ_QUEUE_POLLING_WAIT_SECONDS=0

CMQ_TOPIC_ENABLE=false # set to true to use topic
CMQ_TOPIC_FILTER=routing # or msgtag
CMQ_TOPIC_HOST=https://cmq-topic-{region}.api.qcloud.com
CMQ_TOPIC=topic_name

Tips

  • Region should be replaced with a specific region: gz (Guangzhou), sh (Shanghai), or bj (Beijing).

  • Domain for public network API request: cmq-queue-region.api.qcloud.com / cmq-topic-region.api.qcloud.com

  • Domain for private network API request: cmq-queue-region.api.tencentyun.com / cmq-topic-region.api.tencentyun.com

Usage

Once you completed the configuration you can use Laravel Queue API. If you used other queue drivers you do not need to change anything else. If you do not know how to use Queue API, please refer to the official Laravel documentation: http://laravel.com/docs/queues

Example

Dispatch Jobs

The default connection name is cmq

//use queue only
Job::dispatch()->onConnection('connection-name')->onQueue('queue-name');
// or dispatch((new Job())->onConnection('connection-name')->onQueue('queue-name'))

//use topic and tag filter
Job::dispatch()->onConnection('connection-name')->onQueue('tag1,tag2,tag3');
// or dispatch((new Job())->onConnection('connection-name')->onQueue('tag1,tag2,tag3'))

//use topic and routing filter
Job::dispatch()->onConnection('connection-name')->onQueue('routing-key');
// or dispatch((new Job())->onConnection('connection-name')->onQueue('routing-key'))

Multiple Queues

Configure config/queue.php

'connections' => [
    //...
    'new-connection-name' => [
        'driver' => 'cmq',
        'secret_key' => 'your-secret-key',
        'secret_id'  => 'your-secret-id',
        'queue' => 'your-queue-name',
        'options' => [
            'queue' => [
                'host'                 => 'https://cmq-queue-region.api.qcloud.com',
                'name'                 => 'your-queue-name',
                'polling_wait_seconds' => 0, // 0-30 seconds
                'retries'              => 3,
            ],
            'topic' => [
                'enable'  => false,
                'filter'  => 'routing', // routing or msgtag
                'host'    => 'https://cmq-topic-region.api.qcloud.com',
                'name'    => 'your-topic-name',
                'retries' => 3,
            ],
        ],
        'plain' => [
            'enable' => false,
            'job' => 'App\Jobs\CMQPlainJob@handle',
        ],
    ];
    //...
];

Process Jobs

php artisan queue:work {connection-name} --queue={queue-name}

Plain Mode

Configure .env

CMQ_PLAIN_ENABLE=true
CMQ_PLAIN_JOB=App\Jobs\CMQPlainJobHandler@handle

Create a job implements PlainPayload interface. The method getPayload must return a sting value.

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use LaravelQueueCMQ\Queue\Contracts\PlainPayload;

class CMQPlainJob implements ShouldQueue, PlainPayload
{
    use Dispatchable, InteractsWithQueue, Queueable;
    
    protected $payload;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($payload)
    {
        $this->payload = $payload;
    }
    
    /**
     * Get the plain payload of the job.
     *
     * @return string
     */
    public function getPayload()
    {
        return $this->payload;
    }
}

Create a plain job handler

<?php

namespace App\Jobs;

use Illuminate\Queue\Jobs\Job;

class CMQPlainJobHandler
{
    /**
     * Execute the job.
     * 
     * @param \Illuminate\Queue\Jobs\Job $job
     * @param string $payload
     * 
     * @return void
     */
    public function handle(Job $job, $payload)
    {
        // processing your payload...
        var_dump($payload);
        
        // release back to the queue manually when failed.
        // $job->release();
        
        // delete message when processed.
        if (! $job->isDeletedOrReleased()) {
            $job->delete();
        }        
    }
}

References

License

The MIT License (MIT). Please see License File for more information.

FOSSA Status