Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostgreSQL LISTEN NOTIFY support #375

Open
DrDeath72 opened this issue Apr 7, 2020 · 2 comments
Open

PostgreSQL LISTEN NOTIFY support #375

DrDeath72 opened this issue Apr 7, 2020 · 2 comments
Labels
pr:missing usecase It is not clear what is the use case for the pull request.

Comments

@DrDeath72
Copy link

DrDeath72 commented Apr 7, 2020

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();
	}

}
@samdark samdark added the pr:missing usecase It is not clear what is the use case for the pull request. label Apr 7, 2020
@yii-bot
Copy link

yii-bot commented Apr 7, 2020

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.

@boboldehampsink
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr:missing usecase It is not clear what is the use case for the pull request.
Projects
None yet
Development

No branches or pull requests

4 participants