caching properly works now
Some checks failed
Deploy & Publish Image / deploy (ubuntu-latest, 2.44.0) (push) Has been cancelled

Took 2 hours 0 minutes
This commit is contained in:
Braydon 2024-10-13 20:42:22 -04:00
parent 48973560d1
commit 573f13568d

View File

@ -4,6 +4,8 @@ import path from "node:path";
import config from "@/config"; import config from "@/config";
import simpleGit from "simple-git"; import simpleGit from "simple-git";
import os from "node:os"; import os from "node:os";
import { cache } from "react";
import "server-only";
/** /**
* The regex to match for metadata. * The regex to match for metadata.
@ -35,7 +37,8 @@ const UPDATE_INTERVAL_MS: number = 10 * 60 * 1000; // 10 minutes in milliseconds
* Clone the Git repository if DOCS_DIR is a URL, else use the local directory. * Clone the Git repository if DOCS_DIR is a URL, else use the local directory.
* If it's a Git URL, clone it to a cache directory and reuse it. * If it's a Git URL, clone it to a cache directory and reuse it.
*/ */
const getDocsDirectory = async (): Promise<string> => { const getDocsDirectory = cache(async (): Promise<string> => {
console.log("retrieve docs dir");
if (isGitUrl(DOCS_DIR)) { if (isGitUrl(DOCS_DIR)) {
const repoHash: string = Buffer.from(DOCS_DIR).toString("base64"); // Create a unique identifier based on the repo URL const repoHash: string = Buffer.from(DOCS_DIR).toString("base64"); // Create a unique identifier based on the repo URL
const cacheDir: string = path.join(os.tmpdir(), "docs_cache", repoHash); const cacheDir: string = path.join(os.tmpdir(), "docs_cache", repoHash);
@ -45,6 +48,7 @@ const getDocsDirectory = async (): Promise<string> => {
console.log("Fetching initial docs from Git..."); console.log("Fetching initial docs from Git...");
try { try {
await simpleGit().clone(DOCS_DIR, cacheDir, { "--depth": 1 }); await simpleGit().clone(DOCS_DIR, cacheDir, { "--depth": 1 });
storeUpdatedRepoTime();
} catch (error) { } catch (error) {
// Simply ignore this error. When cloning the repo for // Simply ignore this error. When cloning the repo for
// the first time, it'll sometimes error saying the dir // the first time, it'll sometimes error saying the dir
@ -53,17 +57,15 @@ const getDocsDirectory = async (): Promise<string> => {
} else if (shouldUpdateRepo()) { } else if (shouldUpdateRepo()) {
// Pull the latest changes from Git // Pull the latest changes from Git
console.log("Updating docs content from Git..."); console.log("Updating docs content from Git...");
await simpleGit().pull(cacheDir); await simpleGit(cacheDir)
fs.writeFileSync( .reset(["--hard"]) // Reset any local changes
LAST_UPDATE_FILE, .pull(); // Pull latest changes
JSON.stringify({ lastUpdate: Date.now() }), storeUpdatedRepoTime();
"utf-8"
);
} }
return cacheDir; return cacheDir;
} }
return DOCS_DIR; return DOCS_DIR;
}; });
const shouldUpdateRepo = (): boolean => { const shouldUpdateRepo = (): boolean => {
if (!fs.existsSync(LAST_UPDATE_FILE)) { if (!fs.existsSync(LAST_UPDATE_FILE)) {
@ -76,6 +78,13 @@ const shouldUpdateRepo = (): boolean => {
); );
}; };
const storeUpdatedRepoTime = () =>
fs.writeFileSync(
LAST_UPDATE_FILE,
JSON.stringify({ lastUpdate: Date.now() }),
"utf-8"
);
/** /**
* Get the content to display in the docs. * Get the content to display in the docs.
*/ */