Skip to content

Commit

Permalink
[Bot]fix: handle already subscribed error for blocks with candle bloc…
Browse files Browse the repository at this point in the history
…ks by… (binary-com#13309)

* fix: handle already subscribed error for blocks with candle blocks by unsubscribing candle request

* fix: added forgetAll call to handle alreadySubscribed error

* fix: added setTimeout before executing start and stop of the bot

* fix: add recovery mechanism once alreadySubscribed error occurs

* fix: added logic so that bot recovers when alreadySubscribed error occurs

* fix: added a flag to avoid user from clicking until the bot stops

* fix: unregister bot.click_stop event in case of errors

* fix: added flag to check if bot is already stopping in stopBot function

* fix: added condition to check if api_base.api is present before making the call
  • Loading branch information
vinu-deriv committed Feb 13, 2024
1 parent d1c6d42 commit 9a02b06
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 49 deletions.
5 changes: 5 additions & 0 deletions packages/bot-skeleton/src/scratch/dbot.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,10 @@ class DBot {
* JavaScript code that's fed to the interpreter.
*/
runBot() {
if (api_base.is_stopping) return;

try {
api_base.is_stopping = false;
const code = this.generateCode();

if (!this.interpreter.bot.tradeEngine.checkTicksPromiseExists()) this.interpreter = Interpreter();
Expand Down Expand Up @@ -325,6 +328,8 @@ class DBot {
* that trade will be completed first to reflect correct contract status in UI.
*/
async stopBot() {
if (api_base.is_stopping) return;

api_base.setIsRunning(false);

await this.interpreter.stop();
Expand Down
1 change: 1 addition & 0 deletions packages/bot-skeleton/src/services/api/api-base.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class APIBase {
subscriptions = [];
time_interval = null;
has_activeSymbols = false;
is_stopping = false;

async init(force_update = false) {
if (getLoginId()) {
Expand Down
73 changes: 26 additions & 47 deletions packages/bot-skeleton/src/services/api/ticks_service.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,6 @@ export default class TicksService {
observe() {
if (api_base.api) {
const subscription = api_base.api.onMessage().subscribe(({ data }) => {
if (data.msg_type === 'history') {
const {
subscription: { id },
} = data;
this.subscriptions = this.subscriptions.set('history', id);
}

if (data.msg_type === 'tick') {
const { tick } = data;
const { symbol, id } = tick;
Expand Down Expand Up @@ -290,11 +283,11 @@ export default class TicksService {
});
}

forget = subscription_id => {
forget = () => {
return new Promise((resolve, reject) => {
if (subscription_id) {
if (api_base?.api) {
api_base.api
.forget(subscription_id)
.forgetAll('ticks')
.then(() => {
resolve();
})
Expand All @@ -305,47 +298,33 @@ export default class TicksService {
});
};

unsubscribeFromTicksService() {
forgetCandleSubscription = () => {
return new Promise((resolve, reject) => {
if (this.ticks_history_promise) {
const { stringified_options } = this.ticks_history_promise;
const { symbol = '' } = JSON.parse(stringified_options);
if (symbol) {
if (!this.subscriptions.getIn(['tick', symbol])) {
this.forget(this.subscriptions.get('history'))
.then(res => {
resolve(res);
})
.catch(reject);
} else {
this.forget(this.subscriptions.getIn(['tick', symbol]))
.then(res => {
resolve(res);
})
.catch(reject);
}
} else {
resolve();
}
if (api_base?.api) {
api_base.api
.forgetAll('candles')
.then(() => {
resolve();
})
.catch(reject);
} else {
resolve();
}
this.ticks_history_promise = null;
if (this.candles_promise) {
const { stringified_options } = this.candles_promise;
const { symbol = '' } = JSON.parse(stringified_options);
if (symbol) {
this.forget(this.subscriptions.getIn(['candle', symbol]))
.then(res => {
resolve(res);
});
};

unsubscribeFromTicksService() {
return new Promise((resolve, reject) => {
this.forget()
.then(() => {
this.forgetCandleSubscription()
.then(() => {
resolve();
})
.catch(reject);
} else {
resolve();
}
}

if (!this.ticks_history_promise && !this.candles_promise) {
resolve();
}
})
.catch(reject);
this.ticks_history_promise = null;
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,14 @@ const Interpreter = () => {
);

if (!bot.tradeEngine.contractId && is_timeouts_cancellable) {
api_base.is_stopping = true;
// When user is rate limited, allow them to stop the bot immediately
// granted there is no active contract.
global_timeouts.forEach(timeout => clearTimeout(global_timeouts[timeout]));
terminateSession().then(() => resolve());
terminateSession().then(() => {
api_base.is_stopping = false;
resolve();
});
} else if (
bot.tradeEngine.isSold === false &&
!$scope.is_error_triggered &&
Expand All @@ -182,7 +186,11 @@ const Interpreter = () => {
}
});
} else {
terminateSession().then(() => resolve());
api_base.is_stopping = true;
terminateSession().then(() => {
api_base.is_stopping = false;
resolve();
});
}
} catch (e) {
reject(e);
Expand Down
1 change: 1 addition & 0 deletions packages/bot-web-ui/src/stores/run-panel-store.js
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,7 @@ export default class RunPanelStore {
unregisterBotListeners = () => {
observer.unregisterAll('bot.running');
observer.unregisterAll('bot.stop');
observer.unregisterAll('bot.click_stop');
observer.unregisterAll('bot.trade_again');
observer.unregisterAll('contract.status');
observer.unregisterAll('bot.contract');
Expand Down

0 comments on commit 9a02b06

Please sign in to comment.