Skip to content

Commit 8b43429

Browse files
Better Logging to investigate #374
1 parent 556b788 commit 8b43429

File tree

2 files changed

+53
-29
lines changed

2 files changed

+53
-29
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/) and this p
1010
[How to upgrade to the latest version!](https://unicorn-binance-websocket-api.docs.lucit.tech/readme.html#installation-and-upgrade)
1111

1212
## 2.5.0.dev (development stage/unreleased/unstable)
13+
### Added
14+
- Better Logging to investigate [issue#374](https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/issues/374)
1315

1416
## 2.5.0
1517
Functionally, nothing changes with this update. However, there are now sensible error messages if errors occur in the

unicorn_binance_websocket_api/manager.py

Lines changed: 51 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1364,7 +1364,7 @@ def clear_stream_buffer(self, stream_buffer_name=False):
13641364
except KeyError:
13651365
return False
13661366

1367-
def create_payload(self, stream_id, method, channels=False, markets=False):
1367+
def create_payload(self, stream_id, method, channels=None, markets=None):
13681368
"""
13691369
Create the payload for subscriptions
13701370
@@ -1380,10 +1380,16 @@ def create_payload(self, stream_id, method, channels=False, markets=False):
13801380
"""
13811381
logger.info("BinanceWebSocketApiManager.create_payload(" + str(stream_id) + ", " + str(channels) + ", " +
13821382
str(markets) + ") started ...")
1383-
if type(channels) is str:
1384-
channels = [channels]
1385-
if type(markets) is str:
1386-
markets = [markets]
1383+
if channels is None or markets is None:
1384+
logger.info(f"BinanceWebSocketApiManager.create_payload({str(stream_id)}) - `channels` and `markets` must "
1385+
f"be specified!")
1386+
return None
1387+
if channels is not None:
1388+
if type(channels) is str:
1389+
channels = [channels]
1390+
if markets is not None:
1391+
if type(markets) is str:
1392+
markets = [markets]
13871393
payload = []
13881394
if self.is_exchange_type("dex"):
13891395
if method == "subscribe" and channels is not False:
@@ -1441,7 +1447,7 @@ def create_payload(self, stream_id, method, channels=False, markets=False):
14411447
logger.critical("BinanceWebSocketApiManager.create_payload(" + str(stream_id) + ", "
14421448
+ str(channels) + ", " + str(markets) + ") - Allowed values for `method`: `subscribe` "
14431449
"or `unsubscribe`!")
1444-
return False
1450+
return None
14451451
elif self.is_exchange_type("cex"):
14461452
final_market = "@arr"
14471453
if markets:
@@ -1496,7 +1502,7 @@ def create_payload(self, stream_id, method, channels=False, markets=False):
14961502
logger.critical("BinanceWebSocketApiManager.create_payload(" + str(stream_id) + ", "
14971503
+ str(channels) + ", " + str(markets) + ") - Allowed values for `method`: `subscribe` "
14981504
"or `unsubscribe`!")
1499-
return False
1505+
return None
15001506
logger.info("BinanceWebSocketApiManager.create_payload(" + str(stream_id) + ", "
15011507
+ str(channels) + ", " + str(markets) + ") - Payload: " + str(payload))
15021508
logger.info("BinanceWebSocketApiManager.create_payload(" + str(stream_id) + ", " + str(channels) + ", " +
@@ -4071,11 +4077,15 @@ def split_payload(self, params, method, max_items_per_request=350):
40714077
payload.append(add_payload)
40724078
return payload
40734079
else:
4074-
return False
4080+
logger.error(f"BinanceWebSocketApiManager.split_payload() CEX result is None!")
4081+
return None
40754082
elif self.is_exchange_type('dex'):
4076-
pass
4083+
# Todo: ???
4084+
logger.error(f"BinanceWebSocketApiManager.split_payload() DEX result is None!")
4085+
return None
40774086
else:
4078-
return False
4087+
logger.error(f"BinanceWebSocketApiManager.split_payload() result is None!")
4088+
return None
40794089

40804090
def start_monitoring_api(self, host='127.0.0.1', port=64201, warn_on_update=True):
40814091
"""
@@ -4258,7 +4268,7 @@ def _stream_is_stopping(self, stream_id: str = None) -> bool:
42584268
self.send_stream_signal(stream_id=stream_id, signal_type="STOP")
42594269
return True
42604270

4261-
def subscribe_to_stream(self, stream_id, channels=[], markets=[]):
4271+
def subscribe_to_stream(self, stream_id, channels=None, markets=None):
42624272
"""
42634273
Subscribe channels and/or markets to an existing stream
42644274
@@ -4278,24 +4288,30 @@ def subscribe_to_stream(self, stream_id, channels=[], markets=[]):
42784288
"""
42794289
logger.info(f"BinanceWebSocketApiManager.subscribe_to_stream(" + str(stream_id) + ", " + str(channels) +
42804290
f", " + str(markets) + f"){self.get_debug_log()} - started ... -")
4281-
if type(channels) is str:
4282-
channels = [channels]
4283-
if type(markets) is str:
4284-
markets = [markets]
4285-
if type(channels) is set:
4286-
channels = list(channels)
4287-
if type(markets) is set:
4288-
markets = list(markets)
4291+
4292+
if channels is not None:
4293+
if type(channels) is str:
4294+
channels = [channels]
4295+
if type(channels) is set:
4296+
channels = list(channels)
4297+
4298+
if markets is not None:
4299+
if type(markets) is str:
4300+
markets = [markets]
4301+
if type(markets) is set:
4302+
markets = list(markets)
4303+
42894304
if type(self.stream_list[stream_id]['channels']) is str:
42904305
self.stream_list[stream_id]['channels'] = [self.stream_list[stream_id]['channels']]
4291-
if type(self.stream_list[stream_id]['markets']) is str:
4292-
self.stream_list[stream_id]['markets'] = [self.stream_list[stream_id]['markets']]
42934306
if type(self.stream_list[stream_id]['channels']) is set:
42944307
self.stream_list[stream_id]['channels'] = list(self.stream_list[stream_id]['channels'])
4308+
if type(self.stream_list[stream_id]['markets']) is str:
4309+
self.stream_list[stream_id]['markets'] = [self.stream_list[stream_id]['markets']]
42954310
if type(self.stream_list[stream_id]['markets']) is set:
42964311
self.stream_list[stream_id]['markets'] = list(self.stream_list[stream_id]['markets'])
42974312

42984313
self.stream_list[stream_id]['channels'] = list(set(self.stream_list[stream_id]['channels'] + channels))
4314+
42994315
markets_new = []
43004316
for market in markets:
43014317
if "!" in market \
@@ -4310,30 +4326,36 @@ def subscribe_to_stream(self, stream_id, channels=[], markets=[]):
43104326
elif self.is_exchange_type('cex'):
43114327
markets_new.append(str(market).lower())
43124328
self.stream_list[stream_id]['markets'] = list(set(self.stream_list[stream_id]['markets'] + markets_new))
4329+
43134330
payload = self.create_payload(stream_id, "subscribe",
43144331
channels=self.stream_list[stream_id]['channels'],
43154332
markets=self.stream_list[stream_id]['markets'])
43164333
self.stream_list[stream_id]['subscriptions'] = self.get_number_of_subscriptions(stream_id)
4334+
43174335
# control subscription limit:
4318-
# https://github.com/binance-exchange/binance-official-api-docs/blob/5fccfd572db2f530e25e302c02be5dec12759cf9/CHANGELOG.md#2020-04-23
4336+
# https://github.com/LUCIT-Systems-and-Development/unicorn-binance-websocket-api/wiki/Binance-websocket-endpoint-configuration-overview
43194337
if self.stream_list[stream_id]['subscriptions'] > self.max_subscriptions_per_stream:
4320-
error_msg = "The limit of " + str(self.max_subscriptions_per_stream) + " subscriptions per stream has " \
4321-
"been exceeded!"
4322-
logger.critical(f"BinanceWebSocketApiManager.subscribe_to_stream({str(stream_id)}) "
4323-
f"Info: {str(error_msg)}")
4338+
error_msg = (f"The limit of {str(self.max_subscriptions_per_stream)} subscriptions per stream has been "
4339+
f"exceeded!")
4340+
logger.error(f"BinanceWebSocketApiManager.subscribe_to_stream({str(stream_id)}) - Info: {str(error_msg)}")
43244341
self._crash_stream(stream_id, error_msg=error_msg)
43254342
return False
43264343

4344+
if payload is None:
4345+
logger.error(f"BinanceWebSocketApiManager.subscribe_to_stream({str(stream_id)}) - Info: Payload is None!")
4346+
return False
4347+
43274348
try:
43284349
for item in payload:
43294350
self.stream_list[stream_id]['payload'].append(item)
4330-
logger.info("BinanceWebSocketApiManager.subscribe_to_stream(" + str(stream_id) + ", " + str(channels) +
4331-
", " + str(markets) + ") finished ...")
4351+
logger.info(f"BinanceWebSocketApiManager.subscribe_to_stream({str(stream_id)}, {str(channels)}, "
4352+
f"{str(markets)}) finished ...")
4353+
return True
43324354
except TypeError as error_msg:
43334355
logger.error(f"BinanceWebSocketApiManager.subscribe_to_stream({str(stream_id)}) - TypeError - "
43344356
f"{str(error_msg)}")
43354357
return False
4336-
return True
4358+
43374359

43384360
def unsubscribe_from_stream(self, stream_id, channels=None, markets=None):
43394361
"""

0 commit comments

Comments
 (0)