Skip to content
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

Remove ability to use internal callbacks #776

Merged
merged 7 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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: 0 additions & 1 deletion helmchart/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ spec:
value: "/dls_sw/apps/zocalo/live/configuration.yaml"
- name: ISPYB_CONFIG_PATH
value: "/dls_sw/dasc/mariadb/credentials/ispyb-hyperion-{{ .Values.hyperion.beamline }}.cfg"
args: [ "--external-callbacks" ]
{{- end }}
readinessProbe:
exec:
Expand Down
14 changes: 3 additions & 11 deletions run_hyperion.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ START=1
SKIP_STARTUP_CONNECTION=false
VERBOSE_EVENT_LOGGING=false
IN_DEV=false
EXTERNAL_CALLBACK_SERVICE=false

for option in "$@"; do
case $option in
Expand All @@ -28,9 +27,6 @@ for option in "$@"; do
--verbose-event-logging)
VERBOSE_EVENT_LOGGING=true
;;
--external-callbacks)
EXTERNAL_CALLBACK_SERVICE=true
;;

--help|--info|--h)

Expand Down Expand Up @@ -119,11 +115,9 @@ if [[ $START == 1 ]]; then

#Add future arguments here
declare -A h_only_args=( ["SKIP_STARTUP_CONNECTION"]="$SKIP_STARTUP_CONNECTION"
["VERBOSE_EVENT_LOGGING"]="$VERBOSE_EVENT_LOGGING"
["EXTERNAL_CALLBACK_SERVICE"]="$EXTERNAL_CALLBACK_SERVICE" )
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing )

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whoops

["VERBOSE_EVENT_LOGGING"]="$VERBOSE_EVENT_LOGGING" )
declare -A h_only_arg_strings=( ["SKIP_STARTUP_CONNECTION"]="--skip-startup-connection"
["VERBOSE_EVENT_LOGGING"]="--verbose-event-logging"
["EXTERNAL_CALLBACK_SERVICE"]="--external-callbacks")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

["VERBOSE_EVENT_LOGGING"]="--verbose-event-logging" )

declare -A h_and_cb_args=( ["IN_DEV"]="$IN_DEV" )
declare -A h_and_cb_arg_strings=( ["IN_DEV"]="--dev" )
Expand All @@ -146,9 +140,7 @@ if [[ $START == 1 ]]; then

unset PYEPICS_LIBCA
hyperion `echo $h_commands;`>$start_log_path 2>&1 &
if [ $EXTERNAL_CALLBACK_SERVICE == true ]; then
hyperion-callbacks `echo $cb_commands;`>$callback_start_log_path 2>&1 &
fi
hyperion-callbacks `echo $cb_commands;`>$callback_start_log_path 2>&1 &
echo "$(date) Waiting for Hyperion to start"

for i in {1..30}
Expand Down
49 changes: 6 additions & 43 deletions src/mx_bluesky/hyperion/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,6 @@
PLAN_REGISTRY,
PlanNotFound,
)
from mx_bluesky.hyperion.external_interaction.callbacks.__main__ import (
setup_logging as setup_callback_logging,
)
from mx_bluesky.hyperion.external_interaction.callbacks.common.callback_util import (
CallbacksFactory,
)
from mx_bluesky.hyperion.parameters.cli import parse_cli_args
from mx_bluesky.hyperion.parameters.constants import CONST
from mx_bluesky.hyperion.utils.context import setup_context
Expand All @@ -57,7 +51,6 @@ class Command:
devices: Any | None = None
experiment: Callable[[Any, Any], MsgGenerator] | None = None
parameters: MxBlueskyParameters | None = None
callbacks: CallbacksFactory | None = None


@dataclass
Expand Down Expand Up @@ -89,7 +82,6 @@ def __init__(
RE: RunEngine,
context: BlueskyContext,
skip_startup_connection=False,
use_external_callbacks: bool = False,
) -> None:
self.command_queue: Queue[Command] = Queue()
self.current_status: StatusAndMessage = StatusAndMessage(Status.IDLE)
Expand All @@ -100,15 +92,12 @@ def __init__(

self.RE = RE
self.context = context
self.subscribed_per_plan_callbacks: list[int] = []
RE.subscribe(self.aperture_change_callback)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can get rid of subscribed_per_plan_callbacks?

RE.subscribe(self.logging_uid_tag_callback)

self.use_external_callbacks = use_external_callbacks
if self.use_external_callbacks:
LOGGER.info("Connecting to external callback ZMQ proxy...")
self.publisher = Publisher(f"localhost:{CONST.CALLBACK_0MQ_PROXY_PORTS[0]}")
RE.subscribe(self.publisher)
LOGGER.info("Connecting to external callback ZMQ proxy...")
self.publisher = Publisher(f"localhost:{CONST.CALLBACK_0MQ_PROXY_PORTS[0]}")
RE.subscribe(self.publisher)

if VERBOSE_EVENT_LOGGING:
RE.subscribe(VerbosePlanExecutionLoggingCallback())
Expand All @@ -124,7 +113,6 @@ def start(
experiment: Callable,
parameters: MxBlueskyParameters,
plan_name: str,
callbacks: CallbacksFactory | None,
) -> StatusAndMessage:
LOGGER.info(f"Started with parameters: {parameters.model_dump_json(indent=2)}")

Expand All @@ -143,7 +131,6 @@ def start(
devices=devices,
experiment=experiment,
parameters=parameters,
callbacks=callbacks,
)
)
return StatusAndMessage(Status.SUCCESS)
Expand Down Expand Up @@ -182,17 +169,6 @@ def wait_on_queue(self):
if command.experiment is None:
raise ValueError("No experiment provided for START")
try:
if (
not self.use_external_callbacks
and command.callbacks
and (cbs := command.callbacks())
):
LOGGER.info(
f"Using callbacks for this plan: {not self.use_external_callbacks} - {cbs}"
)
self.subscribed_per_plan_callbacks += [
self.RE.subscribe(cb) for cb in cbs
]
with TRACER.start_span("do_run"):
self.RE(command.experiment(command.devices, command.parameters))

Expand All @@ -213,11 +189,6 @@ def wait_on_queue(self):
self.last_run_aborted = False
else:
self.current_status = make_error_status_and_message(exception)
finally:
[
self.RE.unsubscribe(cb)
for cb in self.subscribed_per_plan_callbacks
]


def compose_start_args(context: BlueskyContext, plan_name: str, action: Actions):
Expand All @@ -226,7 +197,6 @@ def compose_start_args(context: BlueskyContext, plan_name: str, action: Actions)
raise PlanNotFound(f"Experiment plan '{plan_name}' not found in registry.")

experiment_internal_param_type = experiment_registry_entry.get("param_type")
callback_type = experiment_registry_entry.get("callback_collection_type")
plan = context.plan_functions.get(plan_name)
if experiment_internal_param_type is None:
raise PlanNotFound(
Expand All @@ -244,7 +214,7 @@ def compose_start_args(context: BlueskyContext, plan_name: str, action: Actions)
raise ValueError(
f"Supplied parameters don't match the plan for this endpoint {request.data}, for plan {plan_name}"
) from e
return plan, parameters, plan_name, callback_type
return plan, parameters, plan_name


class RunExperiment(Resource):
Expand All @@ -257,12 +227,10 @@ def put(self, plan_name: str, action: Actions):
status_and_message = StatusAndMessage(Status.FAILED, f"{action} not understood")
if action == Actions.START.value:
try:
plan, params, plan_name, callback_type = compose_start_args(
plan, params, plan_name = compose_start_args(
self.context, plan_name, action
)
status_and_message = self.runner.start(
plan, params, plan_name, callback_type
)
status_and_message = self.runner.start(plan, params, plan_name)
except Exception as e:
status_and_message = make_error_status_and_message(e)
LOGGER.error(format_exception(e))
Expand Down Expand Up @@ -313,15 +281,13 @@ def create_app(
test_config=None,
RE: RunEngine = RunEngine({}),
skip_startup_connection: bool = False,
use_external_callbacks: bool = False,
) -> tuple[Flask, BlueskyRunner]:
context = setup_context(
wait_for_connection=not skip_startup_connection,
)
runner = BlueskyRunner(
RE,
context=context,
use_external_callbacks=use_external_callbacks,
skip_startup_connection=skip_startup_connection,
)
app = Flask(__name__)
Expand Down Expand Up @@ -351,12 +317,9 @@ def create_targets():
do_default_logging_setup(
CONST.LOG_FILE_NAME, CONST.GRAYLOG_PORT, dev_mode=args.dev_mode
)
if not args.use_external_callbacks:
setup_callback_logging(args.dev_mode)
LOGGER.info(f"Hyperion launched with args:{argv}")
app, runner = create_app(
skip_startup_connection=args.skip_startup_connection,
use_external_callbacks=args.use_external_callbacks,
)
return app, runner, hyperion_port, args.dev_mode

Expand Down
15 changes: 0 additions & 15 deletions src/mx_bluesky/hyperion/experiment_plans/experiment_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,6 @@
pin_centre_then_xray_centre_plan,
robot_load_then_centre_plan,
)
from mx_bluesky.hyperion.external_interaction.callbacks.common.callback_util import (
CallbacksFactory,
create_gridscan_callbacks,
create_load_centre_collect_callbacks,
create_robot_load_and_centre_callbacks,
create_rotation_callbacks,
)
from mx_bluesky.hyperion.parameters.gridscan import (
GridScanWithEdgeDetect,
HyperionSpecifiedThreeDGridScan,
Expand Down Expand Up @@ -47,44 +40,36 @@ class ExperimentRegistryEntry(TypedDict):
| LoadCentreCollect
| RobotLoadThenCentre
]
callbacks_factory: CallbacksFactory


PLAN_REGISTRY: dict[str, ExperimentRegistryEntry] = {
"flyscan_xray_centre": {
"setup": flyscan_xray_centre_plan.create_devices,
"param_type": HyperionSpecifiedThreeDGridScan,
"callbacks_factory": create_gridscan_callbacks,
},
"grid_detect_then_xray_centre": {
"setup": grid_detect_then_xray_centre_plan.create_devices,
"param_type": GridScanWithEdgeDetect,
"callbacks_factory": create_gridscan_callbacks,
},
"rotation_scan": {
"setup": rotation_scan_plan.create_devices,
"param_type": RotationScan,
"callbacks_factory": create_rotation_callbacks,
},
"pin_tip_centre_then_xray_centre": {
"setup": pin_centre_then_xray_centre_plan.create_devices,
"param_type": PinTipCentreThenXrayCentre,
"callbacks_factory": create_gridscan_callbacks,
},
"robot_load_then_centre": {
"setup": robot_load_then_centre_plan.create_devices,
"param_type": RobotLoadThenCentre,
"callbacks_factory": create_robot_load_and_centre_callbacks,
},
"multi_rotation_scan": {
"setup": rotation_scan_plan.create_devices,
"param_type": MultiRotationScan,
"callbacks_factory": create_rotation_callbacks,
},
"load_centre_collect_full": {
"setup": load_centre_collect_full_plan.create_devices,
"param_type": LoadCentreCollect,
"callbacks_factory": create_load_centre_collect_callbacks,
},
}

Expand Down
21 changes: 19 additions & 2 deletions src/mx_bluesky/hyperion/external_interaction/callbacks/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from threading import Thread
from time import sleep

from bluesky.callbacks import CallbackBase
from bluesky.callbacks.zmq import Proxy, RemoteDispatcher
from dodal.log import LOGGER as dodal_logger
from dodal.log import set_up_all_logging_handlers
Expand Down Expand Up @@ -48,17 +49,33 @@
ERROR_LOG_BUFFER_LINES = 5000


def setup_callbacks():
return [
def create_gridscan_callbacks() -> tuple[
GridscanNexusFileCallback, GridscanISPyBCallback
]:
return (
GridscanNexusFileCallback(param_type=HyperionSpecifiedThreeDGridScan),
GridscanISPyBCallback(
param_type=GridCommonWithHyperionDetectorParams,
emit=ZocaloCallback(CONST.PLAN.DO_FGS, CONST.ZOCALO_ENV),
),
)


def create_rotation_callbacks() -> tuple[
RotationNexusFileCallback, RotationISPyBCallback
]:
return (
RotationNexusFileCallback(),
RotationISPyBCallback(
emit=ZocaloCallback(CONST.PLAN.ROTATION_MAIN, CONST.ZOCALO_ENV)
),
)


def setup_callbacks() -> list[CallbackBase]:
return [
*create_gridscan_callbacks(),
*create_rotation_callbacks(),
LogUidTaggingCallback(),
RobotLoadISPyBCallback(),
SampleHandlingCallback(),
Expand Down
Empty file.
Loading
Loading