Skip to content

Commit

Permalink
feat: 🎸 Added --only flags to run only some services
Browse files Browse the repository at this point in the history
✅ Closes: NCBD-479
  • Loading branch information
raycar5 committed Oct 7, 2021
1 parent c67601f commit 9e9fcb4
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 17 deletions.
15 changes: 5 additions & 10 deletions src/commands/save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,7 @@ import { Command } from '@oclif/command';
import cli from 'cli-ux';
import { existsSync } from 'fs';

import {
anyContainersUp,
containerTime,
startContainers,
stopContainers,
} from '../common/containers';
import { containersUp, containerTime, startContainers, stopContainers } from '../common/containers';
import { createSnapshot, getMetadata, snapshotPath, writeMetadata } from '../common/snapshots';
import { dataDir, snapshotsDir } from '../consts';
import { noData } from '../errors';
Expand All @@ -33,8 +28,8 @@ export default class Save extends Command {
}

const metadata = getMetadata();
const chainRunning = await anyContainersUp();
if (chainRunning) {
const services = await containersUp();
if (services.length > 0) {
metadata.time = containerTime(metadata);
cli.action.start('Pausing all services');
writeMetadata(metadata);
Expand All @@ -47,9 +42,9 @@ export default class Save extends Command {
createSnapshot(fileName);
cli.action.stop();

if (chainRunning) {
if (services.length > 0) {
cli.action.start('Restarting services');
await startContainers(metadata.version, metadata.time, false, metadata.chain);
await startContainers(metadata.version, metadata.time, false, metadata.chain, services);
metadata.startedAt = new Date().toISOString();
writeMetadata(metadata);
cli.action.stop();
Expand Down
28 changes: 25 additions & 3 deletions src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ export default class Start extends Command {
default: false,
description: 'Cleans state before starting',
}),
only: flags.string({
multiple: true,
default: ['chain', 'subquery', 'gql'],
char: 'o',
description: 'Run only some services',
options: ['chain', 'subquery', 'gql'],
}),
verbose: flags.boolean({
description: 'enables verbose output',
default: false,
Expand All @@ -59,7 +66,8 @@ export default class Start extends Command {

async run(): Promise<void> {
const { flags: commandFlags } = this.parse(Start);
const { clean, snapshot, verbose, version, image, chain } = commandFlags;
const { clean, snapshot, verbose, version, image, chain, only } = commandFlags;
const typedOnly = only as ('chain' | 'subquery' | 'gql')[];

if (await anyContainersUp()) {
this.error(chainRunningError);
Expand Down Expand Up @@ -113,12 +121,26 @@ export default class Start extends Command {
prepareDockerfile(version, image);
cli.action.stop();

const services = typedOnly.flatMap(o => {
switch (o) {
case 'chain':
return ['alice', 'bob', 'charlie'];
case 'subquery':
return ['subquery'];
case 'gql':
return ['tooling'];
}
return [];
});

cli.action.start('Starting the containers');
await startContainers(version, metadata.time, verbose, metadata.chain);
await startContainers(version, metadata.time, verbose, metadata.chain, services);
cli.action.stop();

const allChecks = { chain: isChainUp, subquery: isSubqueryUp, gql: isToolingUp };
const checks = typedOnly.map(o => allChecks[o]);

cli.action.start('Checking service liveness');
const checks = [isChainUp, isToolingUp, isSubqueryUp];
const results = await Promise.all(checks.map(c => retry(c)));
if (!results.every(Boolean)) {
const resultMsgs = checks.map((c, i) => `${c.name}: ${results[i]}`);
Expand Down
20 changes: 16 additions & 4 deletions src/common/containers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ export async function startContainers(
version: string,
timestamp: string,
log: boolean,
chain: string
chain: string,
services: string[]
): Promise<void> {
await compose.upAll({
await compose.upMany(services, {
cwd: localDir,
log,
commandOptions: ['--build'],
Expand All @@ -49,12 +50,23 @@ export async function stopContainers(): Promise<void> {
});
}

export async function anyContainersUp(): Promise<boolean> {
const serviceRegex = /local_(.+)_1/;
export async function containersUp(): Promise<string[]> {
const ps = await compose.ps({
cwd: localDir,
});

return ps.data.services.length > 0;
return ps.data.services.map(s => {
const matches = serviceRegex.exec(s.name);
if (!matches || !matches[1]) {
throw new Error('Invalid docker-compose state');
}
return matches![1];
});
}

export async function anyContainersUp(): Promise<boolean> {
return (await containersUp()).length > 0;
}

/**
Expand Down

0 comments on commit 9e9fcb4

Please sign in to comment.