Skip to content
This repository has been archived by the owner on Aug 19, 2020. It is now read-only.

RedisServerHelper #33

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open

Conversation

djpalumbo
Copy link

See issue #29.

@@ -28,8 +28,8 @@ describe('run-command', () => {
});

it('executes the redis command', async () => {
await runCommand('redis-command', ['args1', 'args2']);
await runCommand('redis-command', ['args1', 'args2'], { opt: true });
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is opt? I can't find any documentation on it.

@@ -2,7 +2,7 @@ import execa from 'execa';
import redisDownload from 'redis-download';
import { homedir } from 'os';

export default async function runCommand(command, args) {
export default async function runCommand(command, args, opts) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
export default async function runCommand(command, args, opts) {
export default async function runCommand(command, args, { stdio = 'inherit', detached = false } = {}) {

This way you don't have to sprinkle { studio: 'inherit' } everywhere.

@@ -15,5 +15,5 @@ export default async function runCommand(command, args) {
version,
});

return execa(`${directory}/src/${command}`, args, { stdio: 'inherit' });
return { childProcess: execa(`${directory}/src/${command}`, args, opts) };
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return { childProcess: execa(`${directory}/src/${command}`, args, opts) };
return execa(`${directory}/src/${command}`, args, { stdio, detached });

I'd prefer to control the options explicitly rather than allowing the caller to pass it anything.

Also, all of the CLI commands (redis-benchmark, redis-server, etc) expect this to have execas return value, aka a promise), so they'll all break. I don't currently have tests for those files, which is why it isn't currently failing, but they should be.

}
}
return true;
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather this less nested:

function containsValidPort(args) {
	const flag = args.indexOf('--port');
	if (flag < 0 || flag + 1 >= args.length) {
		return true;
	}
	const port = args[flag + 1];
	return typeof port === 'number' && port >= 0 && port <= 65535;
}

}

export class RedisServerHelper {
constructor(args, opts) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
constructor(args, opts) {
constructor(args = [], { downloadDir, version } = {}) {

This lets us delete most of the args and opts validation.

throw new Error('Invalid key in opts');
}

if ('downloadDir' in opts) process.env.REDIS_DOWNLOADDIR = opts.downloadDir;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ('downloadDir' in opts) process.env.REDIS_DOWNLOADDIR = opts.downloadDir;
if (downloadDir) process.env.REDIS_DOWNLOADDIR =downloadDir;

}

if ('downloadDir' in opts) process.env.REDIS_DOWNLOADDIR = opts.downloadDir;
if ('version' in opts) process.env.REDIS_VERSION = opts.version;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if ('version' in opts) process.env.REDIS_VERSION = opts.version;
if (version) process.env.REDIS_VERSION = version;

if (ready) return true;

return waitUntilReady(tries - 1, interval * backoff, backoff, args);
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather an iterative approach over a recursive one:

async function waitUntilReady(tries, interval, backoff, args) {
	for (let i = 0; i < tries; i++) {
		await wait(interval);

		if (await checkIfReady(args)) {
			return true;
		}
	}

	throw new Error('Timed out');
}

@@ -0,0 +1,3 @@
export default async function wait(interval) {
return new Promise((resolve) => setTimeout(resolve, interval));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather this inline instead of as a separate file, since it's a one liner and it's only used in one place.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants