diff --git a/README.md b/README.md index 1008cb2..92acd78 100644 --- a/README.md +++ b/README.md @@ -213,7 +213,7 @@ where the _watcher_ could be one of: | `REDIS_LOCK_TTL` | Threshold in milliseconds for locking a resource in the Redis DB. Until the threshold is exceeded, the resource is unlocked. Usually it is `1000`. | integer | | `ALLOW_HTTP` | **Only use in test environments - must be omitted in production environments.**. If this parameter is specified and set to `yes`, RPC URLs can be specified in form of HTTP links. A warning that the connection is insecure will be written to the logs. | `yes` / `no` | | `LOG_LEVEL` | Set the level of details in the logs. | `trace` / `debug` / `info` / `warn` / `error` / `fatal` | -| `MAX_PROCESSING_TIME` | If this parameter is specified, the workers processes will be killed if this amount of time (in milliseconds) is ellapsed before they finish processing. It is recommended to set this value to 4 times the value of the longest polling time (set with the `HOME_POLLING_INTERVAL` and `FOREIGN_POLLING_INTERVAL` variables). | integer | +| `MAX_PROCESSING_TIME` | The workers processes will be killed if this amount of time (in milliseconds) is ellapsed before they finish processing. It is recommended to set this value to 4 times the value of the longest polling time (set with the `HOME_POLLING_INTERVAL` and `FOREIGN_POLLING_INTERVAL` variables). To disable this, set the time to 0. | integer | ### Useful Commands for Development diff --git a/config/base.config.js b/config/base.config.js index 258b0f4..ed3799d 100644 --- a/config/base.config.js +++ b/config/base.config.js @@ -45,13 +45,24 @@ switch (process.env.BRIDGE_MODE) { } } +let maxProcessingTime = null +if (String(process.env.MAX_PROCESSING_TIME) === '0') { + maxProcessingTime = 0 +} else if (!process.env.MAX_PROCESSING_TIME) { + maxProcessingTime = + 4 * Math.max(process.env.HOME_POLLING_INTERVAL, process.env.FOREIGN_POLLING_INTERVAL) +} else { + maxProcessingTime = Number(process.env.MAX_PROCESSING_TIME) +} + const bridgeConfig = { homeBridgeAddress: process.env.HOME_BRIDGE_ADDRESS, homeBridgeAbi: homeAbi, foreignBridgeAddress: process.env.FOREIGN_BRIDGE_ADDRESS, foreignBridgeAbi: foreignAbi, eventFilter: {}, - validatorAddress: VALIDATOR_ADDRESS || privateKeyToAddress(VALIDATOR_ADDRESS_PRIVATE_KEY) + validatorAddress: VALIDATOR_ADDRESS || privateKeyToAddress(VALIDATOR_ADDRESS_PRIVATE_KEY), + maxProcessingTime } const homeConfig = { diff --git a/config/foreign-sender.config.js b/config/foreign-sender.config.js index 472c873..43fab9a 100644 --- a/config/foreign-sender.config.js +++ b/config/foreign-sender.config.js @@ -1,8 +1,10 @@ require('dotenv').config() +const baseConfig = require('./base.config') const { web3Foreign } = require('../src/services/web3') module.exports = { + ...baseConfig.bridgeConfig, queue: 'foreign', id: 'foreign', name: 'sender-foreign', diff --git a/config/home-sender.config.js b/config/home-sender.config.js index 6aa65aa..b9a147e 100644 --- a/config/home-sender.config.js +++ b/config/home-sender.config.js @@ -1,8 +1,10 @@ require('dotenv').config() +const baseConfig = require('./base.config') const { web3Home } = require('../src/services/web3') module.exports = { + ...baseConfig.bridgeConfig, queue: 'home', id: 'home', name: 'sender-home', diff --git a/src/sender.js b/src/sender.js index b6418ab..1193f96 100644 --- a/src/sender.js +++ b/src/sender.js @@ -33,8 +33,6 @@ const nonceLock = `lock:${config.id}:nonce` const nonceKey = `${config.id}:nonce` let chainId = 0 -const maxProcessingTime = process.env.MAX_PROCESSING_TIME - async function initialize() { try { const checkHttps = checkHTTPS(process.env.ALLOW_HTTP, logger) @@ -48,8 +46,8 @@ async function initialize() { connectSenderToQueue({ queueName: config.queue, cb: options => { - if (maxProcessingTime) { - return watchdog(() => main(options), maxProcessingTime, () => { + if (config.maxProcessingTime) { + return watchdog(() => main(options), config.maxProcessingTime, () => { logger.fatal('Max processing time reached') process.exit(EXIT_CODES.MAX_TIME_REACHED) }) diff --git a/src/watcher.js b/src/watcher.js index 938a269..939ee1f 100644 --- a/src/watcher.js +++ b/src/watcher.js @@ -31,8 +31,6 @@ const eventContract = new web3Instance.eth.Contract(config.eventAbi, config.even const lastBlockRedisKey = `${config.id}:lastProcessedBlock` let lastProcessedBlock = BN.max(config.startBlock.sub(ONE), ZERO) -const maxProcessingTime = process.env.MAX_PROCESSING_TIME - async function initialize() { try { const checkHttps = checkHTTPS(process.env.ALLOW_HTTP, logger) @@ -54,8 +52,8 @@ async function initialize() { async function runMain({ sendToQueue }) { try { if (connection.isConnected() && redis.status === 'ready') { - if (maxProcessingTime) { - await watchdog(() => main({ sendToQueue }), maxProcessingTime, () => { + if (config.maxProcessingTime) { + await watchdog(() => main({ sendToQueue }), config.maxProcessingTime, () => { logger.fatal('Max processing time reached') process.exit(EXIT_CODES.MAX_TIME_REACHED) })