From 3330f0c325746573a8c8ef15100b1eab51ba65bf Mon Sep 17 00:00:00 2001 From: Adam Sclafani Date: Wed, 12 Aug 2020 11:40:22 -1000 Subject: [PATCH 1/3] Added check for pre-existing internet connection --- opwen_email_client/webapp/tasks.py | 33 ++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/opwen_email_client/webapp/tasks.py b/opwen_email_client/webapp/tasks.py index ddf54035..e58be788 100644 --- a/opwen_email_client/webapp/tasks.py +++ b/opwen_email_client/webapp/tasks.py @@ -1,3 +1,6 @@ +from socket import create_connection +from socket import gethostbyname + from celery import Celery from celery.schedules import crontab from celery.utils.log import get_task_logger @@ -36,16 +39,28 @@ def sync(*args, **kwargs): user_store=webapp.ioc.user_store, ) - start_internet_connection = StartInternetConnection( - state_dir=AppConfig.STATE_BASEDIR, - modem_config_dir=AppConfig.MODEM_CONFIG_DIR, - sim_config_dir=AppConfig.SIM_CONFIG_DIR, - sim_type=AppConfig.SIM_TYPE, - ) + def check_connection(): + try: + host = gethostbyname(AppConfig.STORAGE_ACCOUNT_HOST) + with create_connection((host, 80)): + return True + except OSError: + pass + return False - if AppConfig.SIM_TYPE != 'LocalOnly': - with start_internet_connection(): - sync_emails() + if check_connection(): + sync_emails() + else: + start_internet_connection = StartInternetConnection( + state_dir=AppConfig.STATE_BASEDIR, + modem_config_dir=AppConfig.MODEM_CONFIG_DIR, + sim_config_dir=AppConfig.SIM_CONFIG_DIR, + sim_type=AppConfig.SIM_TYPE, + ) + + if AppConfig.SIM_TYPE != 'LocalOnly': + with start_internet_connection(): + sync_emails() # noinspection PyUnusedLocal From 73f666d28801bea8ecbade24eb33690e7180a076 Mon Sep 17 00:00:00 2001 From: Adam Sclafani Date: Mon, 17 Aug 2020 21:38:19 -1000 Subject: [PATCH 2/3] style fixes, moved check_connection --- opwen_email_client/util/network.py | 12 ++++++++++++ opwen_email_client/webapp/tasks.py | 17 +++-------------- 2 files changed, 15 insertions(+), 14 deletions(-) create mode 100644 opwen_email_client/util/network.py diff --git a/opwen_email_client/util/network.py b/opwen_email_client/util/network.py new file mode 100644 index 00000000..4c8d62d1 --- /dev/null +++ b/opwen_email_client/util/network.py @@ -0,0 +1,12 @@ +from socket import create_connection +from socket import gethostbyname + + +def check_connection(hostname, port): + try: + host = gethostbyname(hostname) + with create_connection((host, 80)): + return True + except OSError: + pass + return False diff --git a/opwen_email_client/webapp/tasks.py b/opwen_email_client/webapp/tasks.py index e58be788..fdf7cd39 100644 --- a/opwen_email_client/webapp/tasks.py +++ b/opwen_email_client/webapp/tasks.py @@ -1,6 +1,3 @@ -from socket import create_connection -from socket import gethostbyname - from celery import Celery from celery.schedules import crontab from celery.utils.log import get_task_logger @@ -10,6 +7,7 @@ from opwen_email_client.webapp.actions import SyncEmails from opwen_email_client.webapp.actions import UpdateCode from opwen_email_client.webapp.config import AppConfig +from opwen_email_client.util.network import check_connection app = Celery(__name__, broker=AppConfig.CELERY_BROKER_URL) app.conf.beat_schedule_filename = AppConfig.CELERY_BEAT_SCHEDULE_FILENAME @@ -39,16 +37,7 @@ def sync(*args, **kwargs): user_store=webapp.ioc.user_store, ) - def check_connection(): - try: - host = gethostbyname(AppConfig.STORAGE_ACCOUNT_HOST) - with create_connection((host, 80)): - return True - except OSError: - pass - return False - - if check_connection(): + if check_connection(AppConfig.STORAGE_ACCOUNT_HOST, 80): sync_emails() else: start_internet_connection = StartInternetConnection( @@ -56,7 +45,7 @@ def check_connection(): modem_config_dir=AppConfig.MODEM_CONFIG_DIR, sim_config_dir=AppConfig.SIM_CONFIG_DIR, sim_type=AppConfig.SIM_TYPE, - ) + ) if AppConfig.SIM_TYPE != 'LocalOnly': with start_internet_connection(): From 1106857a3083511de9ebf1df9cb62cc91ab1795d Mon Sep 17 00:00:00 2001 From: Adam Sclafani Date: Tue, 18 Aug 2020 17:42:07 -1000 Subject: [PATCH 3/3] fix isort, add type annotations --- opwen_email_client/util/network.py | 2 +- opwen_email_client/webapp/tasks.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/opwen_email_client/util/network.py b/opwen_email_client/util/network.py index 4c8d62d1..66e29fda 100644 --- a/opwen_email_client/util/network.py +++ b/opwen_email_client/util/network.py @@ -2,7 +2,7 @@ from socket import gethostbyname -def check_connection(hostname, port): +def check_connection(hostname: str, port: int) -> bool: try: host = gethostbyname(hostname) with create_connection((host, 80)): diff --git a/opwen_email_client/webapp/tasks.py b/opwen_email_client/webapp/tasks.py index fdf7cd39..3a1c17d9 100644 --- a/opwen_email_client/webapp/tasks.py +++ b/opwen_email_client/webapp/tasks.py @@ -2,12 +2,12 @@ from celery.schedules import crontab from celery.utils.log import get_task_logger +from opwen_email_client.util.network import check_connection from opwen_email_client.webapp.actions import RestartApp from opwen_email_client.webapp.actions import StartInternetConnection from opwen_email_client.webapp.actions import SyncEmails from opwen_email_client.webapp.actions import UpdateCode from opwen_email_client.webapp.config import AppConfig -from opwen_email_client.util.network import check_connection app = Celery(__name__, broker=AppConfig.CELERY_BROKER_URL) app.conf.beat_schedule_filename = AppConfig.CELERY_BEAT_SCHEDULE_FILENAME