Skip to content

Commit

Permalink
Updated main loop in order to handle network error whether there is no
Browse files Browse the repository at this point in the history
network available
  • Loading branch information
Javinator9889 committed Jul 22, 2019
1 parent 7622df1 commit 948cd3d
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 31 deletions.
54 changes: 31 additions & 23 deletions pyCloudFlareUpdater/__main__.py
Expand Up @@ -16,10 +16,13 @@
import traceback
from argparse import ArgumentParser
from argparse import SUPPRESS
from logging import WARNING
from logging import getLogger
from os import makedirs
from os import path
from socket import gaierror
from time import sleep
from urllib.error import URLError

from daemonize import Daemonize

Expand All @@ -43,29 +46,34 @@ def main():
mail=preferences.get_mail(),
proxied=preferences.is_record_behind_proxy())
while loop_continuation:
current_ip = get_machine_public_ip()
log.info("Current machine IP: \"{0}\"".format(current_ip))
if preferences.get_latest_ip() == "0.0.0.0":
preferences.set_latest_ip(net.get_cloudflare_latest_ip())
log.warning("User saved latest IP is not up to date - downloading CloudFlare A Record value: \"{0}\""
try:
current_ip = get_machine_public_ip()
log.info("Current machine IP: \"{0}\"".format(current_ip))
if preferences.get_latest_ip() == "0.0.0.0":
preferences.set_latest_ip(net.get_cloudflare_latest_ip())
log.warning(
"User saved latest IP is not up to date - downloading CloudFlare A Record value: \"{0}\""
.format(preferences.get_latest_ip()))
if preferences.get_latest_ip() != current_ip:
log.info("IP needs an upgrade - OLD IP: {0} | NEW IP: {1}"
.format(preferences.get_latest_ip(), current_ip))
result = net.set_cloudflare_ip(current_ip)
log.info("IP updated correctly! - Operation return code: {0}".format(result))
log.debug("Updating saved IP...")
preferences.set_latest_ip(current_ip)
else:
log.info("IP has not changed - skipping")
if not preferences.is_running_as_daemon():
log.info("This script is only executed once. Finishing...")
loop_continuation = False
else:
log.info("Next check in about {0} minute{1}"
.format((preferences.get_time() / 60),
's' if (preferences.get_time() / 60) > 1 else ''))
sleep(preferences.get_time())
if preferences.get_latest_ip() != current_ip:
log.warning("IP needs an upgrade - OLD IP: {0} | NEW IP: {1}"
.format(preferences.get_latest_ip(), current_ip))
result = net.set_cloudflare_ip(current_ip)
log.info("IP updated correctly! - Operation return code: {0}".format(result))
log.debug("Updating saved IP...")
preferences.set_latest_ip(current_ip)
else:
log.info("IP has not changed - skipping")
except (URLError, gaierror) as network_error:
log.error("Failure to connect to the network - extended explanation: " + str(network_error))
finally:
if not preferences.is_running_as_daemon():
log.info("This script is only executed once. Finishing...")
loop_continuation = False
else:
log.info("Next check in about {0} minute{1}"
.format((preferences.get_time() / 60),
's' if (preferences.get_time() / 60) > 1 else ''))
sleep(preferences.get_time())
except KeyboardInterrupt:
log.warning("Received SIGINT - exiting...")
except Exception as e:
Expand Down Expand Up @@ -191,7 +199,7 @@ def parser():
if preferences.is_record_behind_proxy() != p_args.proxied:
preferences.record_behind_proxy(p_args.proxied)
preferences.save_preferences()
file_handler = setup_logging("cloudflareLogger", preferences.get_log_file())
file_handler = setup_logging("cloudflareLogger", preferences.get_log_file(), level=WARNING)
fds = [file_handler.stream.fileno()]
pid_dir = path.dirname(path.abspath(preferences.get_pid_file()))
if not path.exists(pid_dir):
Expand Down
7 changes: 0 additions & 7 deletions pyCloudFlareUpdater/logging_utils/utils.py
Expand Up @@ -78,10 +78,6 @@ def critical(self, msg):
for log in self.__logs:
log.critical(msg)

def exception(self, msg, *args, exc_info: bool = True, **kwargs):
for log in self.__logs:
log.exception(msg, args, exc_info, kwargs)

def get_loggers(self) -> list:
return self.__logs

Expand Down Expand Up @@ -116,8 +112,5 @@ def warning(self, msg):
def critical(self, msg):
self.__instance.critical(msg)

def exception(self, msg, *args, exc_info: bool = True, **kwargs):
self.__instance.exception(msg, args, exc_info, kwargs)

def get_loggers(self) -> list:
return self.__instance.get_loggers()
3 changes: 2 additions & 1 deletion setup.py
Expand Up @@ -25,7 +25,8 @@
description='DDNS service for dynamically update CloudFlare \'A\' Records',
long_description=long_description,
long_description_content_type='text/markdown',
include_package_data=False,
package_data={'': ['cloud.png', 'api_keys.png']},
include_package_data=True,
zip_safe=True,
download_url="https://gitlab.javinator9889.com/ddns-clients/pyCloudFlareUpdater/repository/master/archive.zip",
entry_points={
Expand Down

0 comments on commit 948cd3d

Please sign in to comment.