-
Notifications
You must be signed in to change notification settings - Fork 54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add watchdog for watchers #106
Changes from 6 commits
63ee1ff
3273b23
dd3c727
b101794
66738b9
0cd7385
3171c1e
3d706dd
f38688c
597675e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,11 +10,12 @@ const { getNonce, getChainId } = require('./tx/web3') | |
const { | ||
addExtraGas, | ||
checkHTTPS, | ||
privateKeyToAddress, | ||
syncForEach, | ||
waitForFunds, | ||
privateKeyToAddress | ||
watchdog | ||
} = require('./utils/utils') | ||
const { EXTRA_GAS_PERCENTAGE } = require('./utils/constants') | ||
const { EXIT_CODES, EXTRA_GAS_PERCENTAGE } = require('./utils/constants') | ||
|
||
const { VALIDATOR_ADDRESS_PRIVATE_KEY, REDIS_LOCK_TTL } = process.env | ||
|
||
fvictorio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -32,6 +33,8 @@ const nonceLock = `lock:${config.id}:nonce` | |
const nonceKey = `${config.id}:nonce` | ||
let chainId = 0 | ||
|
||
const maxProcessingTime = process.env.MAX_PROCESSING_TIME | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to define a default value which will be used if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was intentional, to allow not setting the watchdog. If you think it should always be enabled, we can add a default value here. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What risks can you see if we enable the watchdog by default? In this case the user could disable the watchdog by zeroing the configuration parameter explicitly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That is a bit complicated. Javascript treats 0 as a falsy value, so doing On the other hand, the environment variables are strings, so A possible solution is to use a negative value to disable it. That will work both with strings and with numbers: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not understand this.
And later, when you add environment variables validation and casting you will change this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That can work. This is what I did (see f38688c):
I didn't want to add this logic twice (in the watcher and the sender), so I moved it to the config. And since the senders weren't including the base config, I also added it to them. What do you think? |
||
|
||
async function initialize() { | ||
try { | ||
const checkHttps = checkHTTPS(process.env.ALLOW_HTTP, logger) | ||
|
@@ -44,7 +47,16 @@ async function initialize() { | |
chainId = await getChainId(web3Instance) | ||
connectSenderToQueue({ | ||
queueName: config.queue, | ||
cb: main | ||
cb: options => { | ||
if (maxProcessingTime) { | ||
return watchdog(() => main(options), maxProcessingTime, () => { | ||
logger.fatal('Max processing time reached') | ||
process.exit(EXIT_CODES.MAX_TIME_REACHED) | ||
}) | ||
} | ||
|
||
return main(options) | ||
} | ||
}) | ||
} catch (e) { | ||
logger.error(e.message) | ||
akolotov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,7 +7,8 @@ const { redis } = require('./services/redisClient') | |
const logger = require('./services/logger') | ||
const rpcUrlsManager = require('./services/getRpcUrlsManager') | ||
const { getRequiredBlockConfirmations, getEvents } = require('./tx/web3') | ||
const { checkHTTPS } = require('./utils/utils') | ||
const { checkHTTPS, watchdog } = require('./utils/utils') | ||
const { EXIT_CODES } = require('./utils/constants') | ||
|
||
if (process.argv.length < 3) { | ||
logger.error('Please check the number of arguments, config file was not provided') | ||
fvictorio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
@@ -30,6 +31,8 @@ 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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to define a default value which will be used if |
||
|
||
async function initialize() { | ||
try { | ||
const checkHttps = checkHTTPS(process.env.ALLOW_HTTP, logger) | ||
|
@@ -51,7 +54,14 @@ async function initialize() { | |
async function runMain({ sendToQueue }) { | ||
fvictorio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
try { | ||
if (connection.isConnected() && redis.status === 'ready') { | ||
await main({ sendToQueue }) | ||
if (maxProcessingTime) { | ||
await watchdog(() => main({ sendToQueue }), maxProcessingTime, () => { | ||
logger.fatal('Max processing time reached') | ||
process.exit(EXIT_CODES.MAX_TIME_REACHED) | ||
}) | ||
} else { | ||
await main({ sendToQueue }) | ||
} | ||
} | ||
} catch (e) { | ||
logger.error(e) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is my understanding correct and 10 min is too much for the watchdog timer? Could you estimate max time expected to handle one iteration of watcher?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess it depends on the number of events processed at the same time. I will try sending several transactions to the kovan-sokol bridge and see how much time is spent processing them.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sent 10 transactions to Sokol. 2 of them were processed in one block, 8 in another. The time difference between the first log, and the log of the sender receiving the last transactions to send was 3 seconds (that is Sokol's block time, isn't it?).
The time is going to depend on the chain, but I guess one minute should be enough in the vast majority of cases (and it can be configured, anyway).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I think the timout should be comparable with the polling time. I think 3 or 4 times more than the polling time of the corresponding network. What do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that sounds reasonable. You mean using that as the example, or that the timeout should be computed from the polling time?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, let's set the timeout in the example to be 4 times more that the longest polling time and provide these recommendations in README.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please check 3171c1e.