Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add forked docker process #39

Merged
merged 17 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ 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
54 changes: 54 additions & 0 deletions launchSearch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*!
* 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 Dockerode from 'dockerode'

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()
process.exit(0)
lpsinger marked this conversation as resolved.
Show resolved Hide resolved
})
})

await dockerContainer.wait()
}

export async function launchDockerSearch() {
const { dataDir, logsDir, engine, port, options } = JSON.parse(jsonifiedArgs)
const Image =
engine === 'elasticsearch'
? 'elastic/elasticsearch:8.6.2'
: 'opensearchproject/opensearch:2.11.0'
console.log('Launching Docker container', Image)
const docker = new Dockerode()

const container = await docker.createContainer({
Env: [...options, 'path.data=/var/lib/search', 'path.logs=/var/log/search'],
HostConfig: {
AutoRemove: true,
Mounts: [
{ Source: dataDir, Target: '/var/lib/search', Type: 'bind' },
{ Source: logsDir, Target: '/var/log/search', Type: 'bind' },
],
PortBindings: {
[`${port}/tcp`]: [{ HostIP: '127.0.0.1', HostPort: `${port}` }],
},
},
Image,
})
const stream = await container.attach({ stream: true, stderr: true })
stream.pipe(process.stderr)
await container.start()
return container
lpsinger marked this conversation as resolved.
Show resolved Hide resolved
}
50 changes: 24 additions & 26 deletions run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ import { mkdirP, temp } from './paths.js'
import rimraf from 'rimraf'
import { spawn, untilTerminated } from './processes.js'
import type { SandboxEngine } from './engines.js'
import Dockerode from 'dockerode'
import { UnexpectedResolveError, neverResolve } from './promises.js'
import { fork } from 'child_process'
import { fileURLToPath } from 'url'

type SearchEngineLauncherFunction<T = object> = (
props: T & {
Expand Down Expand Up @@ -63,37 +64,34 @@ const launchDocker: SearchEngineLauncherFunction = async ({
port,
options,
}) => {
const Image =
engine === 'elasticsearch'
? 'elastic/elasticsearch:8.6.2'
: 'opensearchproject/opensearch:2.11.0'
console.log('Launching Docker container', Image)
const docker = new Dockerode()
const container = await docker.createContainer({
Env: [...options, 'path.data=/var/lib/search', 'path.logs=/var/log/search'],
HostConfig: {
AutoRemove: true,
Mounts: [
{ Source: dataDir, Target: '/var/lib/search', Type: 'bind' },
{ Source: logsDir, Target: '/var/log/search', Type: 'bind' },
],
PortBindings: {
[`${port}/tcp`]: [{ HostIP: '127.0.0.1', HostPort: `${port}` }],
},
},
Image,
})
const stream = await container.attach({ stream: true, stderr: true })
stream.pipe(process.stderr)
await container.start()
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')
await container.kill()
subprocess.send({ action: 'kill' })
Courey marked this conversation as resolved.
Show resolved Hide resolved
},
async waitUntilStopped() {
await container.wait()
return new Promise<void>((resolve) => {
lpsinger marked this conversation as resolved.
Show resolved Hide resolved
subprocess.on('exit', () => {
console.log('Docker container exited')
resolve()
})
})
},
}
}
Expand Down
4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"extends": "@tsconfig/node14/tsconfig.json",
"compilerOptions": {
"resolveJsonModule": true
"resolveJsonModule": true,
"module": "esnext",
"target": "es2022"
}
}
Loading