@brimdata/zealot
Version:
The Javascript Client for Zed Lakes
128 lines (127 loc) • 3.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "Lake", {
enumerable: true,
get: ()=>Lake
});
const _childProcess = require("child_process");
const _fsExtra = require("fs-extra");
const _path = require("path");
const _paths = require("../cmd/paths");
const _crossFetch = /*#__PURE__*/ _interopRequireDefault(require("cross-fetch"));
function _defineProperty(obj, key, value) {
if (key in obj) {
Object.defineProperty(obj, key, {
value: value,
enumerable: true,
configurable: true,
writable: true
});
} else {
obj[key] = value;
}
return obj;
}
function _interopRequireDefault(obj) {
return obj && obj.__esModule ? obj : {
default: obj
};
}
const zedCommand = (0, _paths.getPath)("zed");
class Lake {
addr() {
return `localhost:${this.port}`;
}
start() {
// @ts-ignore
(0, _fsExtra.mkdirpSync)(this.root, {
recursive: true,
mode: 0o755
});
// @ts-ignore
(0, _fsExtra.mkdirpSync)(this.logs, {
recursive: true,
mode: 0o755
});
const args = [
"serve",
"-l",
this.addr(),
"-lake",
this.root,
"-log.level=info",
"-log.filemode=rotate",
"-log.path",
(0, _path.join)(this.logs, "zlake.log")
];
const opts = {
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}`);
}
// @ts-ignore
this.lake = (0, _childProcess.spawn)(this.bin, args, opts);
this.lake.on("error", (err)=>{
console.error("lake spawn error", err);
});
return waitFor(async ()=>this.isUp());
}
async stop() {
if (this.lake) {
this.lake.kill("SIGTERM");
return waitFor(()=>this.isDown());
} else {
return true;
}
}
async isUp() {
try {
const response = await (0, _crossFetch.default)(`http://${this.addr()}/status`);
const text = await response.text();
return text === "ok";
} catch (e) {
return false;
}
}
async isDown() {
return !await this.isUp();
}
constructor(opts){
_defineProperty(this, "lake", void 0);
_defineProperty(this, "root", void 0);
_defineProperty(this, "port", void 0);
_defineProperty(this, "logs", void 0);
_defineProperty(this, "bin", void 0);
this.root = opts.root;
this.logs = opts.logs;
this.port = opts.port || 9867;
this.bin = opts.bin || zedCommand;
}
}
async function waitFor(condition) {
let giveUp = false;
const id = setTimeout(()=>{
giveUp = true;
}, 5000);
while(!giveUp){
if (await condition()) break;
await sleep(50);
}
clearTimeout(id);
return !giveUp;
}
const sleep = (ms)=>new Promise((r)=>setTimeout(r, ms));