yiisoft / yii2-queue

Yii2 Queue Extension. Supports DB, Redis, RabbitMQ, Beanstalk and Gearman
BSD 3-Clause "New" or "Revised" License
1.07k stars 296 forks source link

Incompatible priority values for different drivers #217

Open antonrybalko opened 6 years ago

antonrybalko commented 6 years ago

Some of drivers support priority queues. But priority values ​​for different drivers are not compatible. I used DB driver and had this code for high priority tasks:

Yii::$app->queue->priority(0)->push($someJob);

By default DB driver sets priority to 1024 and lesser value means higher priority.

Now I move to AMQP Interop driver (RabbitMQ). And I have to rewrite my code for high priority jobs:

Yii::$app->queue->priority(10)->push($someJob);

AMQP Interop maximum priority is 10 by default and higher value means higher priority.

As I see Gearman also supports priorities

PRIORITY METHOD
0 doHighBackground()
1 doBackground()
2 doLowBackground()

I understand that priority value depends on driver. But I'd like to have scalable system and I'd like to have ability to change driver without code rewriting. I propose to add three common priorities to driver classes.

namespace yii\queue\db;

class Queue extends CliQueue
{
    public $priorityLow = 2048;
    public $priorityMedium = 1024;
    public $priorityHigh = 0;
    //...
}
namespace yii\queue\amqp_interop;

class Queue extends CliQueue
{
    public $priorityLow = 1;
    public $priorityMedium = 5;
    public $priorityHigh = 10;
    //...
}
namespace yii\queue\gearman;

class Queue extends CliQueue
{
    public $priorityLow = 'low';
    public $priorityMedium = null;
    public $priorityHigh = 'high';
    //...
}

And use code like

Yii::$app->queue->priority(Yii::$app->queue->priorityHigh)->push($someJob);

Or something like this.

zhuravljov commented 6 years ago

It looks like as syntactic sugar to me. Moreover, some brokers aren't support message priorities. This will not work with amqp, file, redis and sqs drivers.