add health watcher

This commit is contained in:
Danil Kovtonyuk 2021-02-10 01:53:28 +10:00 committed by Alexey Pertsev
parent 37d9cfedfa
commit 273603e981
4 changed files with 37 additions and 2 deletions

@ -1,18 +1,19 @@
{ {
"name": "relay", "name": "relay",
"version": "4.0.1", "version": "4.0.2",
"description": "Relayer for Tornado.cash privacy solution. https://tornado.cash", "description": "Relayer for Tornado.cash privacy solution. https://tornado.cash",
"scripts": { "scripts": {
"server": "node src/server.js", "server": "node src/server.js",
"worker": "node src/worker", "worker": "node src/worker",
"treeWatcher": "node src/treeWatcher", "treeWatcher": "node src/treeWatcher",
"priceWatcher": "node src/priceWatcher", "priceWatcher": "node src/priceWatcher",
"healthWatcher": "node src/healthWatcher",
"eslint": "eslint --ext .js --ignore-path .gitignore .", "eslint": "eslint --ext .js --ignore-path .gitignore .",
"prettier:check": "npx prettier --check . --config .prettierrc", "prettier:check": "npx prettier --check . --config .prettierrc",
"prettier:fix": "npx prettier --write . --config .prettierrc", "prettier:fix": "npx prettier --write . --config .prettierrc",
"lint": "yarn eslint && yarn prettier:check", "lint": "yarn eslint && yarn prettier:check",
"test": "mocha", "test": "mocha",
"start": "yarn server & yarn priceWatcher & yarn treeWatcher & yarn worker " "start": "yarn server & yarn priceWatcher & yarn treeWatcher & yarn worker & yarn healthWatcher"
}, },
"author": "tornado.cash", "author": "tornado.cash",
"license": "MIT", "license": "MIT",

@ -24,4 +24,5 @@ module.exports = {
[jobType.MINING_REWARD]: 800000, [jobType.MINING_REWARD]: 800000,
[jobType.MINING_WITHDRAW]: 800000, [jobType.MINING_WITHDRAW]: 800000,
}, },
minimumBalance: 1000000000000000000,
} }

27
src/healthWatcher.js Normal file

@ -0,0 +1,27 @@
const Web3 = require('web3')
const Redis = require('ioredis')
const { toBN, fromWei } = require('web3-utils')
const { setSafeInterval } = require('./utils')
const { redisUrl, httpRpcUrl, privateKey, minimumBalance } = require('./config')
const web3 = new Web3(httpRpcUrl)
const redis = new Redis(redisUrl)
async function main() {
try {
const { address } = web3.eth.accounts.privateKeyToAccount(privateKey)
const balance = await web3.eth.getBalance(address)
if (toBN(balance).lt(toBN(minimumBalance))) {
throw new Error(`Not enough balance, less than ${fromWei(minimumBalance.toString())} ETH`)
}
await redis.hset('health', { status: true, error: '' })
} catch (e) {
console.error('healthWatcher', e.message)
await redis.hset('health', { status: false, error: e.message })
}
}
setSafeInterval(main, 30 * 1000)

@ -6,6 +6,10 @@ const redis = new Redis(redisUrl)
async function status(req, res) { async function status(req, res) {
const ethPrices = await redis.hgetall('prices') const ethPrices = await redis.hgetall('prices')
const health = await redis.hgetall('health')
const { waiting: currentQueue } = await queue.queue.getJobCounts()
res.json({ res.json({
rewardAccount, rewardAccount,
instances: instances[`netId${netId}`], instances: instances[`netId${netId}`],
@ -14,6 +18,8 @@ async function status(req, res) {
tornadoServiceFee, tornadoServiceFee,
miningServiceFee, miningServiceFee,
version, version,
health,
currentQueue,
}) })
} }