From 0ef671ef003c227c74fcfa0d308fe189ca4d8d9f Mon Sep 17 00:00:00 2001 From: Joris W Date: Sun, 29 Dec 2019 13:29:28 +0100 Subject: [PATCH 1/3] Fix spontaneous bot exits Fixes #1922 by calling `getNext()` based on record count checks rather than the unreliable `.on('end')` cursor event --- commands/sim.js | 47 ++++++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 17 deletions(-) diff --git a/commands/sim.js b/commands/sim.js index e1fc168952..1288e43743 100644 --- a/commands/sim.js +++ b/commands/sim.js @@ -235,14 +235,14 @@ module.exports = function (program, conf) { process.exit(0) }) } - - function getNext () { + + async function getNext () { var opts = { query: { selector: so.selector.normalized }, sort: {time: 1}, - limit: 1000 + limit: 100 } if (so.end) { opts.query.time = {$lte: so.end} @@ -265,21 +265,19 @@ module.exports = function (program, conf) { if (!opts.query.time) opts.query.time = {} opts.query.time['$gte'] = query_start } - var collectionCursor = tradesCollection.find(opts.query).sort(opts.sort).stream() + var collectionCursor = tradesCollection + .find(opts.query) + .sort(opts.sort) + .limit(opts.limit) + + const totalTrades = await collectionCursor.count(true); + collectionCursorStream = collectionCursor.stream() + var numTrades = 0 var lastTrade - collectionCursor.on('data', function(trade){ - lastTrade = trade - numTrades++ - if (so.symmetrical && reversing) { - trade.orig_time = trade.time - trade.time = reverse_point + (reverse_point - trade.time) - } - eventBus.emit('trade', trade) - }) + const onCollectionCursorEnd = () => { - collectionCursor.on('end', function(){ if(numTrades === 0){ if (so.symmetrical && !reversing) { reversing = true @@ -296,11 +294,26 @@ module.exports = function (program, conf) { cursor = lastTrade.time } } - setImmediate(getNext) + collectionCursorStream.close() + setImmediate(async () => await getNext()) + } + + collectionCursorStream.on('data', function(trade){ + lastTrade = trade + numTrades++ + if (so.symmetrical && reversing) { + trade.orig_time = trade.time + trade.time = reverse_point + (reverse_point - trade.time) + } + eventBus.emit('trade', trade) + + if(numTrades && totalTrades && totalTrades == numTrades){ + onCollectionCursorEnd(); + } }) + } - getNext() + return getNext() }) } - From d8b5784b1e735e06339366c2c89071c663d28101 Mon Sep 17 00:00:00 2001 From: Joris Witteman Date: Thu, 2 Jan 2020 10:21:48 +0100 Subject: [PATCH 2/3] Set a somewhat recent ECMAScript version --- .eslintrc | 1 + 1 file changed, 1 insertion(+) diff --git a/.eslintrc b/.eslintrc index 79872a1fdc..d7f2411ef1 100644 --- a/.eslintrc +++ b/.eslintrc @@ -27,6 +27,7 @@ }, "parserOptions": { "sourceType": "module", + "ecmaVersion": "2017", "ecmaFeatures": { "experimentalObjectRestSpread": true } From 178c065ede30870a88ff9cb8e325342aab471981 Mon Sep 17 00:00:00 2001 From: Joris Witteman Date: Thu, 2 Jan 2020 10:23:04 +0100 Subject: [PATCH 3/3] Clean up so eslint passes --- commands/sim.js | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/commands/sim.js b/commands/sim.js index 1288e43743..10707d6326 100644 --- a/commands/sim.js +++ b/commands/sim.js @@ -235,17 +235,18 @@ module.exports = function (program, conf) { process.exit(0) }) } - - async function getNext () { + + var getNext = async () => { var opts = { query: { selector: so.selector.normalized }, - sort: {time: 1}, - limit: 100 + sort: { time: 1 }, + limit: 100, + timeout: false } if (so.end) { - opts.query.time = {$lte: so.end} + opts.query.time = { $lte: so.end } } if (cursor) { if (reversing) { @@ -254,14 +255,12 @@ module.exports = function (program, conf) { if (query_start) { opts.query.time['$gte'] = query_start } - opts.sort = {time: -1} - } - else { + opts.sort = { time: -1 } + } else { if (!opts.query.time) opts.query.time = {} opts.query.time['$gt'] = cursor } - } - else if (query_start) { + } else if (query_start) { if (!opts.query.time) opts.query.time = {} opts.query.time['$gte'] = query_start } @@ -270,15 +269,14 @@ module.exports = function (program, conf) { .sort(opts.sort) .limit(opts.limit) - const totalTrades = await collectionCursor.count(true); - collectionCursorStream = collectionCursor.stream() + var totalTrades = await collectionCursor.count(true) + const collectionCursorStream = collectionCursor.stream() var numTrades = 0 var lastTrade - const onCollectionCursorEnd = () => { - - if(numTrades === 0){ + var onCollectionCursorEnd = () => { + if (numTrades === 0) { if (so.symmetrical && !reversing) { reversing = true reverse_point = cursor @@ -289,8 +287,7 @@ module.exports = function (program, conf) { } else { if (reversing) { cursor = lastTrade.orig_time - } - else { + } else { cursor = lastTrade.time } } @@ -298,7 +295,7 @@ module.exports = function (program, conf) { setImmediate(async () => await getNext()) } - collectionCursorStream.on('data', function(trade){ + collectionCursorStream.on('data', function(trade) { lastTrade = trade numTrades++ if (so.symmetrical && reversing) { @@ -307,11 +304,10 @@ module.exports = function (program, conf) { } eventBus.emit('trade', trade) - if(numTrades && totalTrades && totalTrades == numTrades){ - onCollectionCursorEnd(); + if (numTrades && totalTrades && totalTrades == numTrades) { + onCollectionCursorEnd() } }) - } return getNext()