diff --git a/core/services/subscriptions/check_payment_loop.py b/core/services/subscriptions/check_payment_loop.py index ececea5..c836a6c 100644 --- a/core/services/subscriptions/check_payment_loop.py +++ b/core/services/subscriptions/check_payment_loop.py @@ -20,7 +20,6 @@ MAX_FAILED_CONNECTIONS = 8 - class CheckPaidTimer: _instance = None # CLASS VARIABLE - survives across __init__ calls @@ -48,9 +47,17 @@ class CheckPaidTimer: self.failed_connections = 0 self.total_attempts = 0 - logger.info("Check Paid Timer initialized. Starting polling..") - self.start_polling() - + logger.info("Check Paid Timer initialized.") + # self.start_polling() + + + @classmethod + def get_instance(cls, *args, **kwargs): + if cls._instance is None: + cls(*args, **kwargs) + return cls._instance + + def start_polling(self): logger.info("Starting polling") """If there is a billing code to check if it's paid, it begins the checks.""" @@ -65,8 +72,9 @@ class CheckPaidTimer: self.poll_thread.start() def stop_polling(self): - """Stops checking if paid""" - self.is_running = False + """Stops checking if paid and wipes the instance""" + self.is_running = False # stop checking if paid + CheckPaidTimer._instance = None # wipes instance logger.info("Polling stopped") def _poll_loop(self): @@ -101,6 +109,9 @@ class CheckPaidTimer: print("PAID! Stopping timer") self.stop_polling() + # Reset instance: + CheckPaidTimer._instance = None + # SAVE: marked = mark_as_paid(profile_id=self.profile_id) if not marked.valid: @@ -122,11 +133,13 @@ class CheckPaidTimer: elif result.error_type == ResultError.INVALID_INPUT: print("immediately stop, the billing code or other input is invalid") self.stop_polling() + CheckPaidTimer._instance = None # Reset instance self.view_callback(paid=False, stopped=True, message=result.message) # connection error (timed out). Not related to payment status: elif self.failed_connections > MAX_FAILED_CONNECTIONS: self.stop_polling() + CheckPaidTimer._instance = None # Reset instance message = f"Your network connection timed out after {self.failed_connections} failed attempts" self.view_callback(paid=False, stopped=True, message=message) @@ -141,5 +154,6 @@ class CheckPaidTimer: print(f"result.error_type is {result.error_type}") print(f"result.message is {result.error_type}") self.stop_polling() + CheckPaidTimer._instance = None self.view_callback(paid=False, stopped=True, message=result.message)