2023-01-13 05:40:33 +01:00
|
|
|
import * as fs from "node:fs";
|
|
|
|
import { fileURLToPath } from "node:url";
|
|
|
|
import { dirname } from "node:path";
|
|
|
|
import * as os from "node:os";
|
|
|
|
import cluster from "node:cluster";
|
|
|
|
import chalk from "chalk";
|
|
|
|
import chalkTemplate from "chalk-template";
|
|
|
|
import semver from "semver";
|
|
|
|
|
|
|
|
import Logger from "@/services/logger.js";
|
|
|
|
import loadConfig from "@/config/load.js";
|
|
|
|
import type { Config } from "@/config/types.js";
|
|
|
|
import { lessThan } from "@/prelude/array.js";
|
|
|
|
import { envOption } from "../env.js";
|
|
|
|
import { showMachineInfo } from "@/misc/show-machine-info.js";
|
|
|
|
import { db, initDb } from "../db/postgre.js";
|
2021-08-20 14:34:56 +02:00
|
|
|
|
2022-02-27 03:07:39 +01:00
|
|
|
const _filename = fileURLToPath(import.meta.url);
|
2021-08-20 14:34:56 +02:00
|
|
|
const _dirname = dirname(_filename);
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const meta = JSON.parse(
|
|
|
|
fs.readFileSync(`${_dirname}/../../../../built/meta.json`, "utf-8"),
|
|
|
|
);
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const logger = new Logger("core", "cyan");
|
|
|
|
const bootLogger = logger.createSubLogger("boot", "magenta", false);
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const themeColor = chalk.hex("#31748f");
|
2022-02-27 03:07:39 +01:00
|
|
|
|
2019-11-24 09:11:53 +01:00
|
|
|
function greet() {
|
2021-10-08 14:24:05 +02:00
|
|
|
if (!envOption.quiet) {
|
2023-07-03 00:17:58 +02:00
|
|
|
//#region Firefish logo
|
2019-11-24 09:11:53 +01:00
|
|
|
const v = `v${meta.version}`;
|
2023-07-03 00:18:30 +02:00
|
|
|
console.log(themeColor(" ▄▄▄▄▄▄▄ ▄▄▄ ▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄▄▄▄▄▄▄ ▄▄▄ ▄▄▄▄▄▄▄ ▄▄ ▄▄ ◯ "));
|
|
|
|
console.log(themeColor("█ █ █ ▄ █ █ █ █ █ █ █ █ █ ○ ▄ ▄"));
|
|
|
|
console.log(themeColor("█ ▄▄▄█ █ █ █ █ █ ▄▄▄█ ▄▄▄█ █ ▄▄▄▄▄█ █▄█ █ ⚬ █▄▄ █▄▄ "));
|
|
|
|
console.log(themeColor("█ █▄▄▄█ █ █▄▄█▄█ █▄▄▄█ █▄▄▄█ █ █▄▄▄▄▄█ █ ▄▄▄▄▄▄ ▄"));
|
|
|
|
console.log(themeColor("█ ▄▄▄█ █ ▄▄ █ ▄▄▄█ ▄▄▄█ █▄▄▄▄▄ █ ▄ █ █ █ █▄▄"));
|
|
|
|
console.log(themeColor("█ █ █ █ █ █ █ █▄▄▄█ █ █ █▄▄▄▄▄█ █ █ █ █ █ ● ● █"));
|
|
|
|
console.log(themeColor("█▄▄▄█ █▄▄▄█▄▄▄█ █▄█▄▄▄▄▄▄▄█▄▄▄█ █▄▄▄█▄▄▄▄▄▄▄█▄▄█ █▄▄█ ▀▄▄▄▄▄▄▀"));
|
2019-04-07 14:50:36 +02:00
|
|
|
//#endregion
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
console.log(
|
2023-07-03 00:17:58 +02:00
|
|
|
" Firefish is an open-source decentralized microblogging platform.",
|
2023-01-13 05:40:33 +01:00
|
|
|
);
|
|
|
|
console.log(
|
|
|
|
chalk.rgb(
|
|
|
|
255,
|
|
|
|
136,
|
|
|
|
0,
|
|
|
|
)(
|
2023-07-19 06:21:48 +02:00
|
|
|
" If you like Firefish, please consider starring or contributing to the repo. https://gitlab.prometheus.systems/firefish/firefish",
|
2023-01-13 05:40:33 +01:00
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
console.log("");
|
|
|
|
console.log(
|
|
|
|
chalkTemplate`--- ${os.hostname()} {gray (PID: ${process.pid.toString()})} ---`,
|
|
|
|
);
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
|
2023-07-03 00:17:58 +02:00
|
|
|
bootLogger.info("Welcome to Firefish!");
|
|
|
|
bootLogger.info(`Firefish v${meta.version}`, null, true);
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init master process
|
|
|
|
*/
|
|
|
|
export async function masterMain() {
|
2019-04-12 18:43:22 +02:00
|
|
|
let config!: Config;
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2021-04-24 15:55:18 +02:00
|
|
|
// initialize app
|
2019-04-07 14:50:36 +02:00
|
|
|
try {
|
2019-11-24 09:11:53 +01:00
|
|
|
greet();
|
2021-04-24 15:55:18 +02:00
|
|
|
showEnvironment();
|
|
|
|
await showMachineInfo(bootLogger);
|
|
|
|
showNodejsVersion();
|
|
|
|
config = loadConfigBoot();
|
|
|
|
await connectDb();
|
2019-04-07 14:50:36 +02:00
|
|
|
} catch (e) {
|
2023-01-13 05:40:33 +01:00
|
|
|
bootLogger.error("Fatal error occurred during initialization", null, true);
|
2019-04-07 14:50:36 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
|
2023-07-03 00:17:58 +02:00
|
|
|
bootLogger.succ("Firefish initialized");
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2021-10-08 14:24:05 +02:00
|
|
|
if (!envOption.disableClustering) {
|
2019-04-07 14:50:36 +02:00
|
|
|
await spawnWorkers(config.clusterLimit);
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
bootLogger.succ(
|
|
|
|
`Now listening on port ${config.port} on ${config.url}`,
|
|
|
|
null,
|
|
|
|
true,
|
|
|
|
);
|
2020-04-26 04:39:15 +02:00
|
|
|
|
2023-06-06 06:15:49 +02:00
|
|
|
if (!envOption.noDaemons && !config.onlyQueueProcessor) {
|
2023-01-13 05:40:33 +01:00
|
|
|
import("../daemons/server-stats.js").then((x) => x.default());
|
|
|
|
import("../daemons/queue-stats.js").then((x) => x.default());
|
|
|
|
import("../daemons/janitor.js").then((x) => x.default());
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function showEnvironment(): void {
|
|
|
|
const env = process.env.NODE_ENV;
|
2023-01-13 05:40:33 +01:00
|
|
|
const logger = bootLogger.createSubLogger("env");
|
|
|
|
logger.info(
|
|
|
|
typeof env === "undefined" ? "NODE_ENV is not set" : `NODE_ENV: ${env}`,
|
|
|
|
);
|
|
|
|
|
|
|
|
if (env !== "production") {
|
|
|
|
logger.warn("The environment is not in production mode.");
|
|
|
|
logger.warn("DO NOT USE FOR PRODUCTION PURPOSE!", null, true);
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-24 15:55:18 +02:00
|
|
|
function showNodejsVersion(): void {
|
2023-01-13 05:40:33 +01:00
|
|
|
const nodejsLogger = bootLogger.createSubLogger("nodejs");
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2022-03-19 17:34:45 +01:00
|
|
|
nodejsLogger.info(`Version ${process.version} detected.`);
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
const minVersion = fs
|
|
|
|
.readFileSync(`${_dirname}/../../../../.node-version`, "utf-8")
|
|
|
|
.trim();
|
2022-03-19 17:34:45 +01:00
|
|
|
if (semver.lt(process.version, minVersion)) {
|
|
|
|
nodejsLogger.error(`At least Node.js ${minVersion} required!`);
|
2019-04-07 14:50:36 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2021-04-24 15:55:18 +02:00
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2021-04-24 15:55:18 +02:00
|
|
|
function loadConfigBoot(): Config {
|
2023-01-13 05:40:33 +01:00
|
|
|
const configLogger = bootLogger.createSubLogger("config");
|
2019-04-07 14:50:36 +02:00
|
|
|
let config;
|
|
|
|
|
|
|
|
try {
|
|
|
|
config = loadConfig();
|
|
|
|
} catch (exception) {
|
2023-01-13 05:40:33 +01:00
|
|
|
if (exception.code === "ENOENT") {
|
|
|
|
configLogger.error("Configuration file not found", null, true);
|
2019-04-07 14:50:36 +02:00
|
|
|
process.exit(1);
|
2022-08-04 11:00:02 +02:00
|
|
|
} else if (e instanceof Error) {
|
|
|
|
configLogger.error(e.message);
|
|
|
|
process.exit(1);
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
throw exception;
|
|
|
|
}
|
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
configLogger.succ("Loaded");
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2021-04-24 15:55:18 +02:00
|
|
|
return config;
|
|
|
|
}
|
|
|
|
|
|
|
|
async function connectDb(): Promise<void> {
|
2023-01-13 05:40:33 +01:00
|
|
|
const dbLogger = bootLogger.createSubLogger("db");
|
2020-04-26 04:39:15 +02:00
|
|
|
|
2019-04-07 14:50:36 +02:00
|
|
|
// Try to connect to DB
|
|
|
|
try {
|
2023-01-13 05:40:33 +01:00
|
|
|
dbLogger.info("Connecting...");
|
2019-04-07 14:50:36 +02:00
|
|
|
await initDb();
|
2023-01-13 05:40:33 +01:00
|
|
|
const v = await db
|
|
|
|
.query("SHOW server_version")
|
|
|
|
.then((x) => x[0].server_version);
|
2020-04-26 04:39:15 +02:00
|
|
|
dbLogger.succ(`Connected: v${v}`);
|
2019-04-07 14:50:36 +02:00
|
|
|
} catch (e) {
|
2023-01-13 05:40:33 +01:00
|
|
|
dbLogger.error("Cannot connect", null, true);
|
2020-04-26 04:39:15 +02:00
|
|
|
dbLogger.error(e);
|
2019-04-07 14:50:36 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2021-04-24 15:55:18 +02:00
|
|
|
}
|
2019-04-07 14:50:36 +02:00
|
|
|
|
2023-06-06 02:27:40 +02:00
|
|
|
async function spawnWorkers(limit = 1) {
|
2019-04-07 14:50:36 +02:00
|
|
|
const workers = Math.min(limit, os.cpus().length);
|
2023-01-13 05:40:33 +01:00
|
|
|
bootLogger.info(`Starting ${workers} worker${workers === 1 ? "" : "s"}...`);
|
2019-04-07 14:50:36 +02:00
|
|
|
await Promise.all([...Array(workers)].map(spawnWorker));
|
2023-01-13 05:40:33 +01:00
|
|
|
bootLogger.succ("All workers started");
|
2019-04-07 14:50:36 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function spawnWorker(): Promise<void> {
|
2023-01-13 05:40:33 +01:00
|
|
|
return new Promise((res) => {
|
2019-04-07 14:50:36 +02:00
|
|
|
const worker = cluster.fork();
|
2023-01-13 05:40:33 +01:00
|
|
|
worker.on("message", (message) => {
|
|
|
|
if (message === "listenFailed") {
|
|
|
|
bootLogger.error("The server Listen failed due to the previous error.");
|
2022-05-19 04:49:07 +02:00
|
|
|
process.exit(1);
|
|
|
|
}
|
2023-01-13 05:40:33 +01:00
|
|
|
if (message !== "ready") return;
|
2019-04-07 14:50:36 +02:00
|
|
|
res();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|