Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shim): add nginx cache purger on pre-registration #469

Merged
merged 3 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions container/shim/src/modules/registration.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { parseVersionNumber } from "../utils/version.js";
import { orchestratorAgent } from "../utils/http.js";
import { prefillCache } from "../utils/prefill.js";
import { check } from "../lib/ocsp/check.js";
import { purgeCacheFile } from "../utils/purger.js";

const debug = Debug.extend("registration");

Expand Down Expand Up @@ -50,6 +51,13 @@ export async function register(initial = false) {
preregisterResponse = await sendPreRegisterRequest(postOptions({ nodeId: NODE_ID }));
}

if (preregisterResponse.filesToPurge) {
debug("Purging %d files", preregisterResponse.filesToPurge.length);
for (const file of preregisterResponse.filesToPurge) {
await purgeCacheFile(file);
}
}

if (VERSION !== DEV_VERSION && initial && preregisterResponse?.speedtestRequired !== false) {
let speedtest;
try {
Expand Down Expand Up @@ -247,6 +255,16 @@ async function sendRegisterRequest(initial, registerOptions) {
}
}

/**
* Sends a pre-registration request to the orchestrator.
*
* @typedef {object} PreRegisterResponse
* @property {boolean} speedtestRequired
* @property {string[]} filesToPurge
*
* @param {object} postOptions
* @returns {Promise<PreRegisterResponse>}
*/
async function sendPreRegisterRequest(postOptions) {
debug("Pre-registering with orchestrator");

Expand Down
20 changes: 20 additions & 0 deletions container/shim/src/utils/purger.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import fs from "node:fs/promises";
import { createHash } from "node:crypto";
import path from "node:path";
import { debug } from "./logging.js";

const CACHE_DIR = "/usr/src/app/shared/nginx_cache";

export async function purgeCacheFile(key) {
debug("Purging cache file for key %s", key);
const cacheFilePath = keyToCacheFilePath(key);
await fs
.unlink(path.join(CACHE_DIR, cacheFilePath))
.catch((err) => debug("Failed to purge cache file: %s", err.message));
}

function keyToCacheFilePath(path) {
const hash = createHash("md5").update(path).digest("hex");

return `${hash.slice(-2)}/${hash.slice(-4, -2)}/${hash.slice(-6, -4)}/${hash}`;
}
Loading