From 9938c17e9b19de08ec30353339d7b133b490c6e2 Mon Sep 17 00:00:00 2001
From: Robert Rosca <robert.rosca@xfel.eu>
Date: Tue, 15 Feb 2022 18:11:43 +0100
Subject: [PATCH] Add orca passthrough

---
 webservice/webservice.py | 51 ++++++++++++++++++++++++++++++++++++++++
 1 file changed, 51 insertions(+)

diff --git a/webservice/webservice.py b/webservice/webservice.py
index c8562f277..b67f5dc48 100644
--- a/webservice/webservice.py
+++ b/webservice/webservice.py
@@ -21,6 +21,7 @@ from subprocess import PIPE, run
 from threading import Thread
 from typing import Any, Dict, List, Optional, Tuple
 
+import requests
 import yaml
 import zmq
 import zmq.asyncio
@@ -698,6 +699,42 @@ async def update_mdc_status(mdc: MetadataClient, action: str,
         logging.error(Errors.MDC_RESPONSE.format(response))
 
 
+def _orca_passthrough(
+    proposal_number = None,
+    runs = None,
+    action = None,
+    route = "execute",
+    **kwargs,
+):
+    """ Passes through requests received by the webservice to Orca for use during
+    the transition to the new webservice
+
+    Due to network restrictions on Maxwell, this sends post requests to localhost on
+    port 42751 (a random port number), so either Orca should be running locally or a
+    ssh tunnel should be set up with `ssh -L 42751:localhost:42751 TARGET-SERVER -N &`
+    """
+    try:
+        base_url = "http://localhost"
+        port = "42751"
+
+        args = []
+
+        args.append(f"action={action}") if action else None
+        args.append(f"runs={','.join(str(r) for r in runs)}") if runs else None
+
+        args.extend([f"{k}={v}" for k, v in kwargs.items()])
+
+        url = f"{base_url}:{port}/{route}/{proposal_number}?{'&'.join(filter(None, args))}"
+    except Exception as e:
+        logging.warning("error building orca passthrough request", exc_info=True)
+        return None
+
+    try:
+        requests.post(url)
+    except Exception as e:
+        logging.error(f"orca post request error for url {url}", exc_info=True)
+
+
 class ActionsServer:
     def __init__(self, config, mode):
         self.config = config
@@ -821,6 +858,12 @@ class ActionsServer:
             proposal = self._normalise_proposal_num(proposal)
             pconf_full = self.load_proposal_config(cycle, proposal)
 
+            _orca_passthrough(
+                proposal_number=proposal,
+                runs=[runnr],
+                route="execute",
+            )
+
             data_conf = pconf_full['data-mapping']
             if instrument in pconf_full['correct']:
                 pconf = pconf_full['correct'][instrument]
@@ -942,6 +985,14 @@ class ActionsServer:
 
             proposal = self._normalise_proposal_num(proposal)
 
+            _orca_passthrough(
+                proposal_number=proposal,
+                runs=runs,
+                action="dark",
+                route="execute",
+                karabo_id=karabo_id,
+            )
+
             pconf_full = self.load_proposal_config(cycle, proposal)
 
             data_conf = pconf_full['data-mapping']
-- 
GitLab