Skip to content

Commit

Permalink
Merge pull request #8736 from hicommonwealth/ka.cloudflarePurgeCache
Browse files Browse the repository at this point in the history
Added script to automate cloudflare cache purge
  • Loading branch information
kurtisassad authored Aug 12, 2024
2 parents 42d8131 + 4738d56 commit 039418b
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 2 deletions.
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
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;
}

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);
});
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

0 comments on commit 039418b

Please sign in to comment.