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

Added script to automate cloudflare cache purge #8736

Merged
merged 4 commits into from
Aug 12, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion packages/commonwealth/Procfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ consumer: cd packages/commonwealth && node --import=extensionless/register --max
evm-ce: cd packages/commonwealth && node --import=extensionless/register --max-old-space-size=$(../../scripts/get-max-old-space-size.sh) build/server/workers/evmChainEvents/startEvmPolling.js
knock: cd packages/commonwealth && node --import=extensionless/register --max-old-space-size=$(../../scripts/get-max-old-space-size.sh) build/server/workers/knock/knockWorker.js
message-relayer: cd packages/commonwealth && node --import=extensionless/register --max-old-space-size=$(../../scripts/get-max-old-space-size.sh) build/server/workers/messageRelayer/messageRelayer.js
release: cd packages/commonwealth && node --import=extensionless/register --max-old-space-size=$(../../scripts/get-max-old-space-size.sh) build/server/scripts/releasePhaseEnvCheck.js && npx sequelize-cli db:migrate --config server/sequelize.json
release: cd packages/commonwealth && node --import=extensionless/register --max-old-space-size=$(../../scripts/get-max-old-space-size.sh) build/server/scripts/releasePhaseEnvCheck.js && npx sequelize-cli db:migrate --config server/sequelize.json && node --import=extensionless/register build/server/scripts/purgeCloudflareCache.js
kurtisassad marked this conversation as resolved.
Show resolved Hide resolved
22 changes: 22 additions & 0 deletions packages/commonwealth/server/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const {
MESSAGE_RELAYER_TIMEOUT_MS,
MESSAGE_RELAYER_PREFETCH,
EVM_CE_POLL_INTERVAL,
CF_ZONE_ID,
CF_API_KEY,
} = process.env;

const SEND_EMAILS = _SEND_EMAILS === 'true';
Expand Down Expand Up @@ -114,6 +116,10 @@ export const config = configure(
CLIENT_ID: DISCORD_CLIENT_ID,
BOT_TOKEN: DISCORD_BOT_TOKEN,
},
CLOUDFLARE: {
ZONE_ID: CF_ZONE_ID,
API_KEY: CF_API_KEY,
},
WORKERS: {
/*
* NOTE: (1000 / MESSAGE_RELAYER_TIMEOUT_MS) * MESSAGE_RELAYER_PREFETCH = the upperbound
Expand Down Expand Up @@ -229,6 +235,22 @@ export const config = configure(
'DISCORD_BOT_TOKEN is required in production, frick, beta (QA), and demo',
),
}),
CLOUDFLARE: z.object({
ZONE_ID: z
.string()
.optional()
.refine(
(data) => !(['production'].includes(model_config.APP_ENV) && !data),
'CF_ZONE_ID is required in production',
),
API_KEY: z
.string()
.optional()
.refine(
(data) => !(['production'].includes(model_config.APP_ENV) && !data),
'CF_API_KEY is required in production',
),
}),
WORKERS: z.object({
MESSAGE_RELAYER_TIMEOUT_MS: z.number().int().positive(),
MESSAGE_RELAYER_PREFETCH: z.number().int().positive(),
Expand Down
41 changes: 41 additions & 0 deletions packages/commonwealth/server/scripts/purgeCloudflareCache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { logger } from '@hicommonwealth/core';
import fetch from 'node-fetch';
import { config } from '../config';

const log = logger(import.meta);

async function purgeCache(zoneId?: string, apiKey?: string) {
if (!zoneId || !apiKey) {
log.warn('Missing Cloudflare env variables. Skipping cache purge');
return;
rbennettcw marked this conversation as resolved.
Show resolved Hide resolved
}

const url = `https://api.cloudflare.com/client/v4/zones/${zoneId}/purge_cache`;
const body = {
purge_everything: true,
};
const headers = {
Authorization: `Bearer ${apiKey}`,
'Content-Type': 'application/json',
};

try {
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(body),
});

const responseData = await response.json();
log.info('Cache purge request successful:');
log.info(responseData);
} catch (error) {
log.error('Error purging cache:', error.message);
}
}

purgeCache(config.CLOUDFLARE.ZONE_ID, config.CLOUDFLARE.API_KEY)
.then(() => log.info('finished cloudflare purge script'))
.catch((e) => {
log.error('cloudflare purge script failed:', e);
});
rbennettcw marked this conversation as resolved.
Show resolved Hide resolved
6 changes: 5 additions & 1 deletion packages/discord-bot/tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
"rootDir": ".",
"outDir": "build"
},
"include": ["src", "scripts/releasePhaseEnvCheck.ts"],
"include": [
"src",
"scripts/releasePhaseEnvCheck.ts",
"scripts/purgeCloudflareCache.ts"
],
"references": [
{ "path": "../../libs/adapters/tsconfig.build.json" },
{ "path": "../../libs/core/tsconfig.build.json" },
Expand Down
Loading