Skip to content

Commit

Permalink
Refactor Docker container launching code
Browse files Browse the repository at this point in the history
Put subprocess and parent process code in the same file.
  • Loading branch information
lpsinger committed Jul 3, 2024
1 parent 953469d commit 388b4fc
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 90 deletions.
1 change: 0 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import { pathToFileURL } from 'url'
import { launch } from './run.js'
import { populate } from './data.js'
import { search as getSearchClient } from '@nasa-gcn/architect-functions-search'
import './launchSearch'
import {
cloudformationResources as serverlessCloudformationResources,
services as serverlessServices,
Expand Down
21 changes: 16 additions & 5 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
"@types/dockerode": "^3.3.23",
"@types/lodash": "^4.14.202",
"@types/make-fetch-happen": "^10.0.1",
"@types/node": "^18.13.0",
"@types/node": "^18.19.39",
"@types/tar": "^6.1.4",
"@types/unzip-stream": "^0.3.1",
"@typescript-eslint/eslint-plugin": "^5.53.0",
Expand Down
72 changes: 3 additions & 69 deletions run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import { install } from './install.js'
import { mkdtemp } from 'fs/promises'
import { mkdirP, temp } from './paths.js'
import rimraf from 'rimraf'
import { spawn, untilTerminated } from './processes.js'
import type { SandboxEngine } from './engines.js'
import { UnexpectedResolveError, neverResolve } from './promises.js'
import { fork } from 'child_process'
import { fileURLToPath } from 'url'
import { launchBinary } from './runBinary.js'
import { launchDocker } from './runDocker.js'

type SearchEngineLauncherFunction<T = object> = (
export type SearchEngineLauncherFunction<T = object> = (
props: T & {
options: string[]
dataDir: string
Expand All @@ -31,71 +30,6 @@ type SearchEngineLauncherFunction<T = object> = (
waitUntilStopped: () => Promise<void>
}>

const launchBinary: SearchEngineLauncherFunction<{ bin: string }> = async ({
bin,
dataDir,
logsDir,
options,
}) => {
const args = [...options, `path.data=${dataDir}`, `path.logs=${logsDir}`].map(
(opt) => `-E${opt}`
)

console.log('Spawning', bin, ...args)
const child = await spawn(bin, args, {
stdio: ['ignore', 'ignore', 'inherit'],
})

return {
async kill() {
console.log('Killing child process')
child.kill()
},
async waitUntilStopped() {
await untilTerminated(child)
},
}
}

const launchDocker: SearchEngineLauncherFunction = async ({
dataDir,
logsDir,
engine,
port,
options,
}) => {
const argv = {
dataDir,
logsDir,
engine,
port,
options,
}
// FIXME: fork accepts either a string or URL as the first argument. @types/node has defined the modulePath type as string only.
// new URL(import.meta.url) may be used in place of __filename once this has been updated.
// see: https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/69812
const __filename = fileURLToPath(import.meta.url)
const subprocess = fork(__filename, [
'launch-docker-subprocess',
JSON.stringify(argv),
])

return {
async kill() {
console.log('Killing Docker container')
subprocess.send({ action: 'kill' })
},
async waitUntilStopped() {
return new Promise((resolve) => {
subprocess.on('exit', () => {
console.log('Docker container exited')
resolve()
})
})
},
}
}

export async function launch({
port,
engine,
Expand Down
33 changes: 33 additions & 0 deletions runBinary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*!
* Copyright © 2023 United States Government as represented by the
* Administrator of the National Aeronautics and Space Administration.
* All Rights Reserved.
*
* SPDX-License-Identifier: Apache-2.0
*/

import { spawn, untilTerminated } from './processes.js'
import { type SearchEngineLauncherFunction } from './run.js'

export const launchBinary: SearchEngineLauncherFunction<{
bin: string
}> = async ({ bin, dataDir, logsDir, options }) => {
const args = [...options, `path.data=${dataDir}`, `path.logs=${logsDir}`].map(
(opt) => `-E${opt}`
)

console.log('Spawning', bin, ...args)
const child = await spawn(bin, args, {
stdio: ['ignore', 'ignore', 'inherit'],
})

return {
async kill() {
console.log('Killing child process')
child.kill()
},
async waitUntilStopped() {
await untilTerminated(child)
},
}
}
60 changes: 46 additions & 14 deletions launchSearch.ts → runDocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,12 @@
*/

import Dockerode from 'dockerode'
import { fork } from 'child_process'
import { type SearchEngineLauncherFunction } from './run.js'

const [, , command, jsonifiedArgs] = process.argv

if (command === 'launch-docker-subprocess') {
const dockerContainer = await launchDockerSearch()

const signals = ['message', 'SIGTERM', 'SIGINT']
signals.forEach((signal) => {
process.on(signal, async () => {
await dockerContainer.kill()
})
})

await dockerContainer.wait()
}

async function launchDockerSearch() {
const { dataDir, logsDir, engine, port, options } = JSON.parse(jsonifiedArgs)
const Image =
engine === 'elasticsearch'
Expand All @@ -49,5 +38,48 @@ async function launchDockerSearch() {
const stream = await container.attach({ stream: true, stderr: true })
stream.pipe(process.stderr)
await container.start()
return container

const signals = ['message', 'SIGTERM', 'SIGINT']
signals.forEach((signal) => {
process.on(signal, async () => {
await container.kill()
})
})

await container.wait()
}

export const launchDocker: SearchEngineLauncherFunction = async ({
dataDir,
logsDir,
engine,
port,
options,
}) => {
const argv = {
dataDir,
logsDir,
engine,
port,
options,
}
const subprocess = fork(new URL(import.meta.url), [
'launch-docker-subprocess',
JSON.stringify(argv),
])

return {
async kill() {
console.log('Killing Docker container')
subprocess.send({ action: 'kill' })
},
async waitUntilStopped() {
return new Promise((resolve) => {
subprocess.on('exit', () => {
console.log('Docker container exited')
resolve()
})
})
},
}
}

0 comments on commit 388b4fc

Please sign in to comment.