From 18ef7a03502b8e2451b8fbad855a5786a17a5081 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Fri, 18 Feb 2022 08:41:32 +0000 Subject: [PATCH] Run balance checks intermittently --- handler/transactions.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/handler/transactions.py b/handler/transactions.py index 77f9643..7bce16f 100644 --- a/handler/transactions.py +++ b/handler/transactions.py @@ -1,5 +1,7 @@ # Twisted/Klein imports from twisted.logger import Logger +from twisted.internet.task import LoopingCall +from twisted.internet.threads import deferToThread # Other library imports from json import dumps @@ -37,6 +39,25 @@ class Transactions(object): ca_certs="certs/ca.crt", ) + def run_checks_in_thread(self): + """ + Run all the balance checks that output into ES in another thread. + """ + deferToThread(self.get_total) + deferToThread(self.get_remaining) + deferToThread(self.get_profit) + deferToThread(self.get_open_trades_usd) + deferToThread(self.get_total_remaining) + deferToThread(self.get_total_with_trades) + + def setup_loops(self): + """ + Set up the LoopingCalls to get the balance so we have data in ES. + """ + self.lc_es_checks = LoopingCall(self.run_checks_in_thread) + delay = int(settings.ES.RefreshSec) + self.lc_es_checks.start(delay) + # TODO: write tests then refactor, this is terribly complicated! def transaction(self, data): """