Skip to content

Commit

Permalink
changed the updating of the config file to happen on stop to preven…
Browse files Browse the repository at this point in the history
…t reload from failing due to port changes from -1 to actually assigned port.
  • Loading branch information
aricart committed Dec 4, 2023
1 parent 37f5bd6 commit dc57586
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 17 deletions.
58 changes: 52 additions & 6 deletions tests/helpers/cluster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ import { rgb24 } from "https://deno.land/[email protected]/fmt/colors.ts";
const defaults = {
c: 3,
p: 4222,
w: 80,
chaos: false,
cert: "",
key: "",
};

const argv = parse(
Expand All @@ -31,6 +34,7 @@ const argv = parse(
"c": ["count"],
"d": ["debug"],
"j": ["jetstream"],
"w": ["websocket"],
},
default: defaults,
boolean: ["debug", "jetstream", "server-debug", "chaos"],
Expand All @@ -39,18 +43,60 @@ const argv = parse(

if (argv.h || argv.help) {
console.log(
"usage: cluster [--count 3] [--port 4222] [--debug] [--jetstream] [--chaos]\n",
"usage: cluster [--count 3] [--port 4222] [--key path] [--cert path] [--websocket 80] [--debug] [--jetstream] [--chaos]\n",
);
Deno.exit(0);
}

const count = argv["count"] as number || 3;
const port = argv["port"] as number ?? 4222;
const cert = argv["cert"] as string || undefined;
const key = argv["key"] as string || undefined;

if (cert?.length) {
await Deno.stat(cert).catch((err) => {
console.error(`error loading certificate: ${err.message}`);
Deno.exit(1);
});
}
if (key?.length) {
await Deno.stat(key).catch((err) => {
console.error(`error loading certificate key: ${err.message}`);
Deno.exit(1);
});
}
let wsport = argv["websocket"] as number;
if (wsport === 80 && cert?.length) {
wsport = 443;
}

let chaosTimer: number | undefined;
let cluster: NatsServer[];

try {
const base = { debug: false };
const base = {
debug: false,
tls: {},
websocket: {
port: wsport,
no_tls: true,
compression: true,
tls: {},
},
};

if (cert) {
base.tls = {
cert_file: cert,
key_file: key,
};
base.websocket.no_tls = false;
base.websocket.tls = {
cert_file: cert,
key_file: key,
};
}

const serverDebug = argv["debug"];
if (serverDebug) {
base.debug = true;
Expand All @@ -76,12 +122,12 @@ try {

cluster.forEach((s) => {
const pid = rgb24(`[${s.process.pid}]`, s.rgb);
console.log(
`${pid} ${s.configFile} at nats://${s.hostname}:${s.port}
console.log(`${pid} config ${s.configFile}
\tnats://${s.hostname}:${s.port}
\tws${cert ? "s" : ""}://${s.hostname}:${s.websocket}
\tcluster://${s.hostname}:${s.cluster}
\thttp://127.0.0.1:${s.monitoring}
\tstore: ${s.config?.jetstream?.store_dir}`,
);
${argv.jetstream ? `\tstore: ${s.config?.jetstream?.store_dir}` : ""}`);
});

if (argv.chaos === true && confirm("start chaos?")) {
Expand Down
43 changes: 32 additions & 11 deletions tests/helpers/launcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ export interface JSZ {
};
}

function parseHostport(s?: string) {
function parseHostport(
s?: string,
): { hostname: string; port: number } | undefined {
if (!s) {
return;
}
Expand Down Expand Up @@ -192,12 +194,27 @@ export class NatsServer implements PortInfo {
})();
}

restart(): Promise<NatsServer> {
return this.stop().then(() => {
const conf = JSON.parse(JSON.stringify(this.config));
conf.port = this.port;
return NatsServer.start(conf, this.debug);
});
updatePorts(): Promise<void> {
// if we have -1 ports, lets freeze them
this.config.port = this.port;
if (this.cluster) {
this.config.cluster.listen = `${this.hostname}:${this.cluster}`;
this.config.cluster.name = this.clusterName;
}
if (this.monitoring) {
this.config.http = this.monitoring;
}
if (this.websocket) {
this.config.websocket = this.config.websocket || {};
this.config.websocket.port = this.websocket;
}
return Deno.writeFile(this.configFile, new TextEncoder().encode(toConf(this.config)));
}

async restart(): Promise<NatsServer> {
await this.stop();
const conf = JSON.parse(JSON.stringify(this.config));
return await NatsServer.start(conf, this.debug);
}

pid(): number {
Expand All @@ -219,6 +236,7 @@ export class NatsServer implements PortInfo {

async stop(): Promise<void> {
if (!this.stopped) {
await this.updatePorts();
this.stopped = true;
this.process.stderr?.cancel().catch(() => {});
this.process.kill("SIGKILL");
Expand Down Expand Up @@ -427,7 +445,7 @@ export class NatsServer implements PortInfo {
conf = Object.assign({}, conf);
conf.cluster = conf.cluster || {};
conf.cluster.name = nuid.next();
conf.cluster.listen = conf.cluster.listen || "127.0.0.1:-1";
conf.cluster.listen = conf.cluster.listen || "127.0.0.1:4225";

const ns = await NatsServer.start(conf, debug);
const cluster = [ns];
Expand Down Expand Up @@ -489,10 +507,13 @@ export class NatsServer implements PortInfo {
conf = conf || {};
conf = JSON.parse(JSON.stringify(conf));
conf.port = -1;
if (conf.websocket) {
conf.websocket.port = -1;
}
conf.http = "127.0.0.1:-1";
conf.cluster = conf.cluster || {};
conf.cluster.name = ns.clusterName;
conf.cluster.listen = conf.cluster.listen || "127.0.0.1:-1";
conf.cluster.listen = "127.0.0.1:-1";
conf.cluster.routes = [`nats://${ns.hostname}:${ns.cluster}`];

return NatsServer.start(conf, debug);
Expand All @@ -513,7 +534,6 @@ export class NatsServer implements PortInfo {
/**
* this is only expecting authentication type changes
* @param conf
* @param debug
*/
async reload(conf?: any): Promise<void> {
conf = NatsServer.confDefaults(conf);
Expand All @@ -535,7 +555,7 @@ export class NatsServer implements PortInfo {
conf = NatsServer.confDefaults(conf);
conf.ports_file_dir = tmp;

const confFile = await Deno.makeTempFileSync();
const confFile = Deno.makeTempFileSync();
await Deno.writeFile(confFile, new TextEncoder().encode(toConf(conf)));
if (debug) {
console.info(`${exe} -c ${confFile}`);
Expand Down Expand Up @@ -584,6 +604,7 @@ export class NatsServer implements PortInfo {
if (conf.cluster?.name) {
ports.clusterName = conf.cluster.name;
}

try {
await check(
async () => {
Expand Down

0 comments on commit dc57586

Please sign in to comment.