116 lines
3.1 KiB
Python
Executable File
116 lines
3.1 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import os
|
|
import subprocess
|
|
from datetime import datetime
|
|
from pathlib import Path
|
|
|
|
from logzero import logger
|
|
|
|
from helpers import check_pgroup
|
|
|
|
ENDSTATIONS = [
|
|
"alvra",
|
|
"bernina",
|
|
"cristallina",
|
|
"diavolezza",
|
|
"maloja",
|
|
"furka"
|
|
] # yapf: disable
|
|
|
|
DAQ_BIN = "/sf/daq/bin/"
|
|
SPEAR_PTH = "/sf/daq/code/spear/"
|
|
|
|
def start_spear():
|
|
|
|
description = (
|
|
"After each successful job execution apocalypse will send a message. "
|
|
"If the meta file was written and contains key 'table' each key from that dictionary "
|
|
"will be added to the table as a column for a row that corresponds to that run "
|
|
"pgroup, run and acq numbers and timestamp will be added automatically."
|
|
)
|
|
parser = argparse.ArgumentParser(description=description)
|
|
parser._optionals.title = "flag arguments"
|
|
required = parser.add_argument_group("required")
|
|
optional = parser.add_argument_group("optional")
|
|
|
|
required.add_argument(
|
|
"--endstation",
|
|
"-e",
|
|
choices=ENDSTATIONS,
|
|
required=True,
|
|
help="Endstation running the spear"
|
|
)
|
|
|
|
required.add_argument(
|
|
"--pgroup",
|
|
"-p",
|
|
required=True,
|
|
help=("pgroup, given as string in format 'pXXXXX', where X is a digit.")
|
|
)
|
|
|
|
optional.add_argument("--broker-url", default="sf-daq-11", help="RabbitMQ broker URL")
|
|
msg = " for stand app"
|
|
optional.add_argument("--stand-host", help=f"Host{msg}")
|
|
optional.add_argument("--stand-port", help=f"Port{msg}")
|
|
msg = " for stand api"
|
|
optional.add_argument("--stand-api-host", help=f"Host{msg}")
|
|
optional.add_argument("--stand-api-port", help=f"Port{msg}")
|
|
|
|
args = parser.parse_args()
|
|
|
|
args.pgroup = check_pgroup(args.pgroup)
|
|
|
|
spear_fld = Path(f"/sf/{args.endstation}/data/{args.pgroup}/res/spear/")
|
|
|
|
if not spear_fld.parent.exists():
|
|
msg = f"folder {spear_fld.parent} does not exist, exiting"
|
|
logger.error(msg)
|
|
raise SystemExit(msg)
|
|
|
|
spear_fld.mkdir(parents=True, exist_ok=True)
|
|
|
|
os.chdir(spear_fld)
|
|
|
|
cmd = [f"{DAQ_BIN}stand"]
|
|
add_cmd_arg(cmd, "--app-host", args.stand_host)
|
|
add_cmd_arg(cmd, "--app-port", args.stand_port)
|
|
add_cmd_arg(cmd, "--api-host", args.stand_api_host)
|
|
add_cmd_arg(cmd, "--api-port", args.stand_api_port)
|
|
|
|
p_stand=subprocess.Popen(cmd)
|
|
|
|
cmd = [
|
|
f"{SPEAR_PTH}wait_4_msg.py",
|
|
"--endstation",
|
|
args.endstation,
|
|
"--pgroup",
|
|
args.pgroup
|
|
]
|
|
add_cmd_arg(cmd, "--broker-url", args.broker_url)
|
|
add_cmd_arg(cmd, "--stand-host", args.stand_host)
|
|
add_cmd_arg(cmd, "--stand-port", args.stand_port)
|
|
|
|
try:
|
|
subprocess.run(cmd)
|
|
except KeyboardInterrupt as exc:
|
|
logger.debug("keyboard interrupt, exiting")
|
|
p_stand.terminate()
|
|
raise SystemExit from exc
|
|
except Exception:
|
|
p_stand.terminate()
|
|
raise
|
|
|
|
def add_cmd_arg(cmd, name, val):
|
|
|
|
if val:
|
|
if name is not None:
|
|
return cmd.extend([name,val])
|
|
else:
|
|
return cmd.append(val)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
start_spear()
|