Skip to content

Commit

Permalink
Add default max processing time
Browse files Browse the repository at this point in the history
  • Loading branch information
fvictorio committed Oct 24, 2018
1 parent 3d706dd commit f38688c
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 10 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 12 additions & 1 deletion config/base.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
2 changes: 2 additions & 0 deletions config/foreign-sender.config.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
2 changes: 2 additions & 0 deletions config/home-sender.config.js
Original file line number Diff line number Diff line change
@@ -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',
Expand Down
6 changes: 2 additions & 4 deletions src/sender.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
})
Expand Down
6 changes: 2 additions & 4 deletions src/watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
})
Expand Down

0 comments on commit f38688c

Please sign in to comment.