yiisoft / yii2-queue

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

How do I use retry? #350

Open wxxiong6 opened 5 years ago

wxxiong6 commented 5 years ago

How do I use retry?

Q A
Yii version 2.0.15.1
PHP version 7.2.5
Operating system linux
<?php
namespace backend\job;

use yii\queue\RetryableJobInterface;

class TestPush implements RetryableJobInterface
{
    public $jobLogId;

    public function execute($queue)
    {
        var_dump($queue->attempts, $queue->ttr);
        echo '123123213213' , "\n";
        throw new \Exception(date("Y-m-d H:i:s"));
    }

    public function getTtr()
    {
        return 30;
    }

    public function canRetry($attempt, $error)
    {
        echo $attempt. " ============ canRetry \n";
        return true;
    }
}

output

2019-09-05 09:36:22 [5d70615ba9a845.54703703] backend\job\TestPush (attempt: 1) - Started
/var/www/html/privateFund/trunk/backend/job/TestPush.php:12:
int(1)
/var/www/html/privateFund/trunk/backend/job/TestPush.php:12:
int(300)
123123213213
1 ============ canRetry
2019-09-05 09:36:22 [5d70615ba9a845.54703703] backend\job\TestPush (attempt: 1) - Error (0.081 s)
> Exception: 2019-09-05 09:36:22

1 ============ canRetry The attempt is always 1

rob006 commented 5 years ago

Next try should increment $attempt. Can you share logs of two sequential tries of the same job?

wxxiong6 commented 5 years ago

I used amqp deivers .
The driver does not support retry. see yii2-queue/src/drivers/amqp/Queue.php:63

    public function listen()
    {
        $this->open();
        $callback = function (AMQPMessage $payload) {
            $id = $payload->get('message_id');
            list($ttr, $message) = explode(';', $payload->body, 2);
            if ($this->handleMessage($id, $message, $ttr, 1)) {
                $payload->delivery_info['channel']->basic_ack($payload->delivery_info['delivery_tag']);
            }
        };
        $this->channel->basic_qos(null, 1, null);
        $this->channel->basic_consume($this->queueName, '', false, false, false, false, $callback);
        while (count($this->channel->callbacks)) {
            $this->channel->wait();
        }
    }

amqp_interop drivers can use retry but need to upgrade PHP version to 7.1.3.