don't crash if DB is unavailable
Some checks failed
Docker Compose Build / Run docker build (push) Failing after 29s
Some checks failed
Docker Compose Build / Run docker build (push) Failing after 29s
This commit is contained in:
parent
03623f90ca
commit
e043ed10f8
160
src/index.js
160
src/index.js
@ -1,112 +1,110 @@
|
|||||||
require('dotenv').config()
|
require("dotenv").config();
|
||||||
var process = require('process')
|
var process = require("process");
|
||||||
const express = require('express')
|
const express = require("express");
|
||||||
var CronJob = require('cron').CronJob;
|
var CronJob = require("cron").CronJob;
|
||||||
const etherscan = require('./etherscan');
|
const etherscan = require("./etherscan");
|
||||||
const AppError = require('./error');
|
const AppError = require("./error");
|
||||||
const prom = require('./prometheus');
|
const prom = require("./prometheus");
|
||||||
const DB = require('./db');
|
const DB = require("./db");
|
||||||
|
|
||||||
const startServer = () => {
|
const startServer = () => {
|
||||||
const server = express();
|
const server = express();
|
||||||
|
|
||||||
server.get('/metrics', async (req, res) => {
|
server.get("/metrics", async (req, res) => {
|
||||||
try {
|
try {
|
||||||
res.set('Content-Type', prom.register.contentType)
|
res.set("Content-Type", prom.register.contentType);
|
||||||
res.end(await prom.register.metrics())
|
res.end(await prom.register.metrics());
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
res.status(500).end(ex)
|
res.status(500).end(ex);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
const serverPort = process.env.SERVER_PORT || 3000;
|
const serverPort = process.env.SERVER_PORT || 3000;
|
||||||
const serverHost = process.env.SERVER_HOST || '127.0.0.1';
|
const serverHost = process.env.SERVER_HOST || "127.0.0.1";
|
||||||
server.listen(serverPort, serverHost, () => console.log(`Http server running on port ${serverHost}:${serverPort}`));
|
server.listen(serverPort, serverHost, () => console.log(`Http server running on port ${serverHost}:${serverPort}`));
|
||||||
}
|
};
|
||||||
|
|
||||||
let dbStateDiff;
|
let dbStateDiff;
|
||||||
|
|
||||||
const connectToDB = () => {
|
const connectToDB = () => {
|
||||||
|
try {
|
||||||
const statediffUser = process.env.STATEDIFF_PG_USER;
|
const statediffUser = process.env.STATEDIFF_PG_USER;
|
||||||
const statediffPassword = process.env.STATEDIFF_PG_PASSWORD;
|
const statediffPassword = process.env.STATEDIFF_PG_PASSWORD;
|
||||||
const statediffDB = process.env.STATEDIFF_PG_DATABASE;
|
const statediffDB = process.env.STATEDIFF_PG_DATABASE;
|
||||||
const statediffHost = process.env.STATEDIFF_PG_HOST;
|
const statediffHost = process.env.STATEDIFF_PG_HOST;
|
||||||
const statediffPort = process.env.STATEDIFF_PG_PORT;
|
const statediffPort = process.env.STATEDIFF_PG_PORT;
|
||||||
|
dbStateDiff = new DB("statediff", statediffUser, statediffPassword, statediffDB, statediffHost, statediffPort);
|
||||||
dbStateDiff = new DB('statediff', statediffUser, statediffPassword, statediffDB, statediffHost, statediffPort)
|
} catch (error) {
|
||||||
}
|
console.error("db connection error", error);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const run = async () => {
|
const run = async () => {
|
||||||
|
// statediff database
|
||||||
|
const dbStateDiffBlockNumber = dbStateDiff.getBlockNumber();
|
||||||
|
|
||||||
// statediff database
|
// Etherscan
|
||||||
const dbStateDiffBlockNumber = dbStateDiff.getBlockNumber();
|
const etherscanBlock = etherscan.getBlockNumber();
|
||||||
|
|
||||||
// Etherscan
|
const results = await Promise.allSettled([etherscanBlock, dbStateDiffBlockNumber]);
|
||||||
const etherscanBlock = etherscan.getBlockNumber();
|
|
||||||
|
|
||||||
const results = await Promise.allSettled([
|
for (const result of results) {
|
||||||
etherscanBlock,
|
if (result.status === "rejected") {
|
||||||
dbStateDiffBlockNumber,
|
if (result.reason instanceof AppError) {
|
||||||
|
const errorData = result.reason.data;
|
||||||
|
console.log(errorData.type, errorData.message);
|
||||||
|
} else {
|
||||||
|
console.error(result.reason);
|
||||||
|
}
|
||||||
|
} else if (result.status === "fulfilled") {
|
||||||
|
const value = result.value;
|
||||||
|
switch (value.type) {
|
||||||
|
case "etherscan":
|
||||||
|
prom.etherscanGauge.set(value.blockNumber);
|
||||||
|
|
||||||
])
|
break;
|
||||||
|
case "statediff":
|
||||||
|
prom.statediffDBGauge.set(value.blockNumber);
|
||||||
|
|
||||||
for (const result of results) {
|
break;
|
||||||
if (result.status === 'rejected') {
|
}
|
||||||
if (result.reason instanceof AppError) {
|
|
||||||
const errorData = result.reason.data;
|
|
||||||
console.log(errorData.type, errorData.message);
|
|
||||||
} else {
|
|
||||||
console.error(result.reason);
|
|
||||||
}
|
|
||||||
} else if (result.status === 'fulfilled') {
|
|
||||||
const value = result.value;
|
|
||||||
switch (value.type) {
|
|
||||||
case 'etherscan':
|
|
||||||
prom.etherscanGauge.set(value.blockNumber);
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'statediff':
|
|
||||||
prom.statediffDBGauge.set(value.blockNumber);
|
|
||||||
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms));
|
const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
||||||
|
|
||||||
const main = async () => {
|
const main = async () => {
|
||||||
startServer();
|
startServer();
|
||||||
connectToDB();
|
connectToDB();
|
||||||
|
|
||||||
let lock = false;
|
let lock = false;
|
||||||
|
|
||||||
var job = new CronJob(
|
var job = new CronJob(
|
||||||
'*/10 * * * * *',
|
"*/10 * * * * *",
|
||||||
async () => {
|
async () => {
|
||||||
if (lock) {
|
if (lock) {
|
||||||
console.log('Parallel process is executing. Skipping');
|
console.log("Parallel process is executing. Skipping");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
lock = true;
|
lock = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
await run();
|
await run();
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error(e);
|
console.error(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
lock = false;
|
lock = false;
|
||||||
},
|
},
|
||||||
null,
|
null,
|
||||||
true
|
true
|
||||||
);
|
);
|
||||||
}
|
};
|
||||||
|
|
||||||
main().catch((e) => console.error(e));
|
main().catch((e) => console.error(e));
|
||||||
|
|
||||||
process.on('SIGINT', () => {
|
process.on("SIGINT", () => {
|
||||||
console.info("Interrupted")
|
console.info("Interrupted");
|
||||||
process.exit(0)
|
process.exit(0);
|
||||||
})
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user