Skip to content

Commit

Permalink
#581 call adjacent-servers from server.js
Browse files Browse the repository at this point in the history
  • Loading branch information
tariqksoliman committed Nov 1, 2024
1 parent 17f0bd9 commit ceb1b1f
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 334 deletions.
161 changes: 77 additions & 84 deletions adjacent-servers/adjacent-servers.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,98 +2,91 @@ require("dotenv").config();
const logger = require("../API/logger");
const { spawn } = require("child_process");

const IS_WINDOWS = /^win/i.test(process.platform) ? true : false;
const EXT = IS_WINDOWS ? ".bat" : ".sh";
const CMD = IS_WINDOWS ? "" : "sh ";
function adjacentServers() {
const IS_WINDOWS = /^win/i.test(process.platform) ? true : false;
const EXT = IS_WINDOWS ? ".bat" : ".sh";
const CMD = IS_WINDOWS ? "" : "sh ";

const adjacentServers = [
{
name: "stac",
command: `cd adjacent-servers/stac/ && ${CMD}start-stac${EXT}`,
port: process.env.STAC_PORT || 8881,
env: "WITH_STAC",
},
{
name: "tipg",
command: `cd adjacent-servers/tipg/ && ${CMD}start-tipg${EXT}`,
port: process.env.TIPG_PORT || 8882,
env: "WITH_TIPG",
},
{
name: "titiler",
command: `cd adjacent-servers/titiler/ && ${CMD}start-titiler${EXT}`,
port: process.env.TITILER_PORT || 8883,
env: "WITH_TITILER",
},
{
name: "titiler-pgstac",
command: `cd adjacent-servers/titiler-pgstac/ && ${CMD}start-titiler-pgstac${EXT}`,
port: process.env.TITILER_PGSTAC_PORT || 8884,
env: "WITH_TITILER_PGSTAC",
},
];
const adjacentServers = [
{
name: "stac",
command: `cd adjacent-servers/stac/ && ${CMD}start-stac${EXT}`,
port: process.env.STAC_PORT || 8881,
env: "WITH_STAC",
},
{
name: "tipg",
command: `cd adjacent-servers/tipg/ && ${CMD}start-tipg${EXT}`,
port: process.env.TIPG_PORT || 8882,
env: "WITH_TIPG",
},
{
name: "titiler",
command: `cd adjacent-servers/titiler/ && ${CMD}start-titiler${EXT}`,
port: process.env.TITILER_PORT || 8883,
env: "WITH_TITILER",
},
{
name: "titiler-pgstac",
command: `cd adjacent-servers/titiler-pgstac/ && ${CMD}start-titiler-pgstac${EXT}`,
port: process.env.TITILER_PGSTAC_PORT || 8884,
env: "WITH_TITILER_PGSTAC",
},
];

const serverProcesses = [];
adjacentServers.forEach((s) => {
if (process.env[s.env] === "true") {
logger(
"info",
`Starting ${s.name} server on port ${s.port}...`,
"adjacent-servers"
);
let cmd = spawn(s.command, [s.port], { shell: true });
serverProcesses.push(cmd);
const serverProcesses = [];
adjacentServers.forEach((s) => {
if (process.env[s.env] === "true") {
logger(
"info",
`Starting ${s.name} server on port ${s.port}...`,
"adjacent-servers"
);
let cmd = spawn(s.command, [s.port], { shell: true });
serverProcesses.push(cmd);

cmd.stdout.on("data", (data) => {
if (`${data}`.indexOf("running") != -1)
process.stdout.write(`${s.name} - ${data}`);
});
cmd.stdout.on("data", (data) => {
if (`${data}`.indexOf("running") != -1)
process.stdout.write(`${s.name} - ${data}`);
});

cmd.stderr.on("data", (data) => {
if (`${data}`.indexOf("running") != -1)
process.stdout.write(`${s.name} - ${data}`);
});
cmd.stderr.on("data", (data) => {
if (`${data}`.indexOf("running") != -1)
process.stdout.write(`${s.name} - ${data}`);
});

cmd.on("error", (err) => {
process.stdout.write(`${s.name} ERROR - ${err}`);
cmd.on("error", (err) => {
process.stdout.write(`${s.name} ERROR - ${err}`);
});
}
});

function killProcess() {
logger("info", `Terminating adjacent servers...`, "adjacent-servers");
serverProcesses.forEach((process) => {
// Concurrently actually handles this
// I suspect killing here, makes concurrently hang because
// it's the one who wants to kill them MAYBE!?
process.kill("SIGKILL");
});
logger(
"success",
`Successfully terminated all adjacent servers.`,
"adjacent-servers"
);
}
});

let running = true;

function killProcess() {
logger("info", `Terminating adjacent servers...`, "adjacent-servers");
serverProcesses.forEach((process) => {
// Concurrently actually handles this
// I suspect killing here, makes concurrently hang because
// it's the one who wants to kill them MAYBE!?
process.kill("SIGKILL");
process.on("SIGTERM", killProcess);
process.on("SIGINT", killProcess);
process.on("SIGKILL", killProcess);
process.on("uncaughtException", function (e) {
logger(
"error",
`[uncaughtException] app will be terminated: ${e.stack}`,
"adjacent-servers"
);
killProcess();
});
logger(
"success",
`Successfully terminated all adjacent servers.`,
"adjacent-servers"
);
running = false;
}

process.on("SIGTERM", killProcess);
process.on("SIGINT", killProcess);
process.on("SIGKILL", killProcess);
process.on("uncaughtException", function (e) {
logger(
"error",
`[uncaughtException] app will be terminated: ${e.stack}`,
"adjacent-servers"
);
killProcess();
});

function run() {
setTimeout(function () {
if (running) run();
}, 1000);
}

run();
module.exports = adjacentServers;
16 changes: 8 additions & 8 deletions configure/src/pages/APIs/APIs.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const apiCards = {
subtitle: "FastAPI implementation of the STAC API spec.",
description: "",
link: `${window.location.pathname
.replace(`configure-beta`, "")
.replace(/^\//g, "")}stac/api.html`,
.replace(`${window.mmgisglobal.ROOT_PATH || ""}/configure-beta`, "")
.replace(/^\//g, "")}/stac/api.html`,
active: window.mmgisglobal.WITH_STAC === "true",
},
TiTiler: {
Expand All @@ -56,8 +56,8 @@ const apiCards = {
"A modern dynamic tile server built on top of FastAPI and Rasterio/GDAL.",
description: "",
link: `${window.location.pathname
.replace(`configure-beta`, "")
.replace(/^\//g, "")}titiler/api.html`,
.replace(`${window.mmgisglobal.ROOT_PATH || ""}/configure-beta`, "")
.replace(/^\//g, "")}/titiler/api.html`,
active: window.mmgisglobal.WITH_TITILER === "true",
},
"TiTiler-PgSTAC": {
Expand All @@ -66,8 +66,8 @@ const apiCards = {
"TiTiler-PgSTAC is a TiTiler extension that connects to a PgSTAC database to create dynamic mosaics based on search queries.",
description: "",
link: `${window.location.pathname
.replace(`configure-beta`, "")
.replace(/^\//g, "")}titilerpgstac/api.html`,
.replace(`${window.mmgisglobal.ROOT_PATH || ""}/configure-beta`, "")
.replace(/^\//g, "")}/titilerpgstac/api.html`,
active: window.mmgisglobal.WITH_TITILER_PGSTAC === "true",
},
TiPg: {
Expand All @@ -76,8 +76,8 @@ const apiCards = {
"Simple and Fast Geospatial OGC Features and Tiles API for PostGIS.",
description: "",
link: `${window.location.pathname
.replace(`configure-beta`, "")
.replace(/^\//g, "")}tipg/api.html`,
.replace(`${window.mmgisglobal.ROOT_PATH || ""}/configure-beta`, "")
.replace(/^\//g, "")}/tipg/api.html`,
active: window.mmgisglobal.WITH_TIPG === "true",
},
};
Expand Down
Loading

0 comments on commit ceb1b1f

Please sign in to comment.