-
Notifications
You must be signed in to change notification settings - Fork 14
feat: Run blocking plans via websockets #1413
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tpoliaw
wants to merge
64
commits into
main
Choose a base branch
from
ws
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,662
−127
Open
Changes from all commits
Commits
Show all changes
64 commits
Select commit
Hold shift + click to select a range
146b480
wip
abbiemery 11e8cb6
client wip
abbiemery f31dbfe
use normal iter
abbiemery d5184c8
close ws
abbiemery 728201c
add some trys
abbiemery 7947e76
unpipe
abbiemery 2e45f72
Move websocket handling into BlueapiRestClient
tpoliaw 09b8a0e
Send all events through websocket
tpoliaw 5fd15a1
Split pipe subscribe handles
tpoliaw ecd9bcd
Re-use run subcommand for websockets
tpoliaw db8964b
Raise for connection closing pre plan completed
abbiemery 5664fd2
Remove run blocking from cli
abbiemery 55c1da5
Catch plan key error in run_plan
abbiemery a698b05
Refactor event pipe handling into context manager and iterable
tpoliaw 67c6d38
Testing auth tokens
tpoliaw 47a8100
Re-use existing auth dependency for websocket endpoint
tpoliaw 46b4f3d
Add user auth token in websocket client
tpoliaw b9cd99e
Read authorization from cookie as well as header
tpoliaw 3960803
Add user agent to websocket request
tpoliaw 2d30d5b
Add user agent to all requests
tpoliaw 0630f63
Use new fedid dependency for user name
tpoliaw 249a29e
Test auth from cookie
tpoliaw 42bbf88
Fix CLI event handler test
tpoliaw 4a514b2
Reinstate _valid_return check
tpoliaw 7ea45ee
Use versioned api for websockets
tpoliaw 0aff363
Add type annotation to unpipe
tpoliaw 452bb13
Move ws endpoint to v2 api
tpoliaw 0fac2ed
Use Depends for header and cookie
tpoliaw a7e9f6b
Add sub-protocol to ws communication
tpoliaw 3c24fb4
Used configured host for websockets
tpoliaw f8b7636
Add debug logging of all websocket traffic
tpoliaw 1a4ee6d
Include connection info in logging
tpoliaw 762507a
Split receive logging by type
tpoliaw 84b7e7c
Correct typing in rest run_blocking
tpoliaw 04ab4af
Use rstrip instead of removesuffix to remove multiple trailing slashes
tpoliaw 29b5452
Redact auth tokens in websocket logging
tpoliaw 7413382
Use send_text instead of send json
tpoliaw ab9cd70
Improve error handling
tpoliaw bb80659
Check for active task before running task
tpoliaw 27a71e3
Filter events to only relevant ones
tpoliaw aa20946
Logging adjustments
tpoliaw b39f076
Exclude all private methods from blueapi_rest_client_get_methods
tpoliaw 7831fc5
Move ws_address method to config and deal with empty paths
tpoliaw 0034710
Add tests for websocket tracing
tpoliaw fc0f218
Handle pydantic's messing up of URL paths
tpoliaw 9f23570
Extract cookies/headers from connection manually
tpoliaw f14bf57
WorkerEvent property tests
tpoliaw 423acb4
More coverage hunting
tpoliaw 2016a58
Add interface pipe tests
tpoliaw 8e97eb5
Fix runner tests
tpoliaw 069708e
More tests
tpoliaw 5748d9d
Start to test main method
tpoliaw d7ba9d6
Add rest client run_blocking tests
tpoliaw 1561eae
test rest ws auth
tpoliaw e503529
Add tests for main run_plan handler
tpoliaw 75998a9
Pin uvicorn to 0.49 until websockets are fixed
tpoliaw 6f6fdcb
Add system test to run a plan via websocket
tpoliaw 90b05e1
Add authz to ws
tpoliaw 041537f
Handle server being missing
tpoliaw 0acaaca
Add websocket auth tests
tpoliaw b65ff9d
Add unit test for ws auth check
tpoliaw a84bba4
Update uvicorn post ws fix
tpoliaw 23642f4
Add service unavailable ws test
tpoliaw d2a8d72
Add Origin checks for websockets
tpoliaw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 +1,8 @@ | ||
| import logging | ||
| from collections.abc import Mapping | ||
| from dataclasses import dataclass | ||
| from functools import cache | ||
| from multiprocessing.connection import Connection | ||
| from typing import Any | ||
|
|
||
| from bluesky.callbacks.tiled_writer import TiledWriter | ||
|
|
@@ -9,6 +12,7 @@ | |
|
|
||
| from blueapi.cli.scratch import get_python_environment | ||
| from blueapi.config import ApplicationConfig, OIDCConfig, ServiceAccount, StompConfig | ||
| from blueapi.core.bluesky_types import DataEvent | ||
| from blueapi.core.context import BlueskyContext | ||
| from blueapi.core.event import EventStream | ||
| from blueapi.log import set_up_logging | ||
|
|
@@ -22,14 +26,14 @@ | |
| WorkerTask, | ||
| ) | ||
| from blueapi.utils.serialization import access_blob | ||
| from blueapi.worker.event import TaskStatusEnum, WorkerEvent, WorkerState | ||
| from blueapi.worker.event import ProgressEvent, TaskStatusEnum, WorkerEvent, WorkerState | ||
| from blueapi.worker.task import Task | ||
| from blueapi.worker.task_worker import TaskWorker, TrackableTask | ||
|
|
||
| """This module provides interface between web application and underlying Bluesky | ||
| context and worker""" | ||
|
|
||
|
|
||
| LOGGER = logging.getLogger(__name__) | ||
| _CONFIG: ApplicationConfig = ApplicationConfig() | ||
|
|
||
|
|
||
|
|
@@ -226,7 +230,7 @@ def remove_callback_when_task_finished( | |
| if task.task_id is not None: | ||
| try: | ||
| active_worker.begin_task(task.task_id) | ||
| except KeyError: | ||
| except: | ||
| for channel, token in subscribers: | ||
| channel.unsubscribe(token) | ||
| raise | ||
|
|
@@ -281,3 +285,37 @@ def get_python_env( | |
| """Retrieve information about the Python environment""" | ||
| scratch = config().scratch | ||
| return get_python_environment(config=scratch, name=name, source=source) | ||
|
|
||
|
|
||
| @dataclass | ||
| class SubHandles: | ||
| worker: int | ||
| progress: int | ||
| data: int | ||
|
|
||
|
|
||
| def pipe_events(tx: Connection) -> SubHandles: | ||
| tw = worker() | ||
|
|
||
| def handler( | ||
| worker_event: WorkerEvent | DataEvent | ProgressEvent, | ||
| _cor_id: str | None, | ||
| ) -> None: | ||
|
|
||
| try: | ||
| tx.send(worker_event) | ||
| except BrokenPipeError: | ||
| LOGGER.warning("Sending event to broken pipe") | ||
| pass | ||
|
tpoliaw marked this conversation as resolved.
Dismissed
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. does 'pass' required here? |
||
|
|
||
| w = tw.worker_events.subscribe(handler) | ||
| d = tw.data_events.subscribe(handler) | ||
| p = tw.progress_events.subscribe(handler) | ||
| return SubHandles(worker=w, data=d, progress=p) | ||
|
|
||
|
|
||
| def unpipe_events(hnd: SubHandles) -> None: | ||
| tw = worker() | ||
| tw.worker_events.unsubscribe(hnd.worker) | ||
| tw.data_events.unsubscribe(hnd.data) | ||
| tw.progress_events.unsubscribe(hnd.progress) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.