mirror of
https://github.com/outline/outline.git
synced 2025-12-30 07:19:52 -06:00
chore: Script to invalidate unfurls cache (#7459)
This commit is contained in:
75
server/scripts/20240825000000-invalidate-unfurls-cache.ts
Normal file
75
server/scripts/20240825000000-invalidate-unfurls-cache.ts
Normal file
@@ -0,0 +1,75 @@
|
||||
import RedisAdapter from "@server/storage/redis";
|
||||
import "./bootstrap";
|
||||
|
||||
const redis = RedisAdapter.defaultClient;
|
||||
|
||||
const Keys = {
|
||||
Pattern: "unfurl:*",
|
||||
Total: 0,
|
||||
Error: 0,
|
||||
};
|
||||
|
||||
const Page = {
|
||||
Count: 0,
|
||||
Limit: 1000,
|
||||
};
|
||||
|
||||
const deleteKeys = async (keys: string[]): Promise<void> => {
|
||||
Keys.Total += keys.length;
|
||||
|
||||
const pipeline = redis.pipeline();
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
keys.forEach((key: any) => pipeline.del(key));
|
||||
|
||||
const res = await pipeline.exec();
|
||||
|
||||
if (!res) {
|
||||
Keys.Error += keys.length;
|
||||
return;
|
||||
}
|
||||
|
||||
res.forEach(([err]) => {
|
||||
if (err) {
|
||||
Keys.Error++;
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
const execute = async () => {
|
||||
const stream = redis.scanStream({ match: Keys.Pattern, count: Page.Limit });
|
||||
|
||||
console.log("Invalidating unfurls cache...");
|
||||
|
||||
stream.on("data", (unfurlKeys) => {
|
||||
if (!unfurlKeys.length) {
|
||||
return;
|
||||
}
|
||||
|
||||
Page.Count++;
|
||||
console.log(`Starting page ${Page.Count}...`);
|
||||
|
||||
stream.pause();
|
||||
|
||||
void deleteKeys(unfurlKeys).then(() => {
|
||||
console.log(`Completed page ${Page.Count}.`);
|
||||
stream.resume();
|
||||
});
|
||||
});
|
||||
|
||||
stream.on("end", () => {
|
||||
console.log("Unfurls cache invalidation completed.");
|
||||
console.log(
|
||||
`\nStats\nTotal keys: ${Keys.Total}\nErrored keys: ${Keys.Error}`
|
||||
);
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
stream.on("error", (err) => {
|
||||
console.log(
|
||||
`Encountered an error when invalidating unfurls cache: ${err.message}`
|
||||
);
|
||||
process.exit(1);
|
||||
});
|
||||
};
|
||||
|
||||
void execute();
|
||||
@@ -6,5 +6,6 @@ if (process.env.NODE_ENV !== "test") {
|
||||
}
|
||||
|
||||
require("../storage/database");
|
||||
require("../storage/redis");
|
||||
|
||||
export {};
|
||||
|
||||
Reference in New Issue
Block a user