-
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
4 changed files
with
132 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import asyncio | ||
import logging | ||
import threading | ||
from typing import Coroutine, TypeVar | ||
|
||
from juju import jasyncio | ||
from juju.client import client, connection | ||
from juju.model import Model | ||
|
||
|
||
async def main() -> None: | ||
m = Model() | ||
await m.connect(model_name="testm") | ||
app_facade = client.ApplicationFacade.from_connection(m.connection()) | ||
|
||
helper = SyncModelConnection() | ||
helper.start() | ||
# FIXME wait for it to start | ||
import time | ||
time.sleep(1) | ||
helper.call(helper.ext_init(**m._connector._kwargs_cache), low_level=True) | ||
|
||
for app_name in m.applications: | ||
print(f"main loop {'-'*40}") | ||
# print(m.applications[app_name].constraints.arch) # temporarily broken, but why? | ||
print(m.applications[app_name].name) | ||
print(m.applications[app_name].exposed) | ||
print(m.applications[app_name].charm_url) | ||
print(m.applications[app_name].owner_tag) | ||
# print(m.applications[app_name].life) | ||
print(m.applications[app_name].min_units) | ||
print(m.applications[app_name].constraints) | ||
print(m.applications[app_name].subordinate) | ||
# print(m.applications[app_name].status) | ||
print(m.applications[app_name].workload_version) | ||
|
||
print() | ||
app = await app_facade.Get(app_name) | ||
print(app.application, app.charm, app.constraints.arch) | ||
|
||
print(f"helper loop {'+'*40}") | ||
app = helper.call(client.ApplicationFacade.from_connection(helper._conn).Get(app_name)) | ||
print(app.application, app.charm, app.constraints.arch) | ||
|
||
helper.call(helper._conn.close()) | ||
helper.stop() | ||
|
||
|
||
R = TypeVar("R") | ||
|
||
|
||
class SyncModelConnection(threading.Thread): | ||
_cond: threading.Condition | ||
_conn: connection.Connection | None | ||
|
||
def __init__(self, *, connection_kwargs: dict = {}): # FIXME | ||
super().__init__() | ||
# FIXME check if we really need this explicit locking | ||
self._cond = threading.Condition() | ||
self._conn = None | ||
self._connection_kwargs = connection_kwargs | ||
|
||
def call(self, coro: Coroutine[None, None, R], *, low_level=False) -> R: | ||
assert self._loop | ||
assert self._conn or low_level | ||
f = asyncio.run_coroutine_threadsafe(coro, self._loop) | ||
return f.result() | ||
|
||
# FIXME one or the other | ||
async def ext_init(self, **kw): | ||
self._conn = await connection.Connection.connect(**kw) | ||
|
||
async def init(self): | ||
try: | ||
self._conn = await connection.Connection.connect(**self._connection_kwargs) | ||
except Exception: | ||
logging.exception("Helper thread failed to connect") | ||
assert self._loop | ||
self._loop.stop() | ||
|
||
def run(self): | ||
with self._cond: | ||
self._loop = asyncio.new_event_loop() | ||
asyncio.set_event_loop(self._loop) | ||
|
||
self._loop.run_forever() | ||
|
||
with self._cond: | ||
self._loop.close() | ||
self._loop = None | ||
# In case another thread was waiting for a result when we were asked to stop | ||
self._cond.notify_all() | ||
|
||
def stop(self): | ||
with self._cond: | ||
if self._loop: | ||
self._loop.call_soon_threadsafe(self._loop.stop) | ||
self.join() | ||
|
||
|
||
class SymbolFilter(logging.Filter): | ||
DEBUG = '🐛' | ||
INFO = 'ℹ️' | ||
WARNING = '⚠️' | ||
ERROR = '❌' | ||
CRITICAL = '🔥' | ||
|
||
def filter(self, record): | ||
record.symbol = getattr(self, record.levelname, '#') | ||
# FIXME can control log record origin here if needed | ||
return True | ||
|
||
|
||
if __name__ == "__main__": | ||
# FIXME why is level=DEBUG broken? | ||
logging.basicConfig(level="INFO", format="%(symbol)s %(message)s") | ||
logging.root.addFilter(SymbolFilter()) | ||
jasyncio.run(main()) |
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