From 34a210810f61624d0b492dda7d267fc4884e4608 Mon Sep 17 00:00:00 2001 From: sfisher Date: Fri, 1 Oct 2021 15:19:14 +0200 Subject: [PATCH 1/2] add ispyb simulator plugin --- setup.cfg | 4 +++ src/zocalo/ispyb/simulator.py | 65 +++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) create mode 100644 src/zocalo/ispyb/simulator.py diff --git a/setup.cfg b/setup.cfg index 7bf12c72..322764fc 100644 --- a/setup.cfg +++ b/setup.cfg @@ -57,6 +57,10 @@ zocalo.configuration.plugins = jmx = zocalo.configuration.plugin_jmx:JMX zocalo.wrappers = dummy = zocalo.wrapper:DummyWrapper +ispyb.simulator.before_datacollection = + zocalo = zocalo.ispyb.simulator:before +ispyb.simulator.after_datacollection = + zocalo = zocalo.ispyb.simulator:after [options.packages.find] where = src diff --git a/src/zocalo/ispyb/simulator.py b/src/zocalo/ispyb/simulator.py new file mode 100644 index 00000000..5e21e344 --- /dev/null +++ b/src/zocalo/ispyb/simulator.py @@ -0,0 +1,65 @@ +import logging + +import workflows.transport +import zocalo.configuration +from zocalo.configuration import Configuration + + +logger = logging.getLogger(__name__) + + +def send_message(zc: Configuration, message: dict, headers={}): + if ( + zc.storage + and zc.storage.get("zocalo.default_transport") + in workflows.transport.get_known_transports() + ): + transport_type = zc.storage["zocalo.default_transport"] + else: + transport_type = workflows.transport.default_transport + + transport = workflows.transport.lookup(transport_type)() + try: + transport.connect() + transport.send("processing_recipe", message, headers=headers) + transport.disconnect() + except Exception: + logger.warning("Cant connect to workflow transport") + + +def before(dcid: int): + zc = zocalo.configuration.from_file() + zc.activate() + + default_recipe = "mimas" + if zc.storage: + recipe = zc.storage.get("ispyb.simulator", {}).get("recipe_before", "mimas") + else: + recipe = default_recipe + + send_message( + zc, + message={ + "recipes": [recipe], + "parameters": {"ispyb_dcid": dcid, "event": "start"}, + }, + ) + + +def after(dcid: int): + zc = zocalo.configuration.from_file() + zc.activate() + + default_recipe = "mimas" + if zc.storage: + recipe = zc.storage.get("ispyb.simulator", {}).get("recipe_after", "mimas") + else: + recipe = default_recipe + + send_message( + zc, + { + "recipes": [recipe], + "parameters": {"ispyb_dcid": dcid, "event": "end"}, + }, + ) From 9665804e713f489416a7f8ca41b0d2766f1b76a0 Mon Sep 17 00:00:00 2001 From: Stuart Fisher Date: Thu, 7 Oct 2021 14:53:45 +0200 Subject: [PATCH 2/2] use transport config plugin, tidy calls, add history --- HISTORY.rst | 6 +++++ src/zocalo/ispyb/simulator.py | 47 +++++++++++------------------------ 2 files changed, 20 insertions(+), 33 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index f2ca35a5..2b892d95 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -2,6 +2,12 @@ History ======= +Unlreleased / main +------------------ +* Add a zocalo plugin for ``ispyb.simulate`` + (`#163 `_) to + call a recipe before and after creation of data + 0.9.0 (2021-08-18) ------------------ * Removed --live/--test command line arguments, use -e/--environment instead diff --git a/src/zocalo/ispyb/simulator.py b/src/zocalo/ispyb/simulator.py index 5e21e344..a9b08937 100644 --- a/src/zocalo/ispyb/simulator.py +++ b/src/zocalo/ispyb/simulator.py @@ -2,23 +2,13 @@ import workflows.transport import zocalo.configuration -from zocalo.configuration import Configuration logger = logging.getLogger(__name__) -def send_message(zc: Configuration, message: dict, headers={}): - if ( - zc.storage - and zc.storage.get("zocalo.default_transport") - in workflows.transport.get_known_transports() - ): - transport_type = zc.storage["zocalo.default_transport"] - else: - transport_type = workflows.transport.default_transport - - transport = workflows.transport.lookup(transport_type)() +def _send_message(message: dict, headers={}): + transport = workflows.transport.lookup(workflows.transport.default_transport)() try: transport.connect() transport.send("processing_recipe", message, headers=headers) @@ -27,39 +17,30 @@ def send_message(zc: Configuration, message: dict, headers={}): logger.warning("Cant connect to workflow transport") -def before(dcid: int): +def _get_recipe(event: str): zc = zocalo.configuration.from_file() zc.activate() - default_recipe = "mimas" + recipe = "mimas" if zc.storage: - recipe = zc.storage.get("ispyb.simulator", {}).get("recipe_before", "mimas") - else: - recipe = default_recipe + recipe = zc.storage.get("ispyb.simulator", {}).get(event, "mimas") + + return recipe - send_message( - zc, - message={ - "recipes": [recipe], + +def before(dcid: int): + _send_message( + { + "recipes": [_get_recipe("recipe_before")], "parameters": {"ispyb_dcid": dcid, "event": "start"}, }, ) def after(dcid: int): - zc = zocalo.configuration.from_file() - zc.activate() - - default_recipe = "mimas" - if zc.storage: - recipe = zc.storage.get("ispyb.simulator", {}).get("recipe_after", "mimas") - else: - recipe = default_recipe - - send_message( - zc, + _send_message( { - "recipes": [recipe], + "recipes": [_get_recipe("recipe_after")], "parameters": {"ispyb_dcid": dcid, "event": "end"}, }, )