Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added check for pre-existing internet connection #491

Merged
merged 3 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions opwen_email_client/util/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from socket import create_connection
from socket import gethostbyname


def check_connection(hostname: str, port: int) -> bool:
try:
host = gethostbyname(hostname)
with create_connection((host, 80)):
return True
except OSError:
pass
return False
22 changes: 13 additions & 9 deletions opwen_email_client/webapp/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
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
Expand Down Expand Up @@ -36,16 +37,19 @@ 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,
)
if check_connection(AppConfig.STORAGE_ACCOUNT_HOST, 80):
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()
if AppConfig.SIM_TYPE != 'LocalOnly':
with start_internet_connection():
sync_emails()


# noinspection PyUnusedLocal
Expand Down