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

Enhancement: Ensure messages are consumed at least once. #516

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/drivers/redis/Queue.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,10 @@ protected function moveExpired($from)
{
$now = time();
if ($expired = $this->redis->zrevrangebyscore($from, $now, '-inf')) {
$this->redis->zremrangebyscore($from, '-inf', $now);
foreach ($expired as $id) {
$this->redis->rpush("$this->channel.waiting", $id);
}
$this->redis->zremrangebyscore($from, '-inf', $now);
}
}

Expand Down
38 changes: 38 additions & 0 deletions tests/drivers/redis/QueueTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use tests\app\RetryJob;
use tests\drivers\CliTestCase;
use Yii;
use yii\di\Instance;
use yii\queue\redis\Queue;

/**
Expand Down Expand Up @@ -137,4 +138,41 @@ protected function tearDown()
$this->getQueue()->redis->flushdb();
parent::tearDown();
}

public function testMoveExpired()
{
$job = $this->createSimpleJob();
$this->getQueue()->delay(1)->push($job);
//expect 1 msg should be recv
$msgCount = 0;
$this->getQueue()->messageHandler = function () use(&$msgCount) {
$msgCount++;
};

$fault = function () {
sleep(2);
$mockRedis = Instance::ensure([
'class' => RedisCrashMock::class,
'hostname' => getenv('REDIS_HOST') ?: 'localhost',
'database' => getenv('REDIS_DB') ?: 1,
], 'yii\redis\Connection');

$queue = $this->getQueue();
$old = $queue->redis;
$queue->redis = $mockRedis;

try {
$queue->run(false);
}catch (\Exception $e){
}finally{
$queue->redis = $old;
}
};
$fault();

//make the redlock invalid after 1s
sleep(2);
$this->getQueue()->run(false);
$this->assertEquals(1, $msgCount);
}
}
13 changes: 13 additions & 0 deletions tests/drivers/redis/RedisCrashMock.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

namespace tests\drivers\redis;

use yii\redis\Connection;

class RedisCrashMock extends Connection
{
function rpush($key, $value)
{
throw new \Exception('panic');
}
}