Skip to content

Commit

Permalink
Add retry options
Browse files Browse the repository at this point in the history
  • Loading branch information
pvizeli committed Mar 1, 2019
1 parent 82aa34e commit cc035a1
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
4 changes: 2 additions & 2 deletions hass_nabucasa/acme.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,8 +337,8 @@ async def issue_certificate(self) -> None:

# Finish validation
try:
_LOGGER.info("Wait 90sec for publishing DNS to ACME provider")
await asyncio.sleep(90)
_LOGGER.info("Wait 60sec for publishing DNS to ACME provider")
await asyncio.sleep(60)
await self.cloud.run_executor(self._finish_challenge, challenge)
finally:
await cloud_api.async_remote_challenge_cleanup(self.cloud, challenge.validation)
Expand Down
1 change: 0 additions & 1 deletion hass_nabucasa/auth_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ async def async_setup(cloud):

async def handle_token_refresh():
"""Handle Cloud access token refresh."""
sleep_time = 5
sleep_time = random.randint(2400, 3600)
while True:
try:
Expand Down
30 changes: 28 additions & 2 deletions hass_nabucasa/remote.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
"""Manage remote UI connections."""
import asyncio
from contextlib import suppress
import logging
import random
import ssl
from typing import Optional

Expand All @@ -9,7 +12,7 @@
from snitun.utils.aiohttp_client import SniTunClientAioHttp

from . import cloud_api
from .acme import AcmeHandler, AcmeClientError
from .acme import AcmeClientError, AcmeHandler

_LOGGER = logging.getLogger(__name__)

Expand All @@ -35,6 +38,7 @@ def __init__(self, cloud):
self._acme = None
self._snitun = None
self._snitun_server = None
self._reconnect_task = None

# Register start/stop
cloud.iot.register_on_connect(self.load_backend)
Expand Down Expand Up @@ -105,8 +109,13 @@ async def load_backend(self) -> None:

async def close_backend(self) -> None:
"""Close connections and shutdown backend."""
if self._reconnect_task:
self._reconnect_task.cancel()

# Disconnect snitun
if self._snitun:
await self._snitun.stop()
with suppress(RuntimeError):
await self._snitun.stop()

self._snitun = None
self._acme = None
Expand Down Expand Up @@ -135,3 +144,20 @@ async def _connect_snitun(self):
data = await resp.json()

await self._snitun.connect(data["token"].encode(), aes_key, aes_iv)

# start retry task
if self._reconnect_task:
return
self._reconnect_task = self.cloud.run_task(self._reconnect_snitun())

async def _reconnect_snitun(self):
"""Reconnect after disconnect."""
try:
while True:
await self._snitun.wait()
await asyncio.sleep(random.randint(1, 10))
await self._connect_snitun()
except asyncio.CancelledError:
pass
finally:
self._reconnect_task = None

0 comments on commit cc035a1

Please sign in to comment.