Skip to content

Commit

Permalink
Merge pull request #719 from desci-labs/m0ar/fix-invalidation-script
Browse files Browse the repository at this point in the history
scripts/invalidate-redis: fix script always hanging, add invalidateAll option
  • Loading branch information
m0ar authored Dec 16, 2024
2 parents 5c46fbc + 83939b2 commit 6d60623
Showing 1 changed file with 32 additions and 12 deletions.
44 changes: 32 additions & 12 deletions desci-server/src/scripts/invalidate-redis-cache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,32 @@ Usage Examples:
invalidateByUuid: OPERATION=invalidateByUuid NODE_UUID=noDeUuiD. npm run script:invalidate-redis-cache
*/
async function main() {
try {
const { operation, nodeUuid } = getOperationEnvs();

main();
function main() {
const { operation, nodeUuid } = getOperationEnvs();

switch (operation) {
case 'invalidateByUuid':
if (!nodeUuid) return logger.error('Missing NODE_UUID or MANIFEST_CID');
invalidateByUuid({ nodeUuid });
break;
default:
logger.error('Invalid operation, valid operations include: invalidateByUuid');
return;
switch (operation) {
case 'invalidateByUuid':
if (!nodeUuid) return logger.error('Missing NODE_UUID or MANIFEST_CID');
await invalidateByUuid({ nodeUuid });
break;
case 'invalidateAll':
if (nodeUuid) {
logger.error('NODE_UUID was passed to invalidateAll, aborting in case of mistake');
throw new Error('invalidateAll does not take NODE_UUID');
}
await invalidateAll();
break;
default:
logger.error('Invalid operation, valid operations include: invalidateByUuid');
return;
}
} catch (e) {
const err = e as Error;
console.error('Script failed:', err.message);
process.exit(1);
} finally {
await redisClient.quit();
}
}

Expand All @@ -37,6 +50,11 @@ function getOperationEnvs() {
};
}

async function invalidateAll() {
await redisClient.flushDb();
logger.info('[invalidateAll] Wiped all keys from cache');
}

async function invalidateByUuid({ nodeUuid }: { nodeUuid: string }) {
// Find all published versions of the node
if (!nodeUuid.endsWith('.')) nodeUuid += '.';
Expand Down Expand Up @@ -98,3 +116,5 @@ async function deleteKeys(pattern: string) {

logger.info({ pattern }, `All matching keys deleted.`);
}

main();

0 comments on commit 6d60623

Please sign in to comment.