yii2-queue icon indicating copy to clipboard operation
yii2-queue copied to clipboard

PostgreSQL LISTEN NOTIFY support

Open DrDeath72 opened this issue 5 years ago • 2 comments

Instead of db query each timeout seconds

my sux version

<?php

namespace application\modules\queue\components\drivers;

use yii\queue\db\Queue;

class Db extends Queue
{

	public $listener = 'queue';

	public function run($repeat, $timeout = 0)
	{
		if(!$repeat) {
			return parent::run($repeat, $timeout);
		}
		$this->db->createCommand("LISTEN {$this->listener}")->execute();
		return $this->runWorker(function (callable $canContinue) use ($repeat, $timeout) {
			$first = true;
			while($canContinue()) {
				if(!$first) {
					$notify = $this->db->pdo->pgsqlGetNotify();
					if(!$notify) {
						sleep($timeout);
						continue;
					}
				}
				$first = false;
				while(true) {
					if ($payload = $this->reserve() and $this->handleMessage(
						$payload['id'],
						$payload['job'],
						$payload['ttr'],
						$payload['attempt']
					)) {
						$this->release($payload);
						continue;
					}
					break;
				}
			}
		});
	}

	public function push($job)
	{
		$id = parent::push($job);
		$this->notify($id);
		return $id;
	}

	public function notify($payload)
	{
		$payload = serialize($payload);
		$this->db->createCommand("NOTIFY {$this->listener}, '$payload'")->execute();
	}

}

DrDeath72 avatar Apr 07 '20 09:04 DrDeath72

Thank you for putting effort in the improvement of the Yii framework. We have reviewed your pull request.

Unfortunately a use case is missing. It is required to get a better understanding of the pull request and helps us to determine the necessity and applicability of the suggested change to the framework.

Could you supply us with a use case please? Please be as detailed as possible and show some code!

Thanks!

This is an automated comment, triggered by adding the label pr:missing usecase.

yii-bot avatar Apr 07 '20 13:04 yii-bot

@samdark this is a very interesting concept - see https://adriano.fyi/posts/2023-09-24-choose-postgres-queue-technology/ for the use case

boboldehampsink avatar Sep 25 '23 11:09 boboldehampsink