add try/except logic
This commit is contained in:
parent
d6535affec
commit
c79c363388
|
@ -3,6 +3,9 @@ import time
|
||||||
|
|
||||||
import requests
|
import requests
|
||||||
import socket
|
import socket
|
||||||
|
|
||||||
|
from urllib3.exceptions import NewConnectionError
|
||||||
|
|
||||||
from killswitch import KillSwitch
|
from killswitch import KillSwitch
|
||||||
from timber import LumberJack, axe
|
from timber import LumberJack, axe
|
||||||
from config import _CLIENT_CONFIG as _c
|
from config import _CLIENT_CONFIG as _c
|
||||||
|
@ -33,8 +36,16 @@ class PowerClient:
|
||||||
self.update_config()
|
self.update_config()
|
||||||
|
|
||||||
def get_config(self):
|
def get_config(self):
|
||||||
|
try:
|
||||||
c = requests.get(self.config_url)
|
c = requests.get(self.config_url)
|
||||||
if c.status_code == 200:
|
except NewConnectionError as NCE:
|
||||||
|
axe.error(f'Error: {NCE}')
|
||||||
|
c = None
|
||||||
|
except requests.exceptions.ConnectionError as CE:
|
||||||
|
axe.error(f'Error: {CE}')
|
||||||
|
c = None
|
||||||
|
|
||||||
|
if c and c.status_code == 200:
|
||||||
return c.json()
|
return c.json()
|
||||||
# return json.loads(c.json())
|
# return json.loads(c.json())
|
||||||
else:
|
else:
|
||||||
|
@ -115,7 +126,18 @@ class PowerClient:
|
||||||
return hn
|
return hn
|
||||||
|
|
||||||
def get_status_code(self):
|
def get_status_code(self):
|
||||||
return requests.get(self.url)
|
try:
|
||||||
|
r = requests.get(self.url)
|
||||||
|
except NewConnectionError as NCE:
|
||||||
|
axe.error(f'Error: {NCE}')
|
||||||
|
r = None
|
||||||
|
except requests.exceptions.ConnectionError as CE:
|
||||||
|
axe.error(f'Error: {CE}')
|
||||||
|
r = None
|
||||||
|
return r
|
||||||
|
|
||||||
|
def set_unhealthy(self):
|
||||||
|
pass
|
||||||
|
|
||||||
def check_power(self):
|
def check_power(self):
|
||||||
axe.debug(f'Checking {self.url} for power with {self.RETRY_ATTEMPTS} retry attempts, DRYRUN={self.DRYRUN}')
|
axe.debug(f'Checking {self.url} for power with {self.RETRY_ATTEMPTS} retry attempts, DRYRUN={self.DRYRUN}')
|
||||||
|
@ -130,6 +152,7 @@ class PowerClient:
|
||||||
self.update_config()
|
self.update_config()
|
||||||
self.config_check_count = 0
|
self.config_check_count = 0
|
||||||
|
|
||||||
|
if response:
|
||||||
if response.ok and response.status_code == 200:
|
if response.ok and response.status_code == 200:
|
||||||
if self.RETRY_COUNT == 1:
|
if self.RETRY_COUNT == 1:
|
||||||
axe.info(f'Power is up')
|
axe.info(f'Power is up')
|
||||||
|
@ -151,6 +174,20 @@ class PowerClient:
|
||||||
ks = KillSwitch(DRYRUN=self.DRYRUN)
|
ks = KillSwitch(DRYRUN=self.DRYRUN)
|
||||||
ks.power_down()
|
ks.power_down()
|
||||||
break
|
break
|
||||||
|
else:
|
||||||
|
self.RETRY_SECONDS = self.UNHEALTHY_RETRY_SECONDS
|
||||||
|
if self.RETRY_COUNT < int(math.floor(self.RETRY_ATTEMPTS / 2)):
|
||||||
|
axe.warning(f'Attempt {self.RETRY_COUNT} request failed, power may be down. \
|
||||||
|
Will try {remaining_tries} more times before initiating shutdown.')
|
||||||
|
elif self.RETRY_COUNT < self.RETRY_ATTEMPTS:
|
||||||
|
axe.error(f'Attempt {self.RETRY_COUNT} requests failed, power appears down.\
|
||||||
|
Will try {remaining_tries} more times before initiating shutdown.')
|
||||||
|
else:
|
||||||
|
axe.critical(f'Attempt {self.RETRY_COUNT} requests failed, power appears down.\
|
||||||
|
Engaging Killswitch!')
|
||||||
|
ks = KillSwitch(DRYRUN=self.DRYRUN)
|
||||||
|
ks.power_down()
|
||||||
|
break
|
||||||
|
|
||||||
time.sleep(self.RETRY_SECONDS)
|
time.sleep(self.RETRY_SECONDS)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue