From 95c6dc23c6f35ca52b76e6f740520615186f6cbe Mon Sep 17 00:00:00 2001 From: smart_ex Date: Fri, 8 Apr 2022 20:29:37 +1000 Subject: [PATCH] increment errors score --- docker-compose.test.yml | 2 ++ src/contollers/status.js | 3 ++- src/healthWatcher.js | 1 - src/modules/redis.js | 6 +++--- src/priceWatcher.js | 11 +++++++++-- src/treeWatcher.js | 4 ++-- src/utils.js | 16 ++++++++++++++++ src/worker.js | 3 ++- 8 files changed, 36 insertions(+), 10 deletions(-) diff --git a/docker-compose.test.yml b/docker-compose.test.yml index f139f00..785cb25 100644 --- a/docker-compose.test.yml +++ b/docker-compose.test.yml @@ -54,6 +54,8 @@ services: redis: image: redis restart: always + ports: + - '6379:6379' command: [redis-server, --appendonly, 'yes'] volumes: - redis:/data diff --git a/src/contollers/status.js b/src/contollers/status.js index dae519a..b103034 100644 --- a/src/contollers/status.js +++ b/src/contollers/status.js @@ -2,11 +2,12 @@ const queue = require('../queue') const { netId, tornadoServiceFee, miningServiceFee, instances, rewardAccount } = require('../config') const { version } = require('../../package.json') const { redis } = require('../modules/redis') +const { readRelayerErrors } = require('../utils') async function status(req, res) { const ethPrices = await redis.hgetall('prices') const health = await redis.hgetall('health') - const errors = await redis.zrevrange('errors', 0, -1) + const errors = await readRelayerErrors(redis) const { waiting: currentQueue } = await queue.queue.getJobCounts() diff --git a/src/healthWatcher.js b/src/healthWatcher.js index f942cf0..af81c6e 100644 --- a/src/healthWatcher.js +++ b/src/healthWatcher.js @@ -14,7 +14,6 @@ async function main() { await redis.hset('health', { status: true, error: '' }) } catch (e) { console.error('healthWatcher', e.message) - redis.zadd('errors', e.score || 0, e.message) await redis.hset('health', { status: false, error: e.message }) } } diff --git a/src/modules/redis.js b/src/modules/redis.js index 721c623..3bad7a2 100644 --- a/src/modules/redis.js +++ b/src/modules/redis.js @@ -1,8 +1,8 @@ -const Redis = require('ioredis') +const { createClient } = require('ioredis') const { redisUrl } = require('../config') -const redis = new Redis(redisUrl) -const redisSubscribe = new Redis(redisUrl) +const redis = createClient(redisUrl) +const redisSubscribe = createClient(redisUrl) module.exports = { redis, diff --git a/src/priceWatcher.js b/src/priceWatcher.js index a41ee7b..56d68f7 100644 --- a/src/priceWatcher.js +++ b/src/priceWatcher.js @@ -1,5 +1,12 @@ const { offchainOracleAddress } = require('./config') -const { getArgsForOracle, setSafeInterval, toChecksumAddress, toBN, RelayerError } = require('./utils') +const { + getArgsForOracle, + setSafeInterval, + toChecksumAddress, + toBN, + RelayerError, + logRelayerError, +} = require('./utils') const { redis } = require('./modules/redis') const web3 = require('./modules/web3')() @@ -32,7 +39,7 @@ async function main() { await redis.hmset('prices', ethPrices) console.log('Wrote following prices to redis', ethPrices) } catch (e) { - redis.zadd('errors', e.score || 1, e.message) + await logRelayerError(redis, e.message) console.error('priceWatcher error', e) } } diff --git a/src/treeWatcher.js b/src/treeWatcher.js index 28e8c9e..92add34 100644 --- a/src/treeWatcher.js +++ b/src/treeWatcher.js @@ -1,6 +1,6 @@ const MerkleTree = require('fixed-merkle-tree') const { minerMerkleTreeHeight, torn, netId } = require('./config') -const { poseidonHash2, toBN } = require('./utils') +const { poseidonHash2, toBN, logRelayerError } = require('./utils') const resolver = require('./modules/resolver') const web3 = require('./modules/web3')('ws') const MinerABI = require('../abis/mining.abi.json') @@ -123,7 +123,7 @@ async function init() { eventSubscription = contract.events.NewAccount({ fromBlock: toBlock + 1 }, processNewEvent) blockSubscription = web3.eth.subscribe('newBlockHeaders', processNewBlock) } catch (e) { - redis.zadd('errors', 1, e.message) + await logRelayerError(redis, e.message) console.error('error on init treeWatcher', e.message) } } diff --git a/src/utils.js b/src/utils.js index 6b8bbc6..883713a 100644 --- a/src/utils.js +++ b/src/utils.js @@ -125,6 +125,20 @@ class RelayerError extends Error { } } +const logRelayerError = async (redis, e) => { + await redis.zadd('errors', 'INCR', 1, e.message) +} + +const readRelayerErrors = async redis => { + const set = await redis.zrevrange('errors', 0, -1, 'WITHSCORES') + const errors = [] + while (set.length) { + const [message, score] = set.splice(0, 2) + errors.push({ message, score }) + } + return errors +} + module.exports = { getInstance, setSafeInterval, @@ -140,4 +154,6 @@ module.exports = { BN, isAddress, RelayerError, + logRelayerError, + readRelayerErrors, } diff --git a/src/worker.js b/src/worker.js index aa31685..6e7b56a 100644 --- a/src/worker.js +++ b/src/worker.js @@ -18,6 +18,7 @@ const { fromWei, toChecksumAddress, RelayerError, + logRelayerError, } = require('./utils') const { jobType, status } = require('./constants') const { @@ -106,7 +107,7 @@ async function start() { queue.process(processJob) console.log('Worker started') } catch (e) { - redis.zadd('errors', e.score || 1, e.message) + await logRelayerError(redis, e.message) console.error('error on start worker', e.message) } }