import asyncio

from juju.utils import run_with_interrupt

from conjureup import events
from conjureup.models.provider import LocalhostError, LocalhostJSONError


class BaseCloudController:

    cancel_monitor = asyncio.Event()

    async def _monitor_localhost(self, provider, cb):
        """ Checks that localhost/lxd is available and listening,
        updates widget accordingly
        """

        while not self.cancel_monitor.is_set():
            try:
                compatible = await provider.is_server_compatible()
                if compatible:
                    events.LXDAvailable.set()
                    self.cancel_monitor.set()
                    cb()
                    return
            except (LocalhostError, LocalhostJSONError):
                provider._set_lxd_dir_env()
            except FileNotFoundError:
                pass
            await run_with_interrupt(asyncio.sleep(2),
                                     self.cancel_monitor)

import asyncio

from juju.utils import run_with_interrupt

from conjureup import events
from conjureup.models.provider import LocalhostError, LocalhostJSONError


class BaseCloudController:

    cancel_monitor = asyncio.Event()

    async def _monitor_localhost(self, provider, cb):
        """ Checks that localhost/lxd is available and listening,
        updates widget accordingly
        """

        while not self.cancel_monitor.is_set():
            try:
                provider._set_lxd_dir_env()
                compatible = await provider.is_server_compatible()
                if compatible:
                    events.LXDAvailable.set()
                    self.cancel_monitor.set()
                    cb()
                    return
            except (LocalhostError, LocalhostJSONError, FileNotFoundError):
                pass
            await run_with_interrupt(asyncio.sleep(2),
                                     self.cancel_monitor)

Make sure _set_lxd_dir_env is always called in monitor loop
Make sure _set_lxd_dir_env is always called in monitor loop

Signed-off-by: Adam Stokes <49c255c1d074742f60d19fdba5e2aa5a34add567@users.noreply.github.com>

  import asyncio
  
  from juju.utils import run_with_interrupt
  
  from conjureup import events
  from conjureup.models.provider import LocalhostError, LocalhostJSONError
  
  
  class BaseCloudController:
  
      cancel_monitor = asyncio.Event()
  
      async def _monitor_localhost(self, provider, cb):
          """ Checks that localhost/lxd is available and listening,
          updates widget accordingly
          """
  
          while not self.cancel_monitor.is_set():
              try:
+                 provider._set_lxd_dir_env()
                  compatible = await provider.is_server_compatible()
                  if compatible:
                      events.LXDAvailable.set()
                      self.cancel_monitor.set()
                      cb()
                      return
-             except (LocalhostError, LocalhostJSONError):
+             except (LocalhostError, LocalhostJSONError, FileNotFoundError):
-                 provider._set_lxd_dir_env()
-             except FileNotFoundError:
                  pass
              await run_with_interrupt(asyncio.sleep(2),
                                       self.cancel_monitor)
  
Make sure _set_lxd_dir_env is always called in monitor loop
## Code Before:
import asyncio

from juju.utils import run_with_interrupt

from conjureup import events
from conjureup.models.provider import LocalhostError, LocalhostJSONError


class BaseCloudController:

    cancel_monitor = asyncio.Event()

    async def _monitor_localhost(self, provider, cb):
        """ Checks that localhost/lxd is available and listening,
        updates widget accordingly
        """

        while not self.cancel_monitor.is_set():
            try:
                compatible = await provider.is_server_compatible()
                if compatible:
                    events.LXDAvailable.set()
                    self.cancel_monitor.set()
                    cb()
                    return
            except (LocalhostError, LocalhostJSONError):
                provider._set_lxd_dir_env()
            except FileNotFoundError:
                pass
            await run_with_interrupt(asyncio.sleep(2),
                                     self.cancel_monitor)

## Instruction:
Make sure _set_lxd_dir_env is always called in monitor loop
## Code After:
import asyncio

from juju.utils import run_with_interrupt

from conjureup import events
from conjureup.models.provider import LocalhostError, LocalhostJSONError


class BaseCloudController:

    cancel_monitor = asyncio.Event()

    async def _monitor_localhost(self, provider, cb):
        """ Checks that localhost/lxd is available and listening,
        updates widget accordingly
        """

        while not self.cancel_monitor.is_set():
            try:
                provider._set_lxd_dir_env()
                compatible = await provider.is_server_compatible()
                if compatible:
                    events.LXDAvailable.set()
                    self.cancel_monitor.set()
                    cb()
                    return
            except (LocalhostError, LocalhostJSONError, FileNotFoundError):
                pass
            await run_with_interrupt(asyncio.sleep(2),
                                     self.cancel_monitor)
