From 789e27d627d2a8d9c12a89793d63ba621764a2cb Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Mon, 2 May 2022 18:20:50 +0100 Subject: [PATCH] Port source dashboard processes to async --- handler/sources/agora.py | 34 ++++++++++++++++++++------------ handler/sources/local.py | 0 handler/sources/localbitcoins.py | 32 +++++++++++++++++++----------- 3 files changed, 42 insertions(+), 24 deletions(-) create mode 100644 handler/sources/local.py diff --git a/handler/sources/agora.py b/handler/sources/agora.py index 7755ed3..f3841c4 100644 --- a/handler/sources/agora.py +++ b/handler/sources/agora.py @@ -1,6 +1,7 @@ # Twisted/Klein imports from twisted.internet.task import LoopingCall from twisted.internet.threads import deferToThread +from twisted.internet.defer import inlineCallbacks # Other library imports from json import loads @@ -13,6 +14,8 @@ from datetime import datetime from settings import settings import util +# import sources.local + class Agora(util.Base): """ @@ -41,15 +44,24 @@ class Agora(util.Base): """ Set up the LoopingCall to get all active trades and messages. """ + self.loop_check() + self.log.debug("Setting up loops.") self.lc_dash = LoopingCall(self.loop_check) self.lc_dash.start(int(settings.Agora.RefreshSec)) if settings.Agora.Cheat == "1": self.lc_cheat = LoopingCall(self.run_cheat_in_thread) self.lc_cheat.start(int(settings.Agora.CheatSec)) + self.log.debug("Finished setting up loops.") - @util.handle_exceptions - def wrap_dashboard(self): - dash = self.agora.dashboard_seller() + @inlineCallbacks + def got_dashboard(self, dash): + dash_tmp = yield self.wrap_dashboard(dash) + self.dashboard_hook(dash_tmp) + + @inlineCallbacks + def wrap_dashboard(self, dash=None): # backwards compatibility with TX + if not dash: + dash = yield self.agora.dashboard_seller() if dash is None: return False if dash is False: @@ -72,16 +84,14 @@ class Agora(util.Base): """ Calls hooks to parse dashboard info and get all contact messages. """ - dash_tmp = self.wrap_dashboard() - - # Call dashboard hooks - self.dashboard_hook(dash_tmp) + d = self.agora.dashboard_seller() + d.addCallback(self.got_dashboard) # Get recent messages - self.get_recent_messages() - return dash_tmp + m = self.agora.recent_messages() + m.addCallback(self.got_recent_messages) - def get_dashboard(self): + def get_dashboard_irc(self): """ Get dashboard helper for IRC only. """ @@ -157,13 +167,11 @@ class Agora(util.Base): current_trades.append(reference) self.tx.cleanup(current_trades) - @util.handle_exceptions - def get_recent_messages(self, send_irc=True): + def got_recent_messages(self, messages, send_irc=True): """ Get recent messages. """ messages_tmp = {} - messages = self.agora.recent_messages() if messages is False: return False if not messages["success"]: diff --git a/handler/sources/local.py b/handler/sources/local.py new file mode 100644 index 0000000..e69de29 diff --git a/handler/sources/localbitcoins.py b/handler/sources/localbitcoins.py index 6cf9dfb..25bcaff 100644 --- a/handler/sources/localbitcoins.py +++ b/handler/sources/localbitcoins.py @@ -1,6 +1,7 @@ # Twisted/Klein imports from twisted.internet.task import LoopingCall from twisted.internet.threads import deferToThread +from twisted.internet.defer import inlineCallbacks # Other library imports from json import loads @@ -13,6 +14,8 @@ from datetime import datetime from settings import settings import util +# import sources.local + class LBTC(util.Base): """ @@ -41,15 +44,25 @@ class LBTC(util.Base): """ Set up the LoopingCall to get all active trades and messages. """ + self.log.debug("Running loop check.") + self.loop_check() + self.log.debug("Setting up loops.") self.lc_dash = LoopingCall(self.loop_check) self.lc_dash.start(int(settings.LocalBitcoins.RefreshSec)) if settings.LocalBitcoins.Cheat == "1": self.lc_cheat = LoopingCall(self.run_cheat_in_thread) self.lc_cheat.start(int(settings.LocalBitcoins.CheatSec)) + self.log.debug("Finished setting up loops.") + + @inlineCallbacks + def got_dashboard(self, dash): + dash_tmp = yield self.wrap_dashboard(dash) + self.dashboard_hook(dash_tmp) @util.handle_exceptions - def wrap_dashboard(self): - dash = self.lbtc.dashboard() # no dashboard_seller for lbtc + def wrap_dashboard(self, dash=None): # backwards compatibility with TX + if not dash: + dash = self.lbtc.dashboard() # no dashboard_seller for lbtc if dash is None: return False if dash is False: @@ -72,16 +85,14 @@ class LBTC(util.Base): """ Calls hooks to parse dashboard info and get all contact messages. """ - dash_tmp = self.wrap_dashboard() - - # Call dashboard hooks - self.dashboard_hook(dash_tmp) + d = self.lbtc.dashboard() + d.addCallback(self.got_dashboard) # Get recent messages - self.get_recent_messages() - return dash_tmp + m = self.lbtc.recent_messages() + m.addCallback(self.got_recent_messages) - def get_dashboard(self): + def get_dashboard_irc(self): """ Get dashboard helper for IRC only. """ @@ -154,12 +165,11 @@ class LBTC(util.Base): self.tx.cleanup(current_trades) @util.handle_exceptions - def get_recent_messages(self, send_irc=True): + def got_recent_messages(self, messages, send_irc=True): """ Get recent messages. """ messages_tmp = {} - messages = self.lbtc.recent_messages() if messages is False: return False if not messages["success"]: