-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from simonsobs/dev
Update hook arguments, plugins, add hooks
- Loading branch information
Showing
30 changed files
with
383 additions
and
396 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,53 @@ | ||
import dataclasses | ||
import datetime | ||
from datetime import timezone | ||
from typing import Optional | ||
|
||
from nextline import spawned | ||
from nextline.plugin.spec import Context, hookimpl | ||
from nextline.types import RunInfo | ||
from nextline.utils import ExitedProcess | ||
|
||
|
||
class RunInfoRegistrar: | ||
def __init__(self) -> None: | ||
self._script: Optional[str] = None | ||
self._run_info: Optional[RunInfo] = None | ||
|
||
@hookimpl | ||
async def on_change_script(self, script: str) -> None: | ||
self._script = script | ||
|
||
@hookimpl | ||
async def on_initialize_run(self, context: Context) -> None: | ||
assert context.run_arg | ||
if isinstance(context.run_arg.statement, str): | ||
script = context.run_arg.statement | ||
else: | ||
script = None | ||
self._run_info = RunInfo( | ||
run_no=context.run_arg.run_no, state='initialized', script=self._script | ||
run_no=context.run_arg.run_no, state='initialized', script=script | ||
) | ||
await context.pubsub.publish('run_info', self._run_info) | ||
|
||
@hookimpl | ||
async def on_start_run(self, context: Context) -> None: | ||
assert self._run_info is not None | ||
assert context.running_process | ||
assert context.running_process.process_created_at.tzinfo is timezone.utc | ||
started_at = context.running_process.process_created_at.replace(tzinfo=None) | ||
self._run_info = dataclasses.replace( | ||
self._run_info, | ||
state='running', | ||
started_at=datetime.datetime.utcnow(), | ||
self._run_info, state='running', started_at=started_at | ||
) | ||
await context.pubsub.publish('run_info', self._run_info) | ||
|
||
@hookimpl | ||
async def on_end_run( | ||
self, context: Context, exited_process: ExitedProcess[spawned.RunResult] | ||
) -> None: | ||
async def on_end_run(self, context: Context) -> None: | ||
assert self._run_info is not None | ||
run_result = exited_process.returned or spawned.RunResult(ret=None, exc=None) | ||
assert context.exited_process | ||
run_result = context.exited_process.returned | ||
assert run_result | ||
assert context.exited_process.process_exited_at.tzinfo is timezone.utc | ||
ended_at = context.exited_process.process_exited_at.replace(tzinfo=None) | ||
|
||
self._run_info = dataclasses.replace( | ||
self._run_info, | ||
state='finished', | ||
result=run_result.fmt_ret, | ||
exception=run_result.fmt_exc, | ||
ended_at=datetime.datetime.utcnow(), | ||
ended_at=ended_at, | ||
) | ||
await context.pubsub.publish('run_info', self._run_info) | ||
|
||
self._run_info = None |
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
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 |
---|---|---|
@@ -1,8 +1,10 @@ | ||
__all__ = [ | ||
'OnEvent', | ||
'CommandSender', | ||
'Result', | ||
'RunSession', | ||
'Signal', | ||
] | ||
|
||
from .monitor import OnEvent | ||
from .session import CommandSender, Result, RunSession, Signal |
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 |
---|---|---|
@@ -1,54 +1,32 @@ | ||
import asyncio | ||
import time | ||
from collections.abc import AsyncIterator | ||
from contextlib import asynccontextmanager | ||
from logging import getLogger | ||
|
||
from nextline import spawned | ||
from nextline.plugin.spec import Context | ||
from nextline.spawned import QueueOut | ||
from nextline import events | ||
from nextline.plugin.spec import Context, hookimpl | ||
|
||
|
||
@asynccontextmanager | ||
async def relay_queue(context: Context, queue: QueueOut) -> AsyncIterator[None]: | ||
task = asyncio.create_task(_monitor(context, queue)) | ||
try: | ||
yield | ||
finally: | ||
up_to = 0.05 | ||
start = time.process_time() | ||
while not queue.empty() and time.process_time() - start < up_to: | ||
await asyncio.sleep(0) | ||
await asyncio.to_thread(queue.put, None) # type: ignore | ||
await task | ||
|
||
|
||
async def _monitor(context: Context, queue: QueueOut) -> None: | ||
while (event := await asyncio.to_thread(queue.get)) is not None: | ||
await _on_event(context, event) | ||
|
||
|
||
async def _on_event(context: Context, event: spawned.Event) -> None: | ||
ahook = context.hook.ahook | ||
match event: | ||
case spawned.OnStartTrace(): | ||
await ahook.on_start_trace(context=context, event=event) | ||
case spawned.OnEndTrace(): | ||
await ahook.on_end_trace(context=context, event=event) | ||
case spawned.OnStartTraceCall(): | ||
await ahook.on_start_trace_call(context=context, event=event) | ||
case spawned.OnEndTraceCall(): | ||
await ahook.on_end_trace_call(context=context, event=event) | ||
case spawned.OnStartCmdloop(): | ||
await ahook.on_start_cmdloop(context=context, event=event) | ||
case spawned.OnEndCmdloop(): | ||
await ahook.on_end_cmdloop(context=context, event=event) | ||
case spawned.OnStartPrompt(): | ||
await ahook.on_start_prompt(context=context, event=event) | ||
case spawned.OnEndPrompt(): | ||
await ahook.on_end_prompt(context=context, event=event) | ||
case spawned.OnWriteStdout(): | ||
await ahook.on_write_stdout(context=context, event=event) | ||
case _: | ||
logger = getLogger(__name__) | ||
logger.warning(f'Unknown event: {event!r}') | ||
class OnEvent: | ||
@hookimpl | ||
async def on_event_in_process(self, context: Context, event: events.Event) -> None: | ||
ahook = context.hook.ahook | ||
match event: | ||
case events.OnStartTrace(): | ||
await ahook.on_start_trace(context=context, event=event) | ||
case events.OnEndTrace(): | ||
await ahook.on_end_trace(context=context, event=event) | ||
case events.OnStartTraceCall(): | ||
await ahook.on_start_trace_call(context=context, event=event) | ||
case events.OnEndTraceCall(): | ||
await ahook.on_end_trace_call(context=context, event=event) | ||
case events.OnStartCmdloop(): | ||
await ahook.on_start_cmdloop(context=context, event=event) | ||
case events.OnEndCmdloop(): | ||
await ahook.on_end_cmdloop(context=context, event=event) | ||
case events.OnStartPrompt(): | ||
await ahook.on_start_prompt(context=context, event=event) | ||
case events.OnEndPrompt(): | ||
await ahook.on_end_prompt(context=context, event=event) | ||
case events.OnWriteStdout(): | ||
await ahook.on_write_stdout(context=context, event=event) | ||
case _: | ||
logger = getLogger(__name__) | ||
logger.warning(f'Unknown event: {event!r}') |
Oops, something went wrong.