From 3806b534b11eabfe797a7b81d1502c58c703ae5c Mon Sep 17 00:00:00 2001 From: Jesper Callesen Date: Thu, 26 Sep 2024 12:53:57 +0200 Subject: [PATCH 1/3] Updated purge() and updateMetrics() Updated purge() and updateMetrics() to be more deterministic, to avoid exception. --- src/LuaScripts.php | 57 +++++++++++++++++++++++----------------------- 1 file changed, 29 insertions(+), 28 deletions(-) diff --git a/src/LuaScripts.php b/src/LuaScripts.php index ef260f03..7ba5b101 100644 --- a/src/LuaScripts.php +++ b/src/LuaScripts.php @@ -21,17 +21,21 @@ public static function updateMetrics() redis.call('sadd', KEYS[2], KEYS[1]) local hash = redis.call('hmget', KEYS[1], 'throughput', 'runtime') - - local throughput = hash[1] + 1 + + local throughput = tonumber(hash[1]) or 0 + + throughput = throughput + 1 + local runtime = 0 - if hash[2] then - runtime = ((hash[1] * tonumber(hash[2])) + tonumber(ARGV[1])) / throughput + runtime = ((tonumber(hash[1]) * tonumber(hash[2])) + tonumber(ARGV[1])) / throughput else runtime = tonumber(ARGV[1]) end - + redis.call('hmset', KEYS[1], 'throughput', throughput, 'runtime', runtime) + + return {throughput, runtime} LUA; } @@ -50,30 +54,27 @@ public static function purge() return <<<'LUA' local count = 0 - local cursor = 0 + local batch_size = tonumber(100) + local start = tonumber(0) - repeat - -- Iterate over the recent jobs sorted set - local scanner = redis.call('zscan', KEYS[1], cursor) - cursor = scanner[1] - - for i = 1, #scanner[2], 2 do - local jobid = scanner[2][i] - local hashkey = ARGV[1] .. jobid - local job = redis.call('hmget', hashkey, 'status', 'queue') - - -- Delete the pending/reserved jobs, that match the queue - -- name, from the sorted sets as well as the job hash - if((job[1] == 'reserved' or job[1] == 'pending') and job[2] == ARGV[2]) then - redis.call('zrem', KEYS[1], jobid) - redis.call('zrem', KEYS[2], jobid) - redis.call('del', hashkey) - count = count + 1 - end + -- Use ZRANGE to get a range of elements from the sorted set in a deterministic way + local jobs = redis.call('zrange', KEYS[1], start, start + batch_size - 1) + + for i = 1, #jobs do + local jobid = jobs[i] + local hashkey = ARGV[1] .. jobid + local job = redis.call('hmget', hashkey, 'status', 'queue') + + -- Delete the pending/reserved jobs that match the queue name + if ((job[1] == 'reserved' or job[1] == 'pending') and job[2] == ARGV[2]) then + redis.call('zrem', KEYS[1], jobid) + redis.call('zrem', KEYS[2], jobid) + redis.call('del', hashkey) + count = count + 1 end - until cursor == '0' - - return count + end + + return {count, start + batch_size} LUA; } -} +} \ No newline at end of file From 2c90a09e70ac8d3c33f0b88bc0aa35813c606c56 Mon Sep 17 00:00:00 2001 From: Jesper Callesen Date: Thu, 26 Sep 2024 13:11:21 +0200 Subject: [PATCH 2/3] This should fix it the failed tests. --- src/LuaScripts.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/src/LuaScripts.php b/src/LuaScripts.php index 7ba5b101..c53c047e 100644 --- a/src/LuaScripts.php +++ b/src/LuaScripts.php @@ -34,8 +34,6 @@ public static function updateMetrics() end redis.call('hmset', KEYS[1], 'throughput', throughput, 'runtime', runtime) - - return {throughput, runtime} LUA; } @@ -74,7 +72,7 @@ public static function purge() end end - return {count, start + batch_size} + return count LUA; } } \ No newline at end of file From f92e3dbfd8f2b8dbb5c4a3d2064e8edf33544f00 Mon Sep 17 00:00:00 2001 From: Jesper Callesen Date: Thu, 26 Sep 2024 13:14:04 +0200 Subject: [PATCH 3/3] Was missing newline. --- src/LuaScripts.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/LuaScripts.php b/src/LuaScripts.php index c53c047e..dc4b37a7 100644 --- a/src/LuaScripts.php +++ b/src/LuaScripts.php @@ -75,4 +75,4 @@ public static function purge() return count LUA; } -} \ No newline at end of file +}