def _is_expired(self, host):
"""Check if the given host is considered to be expired.
Expiration is based on the value of HOST_TTL.
:param host: the host dictionary with check in info
:type: list(dict)
:returns: True if host entry expired, False otherwise
:rtype: bool
"""
last_check_in = host['last_check_in']
now = pytz.utc.localize(datetime.datetime.utcnow())
if not last_check_in.tzinfo:
last_check_in = pytz.utc.localize(last_check_in)
# datetime.now(tz.tzutc()) and datetime.utcnow() do not return a tz-aware datetime
# as a result, we use pytz to localize the timestamp to the UTC timezone
time_elapsed = (now - last_check_in).total_seconds()
if time_elapsed > settings.value.HOST_TTL:
logging.info(
"Expiring host %s for service %s because %d seconds have elapsed since last_checkin"
% (host['tags']['instance_id'], host['service'], time_elapsed)
)
return True
else:
return False
评论列表
文章目录