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 Jun 25, 2024
1 parent eb4de76 commit b66ab7f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 83 deletions.
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)
},
}
}
65 changes: 51 additions & 14 deletions launchSearch.ts → runDocker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,13 @@
*/

import Dockerode from 'dockerode'
import { fork } from 'child_process'
import { fileURLToPath } from 'url'
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 +39,52 @@ 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,
}
// 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()
})
})
},
}
}

0 comments on commit b66ab7f

Please sign in to comment.