Skip to content

Commit

Permalink
fix(queues): use scan instead of keys fixes taskforcesh/issues#65
Browse files Browse the repository at this point in the history
  • Loading branch information
manast committed Jan 23, 2023
1 parent 9c30e35 commit 3c017ec
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/queues-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,39 @@ export async function updateQueuesCache(
}

const queueNameRegExp = new RegExp("(.*):(.*):id");
const maxCount = 50000;
const maxTime = 30000;

const getQueueKeys = async (client: Redis.Redis | Redis.Cluster) => {
let keys = [],
cursor = "0";
const startTime = Date.now();

do {
const [nextCursor, scannedKeys] = await client.scan(
cursor,
"MATCH",
"*:*:id",
"COUNT",
maxCount
);
cursor = nextCursor;

keys.push(...scannedKeys);
} while (Date.now() - startTime < maxTime && cursor !== "0");

return keys;
};

async function getConnectionQueues(
redisOpts: RedisOptions,
clusterNodes?: string[]
): Promise<FoundQueue[]> {
const queues = await execRedisCommand(
redisOpts,
async (client) => {
const keys: string[] = await client.keys("*:*:id");
const keys = await getQueueKeys(client);

const queues = keys.map(function (key) {
var match = queueNameRegExp.exec(key);
if (match) {
Expand Down

0 comments on commit 3c017ec

Please sign in to comment.