forked from Snipa22/nodejs-pool
-
Notifications
You must be signed in to change notification settings - Fork 74
Expand file tree
/
Copy pathinit_mini.js
More file actions
61 lines (53 loc) · 2.53 KB
/
Copy pathinit_mini.js
File metadata and controls
61 lines (53 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"use strict";
const applyConfigRows = require("./lib/common/config_rows.js");
const path = require("path");
const resolveCoinConfig = require("./resolve_coin_config.js");
const { getInitializedLocalDatabase } = require("./lib/common/database.js");
const REPO_ROOT = __dirname;
const CONFIG_PATH = path.join(REPO_ROOT, "config.json");
const COIN_CONFIG_PATH = path.join(REPO_ROOT, "coinConfig.json");
const DATA_PROTO_PATH = path.join(REPO_ROOT, "lib/common/data.proto");
// Standalone scripts (manage_scripts/tools) open the shared LMDB env here but exit via
// process.exit() without a graceful shutdown. Closing the env on "exit" frees this process's
// reader slots (and aborts any read txn an iterator left open) so a script run never leaves a
// stale reader behind. Idempotent and guarded so it is safe on every exit path.
let envClosed = false;
function closeEnv() {
if (envClosed) return;
const database = global.database;
const env = database && database.role === "local" ? database.env : null;
if (!env || typeof env.close !== "function") return;
envClosed = true;
try {
env.close();
} catch (_error) { /* best-effort close on shutdown; ignore errors */ }
}
/** @param {() => void | Promise<void>} callback */
function init(callback) {
const fs = require("fs");
const mysql = require("promise-mysql");
const config = fs.readFileSync(CONFIG_PATH, "utf8");
const coinConfig = fs.readFileSync(COIN_CONFIG_PATH, "utf8");
const protobuf = require("protocol-buffers");
global.support = require("./lib/common/support.js")();
const startupConfig = JSON.parse(config);
const resolvedCoinConfig = resolveCoinConfig(startupConfig, JSON.parse(coinConfig));
global.config = startupConfig;
global.mysql = mysql.createPool(global.config.mysql);
global.protos = protobuf(fs.readFileSync(DATA_PROTO_PATH));
/** @type {Promise<import("./lib/common/config_rows.js").ConfigRow[]>} */
const configRows = global.mysql.query("SELECT * FROM config");
configRows.then(function (rows) {
applyConfigRows(global.config, rows);
}).then(function(){
global.config.coin = resolvedCoinConfig;
const coinInc = require(resolvedCoinConfig.funcFile);
global.coinFuncs = new coinInc();
const comms = require("./lib/common/local_comms");
const localDatabase = new comms();
localDatabase.initEnv();
global.database = getInitializedLocalDatabase(localDatabase);
process.on("exit", closeEnv);
}).then(callback);
}
module.exports = { init };