Skip to content

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

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

Merged
merged 11 commits into from
Mar 11, 2025
Merged
Show file tree
Hide file tree
Changes from 7 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
49 changes: 49 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,52 @@ protected function tearDown()
$this->getQueue()->redis->flushdb();
parent::tearDown();
}

/**
* Verify that Redis data persists when process crashes during moveExpired
*
* Steps:
* 1. Push a delayed job into queue
* 2. Wait for job to expire
* 3. Mock Redis to simulate crash during moveExpired
* 4. Successfully process job after recovery
*/
public function testConsumeMsgAtLeastOnce()
{
$job = $this->createSimpleJob();
$this->getQueue()->delay(1)->push($job);
//expect 1 msg should be recv
$msgCount = 0;
$this->getQueue()->messageHandler = function () use(&$msgCount) {
$msgCount++;
};

//ensuer the delayed msg can be consumed
sleep(2);

// based on the implemention, emulate a crash when redis "rpush"
// command should be execute,
$mockRedis = Instance::ensure([
'class' => RedisCrashMock::class,
'hostname' => getenv('REDIS_HOST') ?: 'localhost',
'database' => getenv('REDIS_DB') ?: 1,
'crashOnCommand' => 'rpush' // Crash when trying to move job to waiting queue
], 'yii\redis\Connection');

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

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

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

use yii\redis\Connection;

class RedisCrashMock extends Connection
{
public $crashOnCommand;

public function executeCommand($name, $params = [])
{
if ($name === $this->crashOnCommand) {
throw new \RuntimeException('Simulated Redis crash');
}
return parent::executeCommand($name, $params);
}
}