Skip to content

Instantly share code, notes, and snippets.

@adam-stokes
Created August 23, 2017 15:44
Show Gist options
  • Save adam-stokes/8789037b1d3b5c64c58485d84da9f1d0 to your computer and use it in GitHub Desktop.
Save adam-stokes/8789037b1d3b5c64c58485d84da9f1d0 to your computer and use it in GitHub Desktop.
diff --git a/conjureup/controllers/clouds/common.py b/conjureup/controllers/clouds/common.py
index 6bd84ba..cd28782 100644
--- a/conjureup/controllers/clouds/common.py
+++ b/conjureup/controllers/clouds/common.py
@@ -1,33 +1,27 @@
import asyncio
-
-from conjureup import events
+from juju.utils import run_with_interrupt
+from conjureup import events, utils
from conjureup.models.provider import LocalhostError, LocalhostJSONError
class BaseCloudController:
- retry_count = 0
- max_retry = 20
+
+ cancel_monitor = asyncio.Event()
async def _monitor_localhost(self, provider, cb):
""" Checks that localhost/lxd is available and listening,
updates widget accordingly
"""
- if events.LXDAvailable.is_set():
- return
- try:
- await provider.is_server_available()
- events.LXDAvailable.set()
- self.retry_count = 0
- cb()
- except (LocalhostError,
- LocalhostJSONError):
- if self.retry_count == self.max_retry:
- raise
- provider._set_lxd_dir_env()
- self.retry_count += 1
- await asyncio.sleep(2)
- await self._monitor_localhost(provider, cb)
- except FileNotFoundError:
- await asyncio.sleep(5)
- await self._monitor_localhost(provider, cb)
+ while not self.cancel_monitor.is_set():
+ try:
+ await provider.is_server_available()
+ events.LXDAvailable.set()
+ cb()
+ except (LocalhostError, LocalhostJSONError):
+ provider._set_lxd_dir_env()
+ await run_with_interrupt(asyncio.sleep(2),
+ self.cancel_monitor)
+ except FileNotFoundError:
+ await run_with_interrupt(asyncio.sleep(5),
+ self.cancel_monitor)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment