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

Job runs twice #374

Closed yujin1st closed 4 years ago

yujin1st commented 4 years ago

What steps will reproduce the problem?

Have long tasks - each takes up to 2-3 hours. Each one has record in db and also has jobId, which is set by queue job

  1. have 10-20 jobs with ttr 3 hours and 5 workers
  2. each job checks at the beginning if jobId for task is set. If doesn't exist
    • it generates uid (time+rand_int) and saves it into db to it's task record
    • if not - it means task runs second time

Redis for queue

What do you get?

Often jobs runs second attempt.

Additional info

Q A
Yii version 2.0.32
Yii queue version 2.3.0
PHP version 7.3
Operating system debian 9


class ImportJob extends BaseShopJob implements RetryableJobInterface
{

  /** @var integer */
  public $importId;
  private $uid = 0;

  /** @var Import */
  private $_import;

  /**
   * @return Import
   */
  public function getImport() {
    if ($this->_import === null) {
      $this->_import = Import::findOne($this->importId);
    }
    return $this->_import;
  }

  /**
   * @param Queue $queue which pushed and is handling the job
   *
   * @throws Exception
   */
  public function execute($queue) {

    if (!$this->uid) {
      $this->uid = time() + random_int(0, 9999);
    }

    if ($this->getImport()->jobId && $this->uid !== (int)$this->getImport()->jobId) {
      Yii::error([
        'job is already running',
        'oldUid' => $this->getImport()->jobId,
        'curUid' => $this->uid,
        'importId' => $this->importId,
      ], 'supply\import\run-duplicate');
      return;
    } else {
      $this->getImport()->updateAttributes([
        'jobId' => $this->uid,
      ]);
    }

    Yii::info([
      'starting import job',
      'importId' => $this->importId,
      'uid' => $this->uid,
    ], 'supply\import\run');

      $this->getImport()->run($this->uid);
  }

  public function getTtr() {
    return 60 * 60 * 5; // 5 hours
  }

  /**
   *
   * Запрещаем повторный запуск 
   *
   * @param int $attempt number
   * @param Exception|Throwable $error from last execute of the job
   *
   * @return bool
   */
  public function canRetry($attempt, $error) {
    return false;
  }
}```

![111](https://user-images.githubusercontent.com/2317913/78541970-73d08f00-7831-11ea-9685-e5fd9f6e7195.png)
![222](https://user-images.githubusercontent.com/2317913/78541985-77641600-7831-11ea-92d7-ebc6412eca9b.png)
yii-bot commented 4 years ago

Thank you for your question. In order for this issue tracker to be effective, it should only contain bug reports and feature requests.

We advise you to use our community driven resources:

If you are confident that there is a bug in the framework, feel free to provide information on how to reproduce it. This issue will be closed for now.

This is an automated comment, triggered by adding the label question.

yujin1st commented 4 years ago

Why is this marked as question? Post has described problem, it has screenshots to show situation and some code to reproduce it