Skip to content

Commit

Permalink
keep around socket
Browse files Browse the repository at this point in the history
  • Loading branch information
mattnibs committed Jan 4, 2024
1 parent 4f7e1e6 commit a0585b6
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 45 deletions.
5 changes: 1 addition & 4 deletions apps/zui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@
"dependencies": {
"keytar": "^7.7.0"
},
"optionalDependencies": {
"node-pipe": "^0.1.1"
},
"devDependencies": {
"@babel/core": "^7.17.9",
"@brimdata/zed-js": "workspace:*",
Expand Down Expand Up @@ -158,7 +155,7 @@
"use-resize-observer": "^8.0.0",
"web-file-polyfill": "^1.0.4",
"web-streams-polyfill": "^3.2.0",
"zed": "brimdata/zed#98d319dcdc7a9a7399d3f4705ef2eecc02d7e8b9",
"zed": "brimdata/zed#f569eb22a55b44e8fd6f2ea7570a1f292adafa45",
"zui-test-data": "workspace:*"
},
"peerDependencies": {
Expand Down
46 changes: 32 additions & 14 deletions packages/zed-node/src/lake.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { ChildProcess, spawn, SpawnOptions } from 'child_process';
import { mkdirpSync } from 'fs-extra';
import { join } from 'path';
import fetch from 'node-fetch';
import { getZedPath } from './binpath';
import { get } from 'http';
import { Socket } from 'net';

type ConstructorOpts = {
root: string;
Expand All @@ -18,6 +19,7 @@ export class Lake {
logs: string;
bin: string;
cors: string[];
stayAliveSocket?: Socket;

constructor(opts: ConstructorOpts) {
this.root = opts.root;
Expand All @@ -31,7 +33,7 @@ export class Lake {
return `localhost:${this.port}`;
}

start() {
async start() {
mkdirpSync(this.root, { mode: 0o755 });
mkdirpSync(this.logs, { mode: 0o755 });

Expand All @@ -45,6 +47,8 @@ export class Lake {
'-log.filemode=rotate',
'-log.path',
join(this.logs, 'zlake.log'),
'-stayalivetimeout',
'5s',
];
for (const origin of this.cors) {
args.push(`--cors.origin=${origin}`);
Expand All @@ -54,26 +58,29 @@ export class Lake {
stdio: ['inherit', 'inherit', 'inherit'],
windowsHide: true,
};
// For unix systems, pass posix pipe read file descriptor into lake process.
// In the event of Zui getting shutdown via `SIGKILL`, this will let lake
// know that it has been orphaned and to shutdown.

if (process.platform !== 'win32') {
// eslint-disable-next-line @typescript-eslint/no-var-requires
const { readfd } = require('node-pipe').pipeSync();
opts.stdio.push(readfd);
args.push(`-brimfd=${opts.stdio.length - 1}`);
}

this.lake = spawn(this.bin, args, opts as SpawnOptions);
this.lake.on('error', (err) => {
console.error('lake spawn error', err);
});

return waitFor(async () => this.isUp());
const ok = await waitFor(async () => this.isUp());
if (ok) {
// Poll the stayalive endpoint for the life of the program. If the
// connection goes away the Zed process know that it has been orphaned and
// will shutdown.
get(`http://${this.addr()}/stayalive`, (res) => {
if (res.statusCode !== 200) {
console.error(`error polling stayalive endpoint: ${res.statusCode} ${res.statusMessage}`)
} else {
this.stayAliveSocket = res.socket
}
})
}
return ok
}

async stop(): Promise<boolean> {
await this.stopPoll()
if (this.lake) {
this.lake.kill('SIGTERM');
return waitFor(() => this.isDown());
Expand All @@ -82,6 +89,17 @@ export class Lake {
}
}

stopPoll() :Promise<void> {
return new Promise(resolve => {
if (this.stayAliveSocket) {
this.stayAliveSocket?.on('close', resolve)
this.stayAliveSocket?.end()
} else {
resolve()
}
})
}

async isUp() {
try {
const response = await fetch(`http://${this.addr()}/status`);
Expand Down
31 changes: 4 additions & 27 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -14264,15 +14264,6 @@ __metadata:
languageName: node
linkType: hard

"node-addon-api@npm:*":
version: 6.1.0
resolution: "node-addon-api@npm:6.1.0"
dependencies:
node-gyp: latest
checksum: 3a539510e677cfa3a833aca5397300e36141aca064cdc487554f2017110709a03a95da937e98c2a14ec3c626af7b2d1b6dabe629a481f9883143d0d5bff07bf2
languageName: node
linkType: hard

"node-addon-api@npm:^1.6.3":
version: 1.7.2
resolution: "node-addon-api@npm:1.7.2"
Expand Down Expand Up @@ -14359,16 +14350,6 @@ __metadata:
languageName: node
linkType: hard

"node-pipe@npm:^0.1.1":
version: 0.1.1
resolution: "node-pipe@npm:0.1.1"
dependencies:
node-addon-api: "*"
node-gyp: latest
conditions: (os=darwin | os=linux)
languageName: node
linkType: hard

"node-releases@npm:^2.0.13":
version: 2.0.13
resolution: "node-releases@npm:2.0.13"
Expand Down Expand Up @@ -19099,10 +19080,10 @@ __metadata:
languageName: node
linkType: hard

"zed@brimdata/zed#98d319dcdc7a9a7399d3f4705ef2eecc02d7e8b9":
"zed@brimdata/zed#f569eb22a55b44e8fd6f2ea7570a1f292adafa45":
version: 0.33.0-dev
resolution: "zed@https://github.com/brimdata/zed.git#commit=98d319dcdc7a9a7399d3f4705ef2eecc02d7e8b9"
checksum: 0352ecf9fa565af22de1d2cc8a515d7df94340c6dfe543f87424d3410a6c07dcb4802a2876311e0677d65e18d7063958d097c239902da28932e8342dadd64e18
resolution: "zed@https://github.com/brimdata/zed.git#commit=f569eb22a55b44e8fd6f2ea7570a1f292adafa45"
checksum: 476f1ca6bfede56154cc2d8e69852aa5db8d2395e6f5ead333bae20898125374ced9dac2e10b1c104ed9e13433b5f4fd950b8c243940b0597a0e94f025b8b877
languageName: node
linkType: hard

Expand Down Expand Up @@ -19261,7 +19242,6 @@ __metadata:
msw: ^0.36.8
next: ^13.3.0
node-fetch: ^2.6.1
node-pipe: ^0.1.1
nodemon: ^2.0.22
npm-run-all: ^4.1.5
ohm-js: ^17.0.4
Expand Down Expand Up @@ -19298,13 +19278,10 @@ __metadata:
use-resize-observer: ^8.0.0
web-file-polyfill: ^1.0.4
web-streams-polyfill: ^3.2.0
zed: "brimdata/zed#98d319dcdc7a9a7399d3f4705ef2eecc02d7e8b9"
zed: "brimdata/zed#f569eb22a55b44e8fd6f2ea7570a1f292adafa45"
zui-test-data: "workspace:*"
peerDependencies:
react: ^18.0.0
react-dom: ^18.0.0
dependenciesMeta:
node-pipe:
optional: true
languageName: unknown
linkType: soft

0 comments on commit a0585b6

Please sign in to comment.