Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/aleph/vm/orchestrator/views/operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ async def stream_logs(request: web.Request) -> web.StreamResponse:
logger.debug(message)

await ws.send_json({"type": log_type, "message": message})
queue.task_done()

finally:
await ws.close()
Expand Down
29 changes: 20 additions & 9 deletions src/aleph/vm/utils/logs.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import asyncio
import logging
from collections.abc import Callable, Generator
from datetime import datetime
from typing import TypedDict
from datetime import datetime, timedelta
from typing import List, TypedDict

from systemd import journal

Expand Down Expand Up @@ -35,27 +35,38 @@
r = journal.Reader()
r.add_match(SYSLOG_IDENTIFIER=stdout_identifier)
r.add_match(SYSLOG_IDENTIFIER=stderr_identifier)
queue: asyncio.Queue = asyncio.Queue(maxsize=1000)
queue: asyncio.Queue = asyncio.Queue(maxsize=5)
tasks: List[asyncio.Future] = []

Check warning on line 39 in src/aleph/vm/utils/logs.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/utils/logs.py#L38-L39

Added lines #L38 - L39 were not covered by tests

def _ready_for_read() -> None:
change_type = r.process() # reset fd status
if change_type != journal.APPEND:
return
async def process_messages() -> None:
loop.remove_reader(r.fileno())

Check warning on line 42 in src/aleph/vm/utils/logs.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/utils/logs.py#L41-L42

Added lines #L41 - L42 were not covered by tests
entry: EntryDict
for entry in r:
log_type = "stdout" if entry["SYSLOG_IDENTIFIER"] == stdout_identifier else "stderr"
msg = entry["MESSAGE"]
asyncio.create_task(queue.put((log_type, msg)))
await queue.put((log_type, msg))
r.process() # reset fd status
r.process() # reset fd status
loop.add_reader(r.fileno(), _ready_for_read)

Check warning on line 50 in src/aleph/vm/utils/logs.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/utils/logs.py#L47-L50

Added lines #L47 - L50 were not covered by tests

def _ready_for_read() -> None:
task = loop.create_task(process_messages(), name=f"process_messages-queue-{id(queue)}")
tasks.append(task)
task.add_done_callback(tasks.remove)

Check warning on line 55 in src/aleph/vm/utils/logs.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/utils/logs.py#L52-L55

Added lines #L52 - L55 were not covered by tests

if skip_past:
r.seek_tail()
# seek_tail doesn't work see https://github.com/systemd/systemd/issues/17662
r.seek_realtime(datetime.now() - timedelta(seconds=10))

Check warning on line 59 in src/aleph/vm/utils/logs.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/utils/logs.py#L59

Added line #L59 was not covered by tests

loop = asyncio.get_event_loop()
loop.add_reader(r.fileno(), _ready_for_read)
r.process()

Check warning on line 63 in src/aleph/vm/utils/logs.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/utils/logs.py#L63

Added line #L63 was not covered by tests

def do_cancel():
logger.info(f"cancelling reader {r}")
loop.remove_reader(r.fileno())
for task in tasks:
task.cancel()

Check warning on line 69 in src/aleph/vm/utils/logs.py

View check run for this annotation

Codecov / codecov/patch

src/aleph/vm/utils/logs.py#L69

Added line #L69 was not covered by tests
r.close()

return queue, do_cancel
Expand Down
Loading