-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
119 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
# Copyright 2024 Canonical Ltd. | ||
# Licensed under the Apache V2, see LICENCE file for details. | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import logging | ||
import threading | ||
from typing import Coroutine, TypeVar, Self | ||
|
||
from juju.client import connection | ||
|
||
R = TypeVar("R") | ||
|
||
|
||
class SyncProxy(threading.Thread): | ||
_cond: threading.Condition | ||
_conn: connection.Connection | None | ||
_loop: asyncio.AbstractEventLoop | None | ||
|
||
@classmethod | ||
def new_running(cls, *, connection_kwargs: dict) -> Self: | ||
self = cls(connection_kwargs=connection_kwargs) | ||
self.start() | ||
return self | ||
|
||
def __init__(self, *, connection_kwargs: dict): | ||
super().__init__() | ||
self._cond = threading.Condition() | ||
self._conn = None | ||
self._connection_kwargs = connection_kwargs | ||
|
||
def start(self): | ||
super().start() | ||
|
||
with self._cond: | ||
while not self._loop: | ||
self._cond.wait() | ||
|
||
try: | ||
asyncio.run_coroutine_threadsafe(self._connect(), self._loop).result() | ||
except Exception: | ||
logging.exception("Helper thread failed to connect") | ||
assert self._loop | ||
self._loop.stop() | ||
raise | ||
|
||
def call(self, coro: Coroutine[None, None, R]) -> R: | ||
assert self._loop | ||
assert self._conn # for sanity, really | ||
return asyncio.run_coroutine_threadsafe(coro, self._loop).result() | ||
|
||
async def _connect(self): | ||
try: | ||
self._conn = await connection.Connection.connect(**self._connection_kwargs) | ||
finally: | ||
with self._cond: | ||
self._cond.notify_all() | ||
|
||
def run(self) -> None: | ||
with self._cond: | ||
self._loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(self._loop) | ||
self._cond.notify_all() | ||
|
||
self._loop.run_forever() | ||
|
||
# FIXME not needed, as caller is expected to .join() | ||
with self._cond: | ||
self._loop.close() | ||
self._loop = None | ||
self._cond.notify_all() | ||
|
||
def stop(self) -> None: | ||
try: | ||
if self._conn: | ||
# FIXME somehow I'm getting Bad file description here | ||
# and it's deep in the websockets/legacy/framing... | ||
# And I was not getting the same when in prev revision with manual helper... | ||
self.call(self._conn.close()) | ||
self._conn = None | ||
with self._cond: | ||
if self._loop: | ||
self._loop.call_soon_threadsafe(self._loop.stop) | ||
self.join() | ||
except Exception: | ||
logging.exception("XXXXXXXXX") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters