"""Implementation of reachability functionality""" from logging import Logger from threading import Thread from time import sleep from cloudsync.utils.globals import * import requests # REACHABILITY PARAMETERS REACHABILITY_CHECK_PAUSE = 5 REACHABILITY_CYCLES = 3 REACHABILITY_CYCLE_PAUSE = 10 class ReachabilityProvider: def __init__(self, logger: Logger, url_reachability): self.logger = logger self.url_reachability = url_reachability self.reachability = False self.thread = Thread(target=self.reachability_test, daemon=True) self.thread.start() def reachability_test(self): """ Continuously monitors the connection to REACHABILITY_URL """ while True: headers = { 'User-Agent': USER_AGENT, } try: response = requests.get(url=self.url_reachability, headers=headers, timeout=10) status_code = response.status_code except requests.exceptions.RequestException: status_code = INTERNAL_SERVER_ERROR if status_code == OK: if not self.reachability: self.logger.info('Internet UP') self.reachability = True else: if self.reachability: self.logger.warning('Internet DOWN') self.reachability = False sleep(REACHABILITY_CHECK_PAUSE)