refactor: persist docker

This commit is contained in:
cha0s 2021-01-24 10:41:04 -06:00
parent d3d4458845
commit 97be0919b7
2 changed files with 30 additions and 16 deletions

View File

@ -1,21 +1,24 @@
import {spawn} from 'child_process';
import {spawn, spawnSync} from 'child_process';
import mkdirp from 'mkdirp';
export default function createDockerContainer(latus) {
const {id} = latus.get('@latus/core');
const {
config: {
'@latus/db/server': {
database,
docker,
password,
port,
},
},
} = latus;
database,
docker,
password,
port,
} = latus.get('@latus/db/server');
const name = `${id}_sequelize`;
const {output} = spawnSync(`docker container inspect -f '{{.State.Running}}' ${name}`);
if ('true' === output) {
return;
}
const args = [
'run',
'--rm',
'--name', name,
'-p', `${port}:3306`,
'-e', `MYSQL_DATABASE=${database}`,
'-e', `MYSQL_ROOT_PASSWORD=${password}`,
@ -32,6 +35,8 @@ export default function createDockerContainer(latus) {
'mysql',
'--default-authentication-plugin=mysql_native_password',
);
const {pid} = spawn('docker', args, {stdio: 'ignore'});
process.on('exit', () => process.kill(pid));
spawn('docker', args, {
detached: true,
stdio: 'ignore',
}).unref();
}

View File

@ -1,6 +1,6 @@
import D from 'debug';
import {spawn} from 'child_process';
import {spawn, spawnSync} from 'child_process';
import mkdirp from 'mkdirp';
@ -9,10 +9,17 @@ import createClient from './create-client';
const debug = D('@latus/redis/docker');
export default async function createDockerContainer(latus) {
const {config: {'@latus/redis/server': {docker, port}}} = latus;
const {id} = latus.get('@latus/core');
const {docker, port} = latus.get('@latus/redis/server');
const name = `${id}_redis`;
const {output} = spawnSync(`docker container inspect -f '{{.State.Running}}' ${name}`);
if ('true' === output) {
return;
}
const args = [
'run',
'--rm',
'--name', name,
'-p', `${port}:6379`,
];
if ('cached' === docker) {
@ -26,8 +33,10 @@ export default async function createDockerContainer(latus) {
args.push(
'redis',
);
const {pid} = spawn('docker', args, {stdio: 'ignore'});
process.on('exit', () => process.kill(pid));
spawn('docker', args, {
detached: true,
stdio: 'ignore',
}).unref();
// eslint-disable-next-line no-constant-condition
while (true) {
const client = createClient(latus);