Skip to content

Commit

Permalink
Merge branch 'trs/redis-assertions'
Browse files Browse the repository at this point in the history
  • Loading branch information
tsibley committed Oct 18, 2023
2 parents 9d7667f + e40645e commit c481847
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ const __basedir = path.join(__dirname, "..");
export const PRODUCTION = process.env.NODE_ENV === "production";


/**
* Running as a Heroku Review app?
*
* These run in production mode but do not have access to sensitive information
* (private S3 buckets, production Redis, production encryption keys, etc).
*
* See {@link https://devcenter.heroku.com/articles/github-integration-review-apps#injected-environment-variables}.
*
* @type {boolean}
*/
export const REVIEW_APP = !!process.env.HEROKU_PR_NUMBER;


/**
* Path to a JSON file containing configuration values.
*
Expand Down
35 changes: 35 additions & 0 deletions src/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import Redis from 'ioredis';

import { PRODUCTION, REVIEW_APP } from './config.js';
import * as utils from './utils/index.js';


Expand All @@ -32,6 +33,40 @@ const REDIS = process.env.REDIS_URL
? herokuRedisClient(process.env.REDIS_URL)
: null;

/* XXX TODO: Provision lowest-tier Heroku Redis addon for review apps and
* remove the !REVIEW_APP condition.
*
* Heroku has good support for this, but to enable it I think we need to switch
* how we configure review apps. Currently they're configured via the Heroku
* Dashboard¹. I think we need to switch to the app.json file² instead, as
* described in the review app documentation.³
* -trs, 12 Oct 2023
*
* ¹ <https://dashboard.heroku.com/pipelines/38f67fc7-d93c-40c6-a182-501da2f89d9d/settings>
* ² <https://devcenter.heroku.com/articles/app-json-schema>
* ³ <https://devcenter.heroku.com/articles/github-integration-review-apps>
*/
if (PRODUCTION && !REVIEW_APP && !REDIS) {
throw new Error("REDIS_URL required in production mode");
}

if (REDIS) {
/* Use "info memory" command instead of "config get maxmemory-policy" since
* Heroku Redis disables admin commands, which includes "config".
*/
const memoryInfo = new Map(
(await REDIS.info("memory"))
.split(/\r\n/)
.map(line => line.match(/^(?<key>[^:]+):(?<value>.*)$/)?.groups)
.filter(matched => matched)
.map(({key, value}) => [key.replace(/_/g, "-"), value])
);
const maxmemoryPolicy = memoryInfo.get("maxmemory-policy");
if (maxmemoryPolicy !== "volatile-ttl") {
throw new Error(`Redis maxmemory-policy is "${maxmemoryPolicy}", but it *must* be "volatile-ttl".`);
}
}


function herokuRedisClient(urlStr) {
const url = new URL(urlStr);
Expand Down

0 comments on commit c481847

Please sign in to comment.