start frappy server automatically, if not running yet

when restarting NICOS, the frapy servers should be (re-)started
if not running already with the correct config
This commit is contained in:
2022-09-21 17:22:16 +02:00
parent 78edd77ce1
commit 16ba2bcf19
2 changed files with 27 additions and 20 deletions

View File

@ -31,7 +31,7 @@ import os
from os.path import expanduser
from nicos import config, session
from nicos.core import Override, Param, Moveable, status, DeviceAlias
from nicos.core import Override, Param, Moveable, status, DeviceAlias, SIMULATION, POLLER
from nicos.devices.secop import SecNodeDevice
from nicos.core import Device, anytype, listof
from nicos.utils.comparestrings import compare
@ -215,11 +215,17 @@ class FrappyNode(SecNodeDevice, Moveable):
def doStart(self, value):
if value == 'None':
value = None
self.restart(value)
self.restart(value, True) # frappy server will be restarted even when unchanged
def doStop(self):
"""never busy"""
def doInit(self, mode):
if mode != SIMULATION and session.sessiontype != POLLER:
cfg = self.doRead()
self.restart(cfg, False) # do not restart when not changed
super().doInit(mode)
def doRead(self, maxage=0):
if self._cfgvalue is None and self._cache:
self._cfgvalue = self._cache.get(self, 'value')
@ -254,7 +260,7 @@ class FrappyNode(SecNodeDevice, Moveable):
return available_cfg
def disable(self):
seaconn = session.devices.get('seaconn')
seaconn = session.devices.get('sea_%s' % self.service)
if seaconn and seaconn._attached_secnode:
seaconn.communicate('frappy_remove %s' % self.service)
self._set_status(*self._status)
@ -264,23 +270,27 @@ class FrappyNode(SecNodeDevice, Moveable):
code, text = status.DISABLED, 'disabled'
SecNodeDevice._set_status(self, code, text)
def restart(self, cfg=None):
def restart(self, cfg=None, restart=True):
"""restart frappy server
if value is not given: restart with the same config
else restart with given config
:param cfg: config for frappy server, if not given, restart with the same config
:param restart: when false, do not restart when already running with same cfg
"""
was_cfg = self._cfgvalue and ':' not in self._cfgvalue
if cfg is None: # do restart anyway
if cfg is None:
cfg = self._cfgvalue
elif cfg == self._cfgvalue: # probably called from doStart
if cfg == '':
self.disable()
# do not restart in this case
return
is_cfg = cfg and ':' not in cfg
ins = config.instrument
info = self._service_manager.get_ins_info(ins)
cfginfo = {}
self._service_manager.get_procs(cfginfo=cfginfo)
running_cfg = cfginfo.get((ins, self.service), '')
if cfg == running_cfg:
if not restart:
return
else:
self.disable()
if running_cfg:
self._service_manager.do_stop(ins, self.service)
is_cfg = cfg and ':' not in cfg
if is_cfg:
available_cfg = self.available_cfg(self.service)
failed = False
@ -297,14 +307,10 @@ class FrappyNode(SecNodeDevice, Moveable):
uri = cfg
if uri != self.uri:
self.uri = '' # disconnect
if was_cfg:
self._service_manager.do_stop(ins, self.service)
if uri:
if is_cfg:
self._service_manager.do_restart(ins, self.service, cfg, self.log)
self._service_manager.do_start(ins, self.service, cfg, logger=self.log)
self.uri = uri # connect
else:
self.disable()
self._cfgvalue = cfg
if self._cache:
self._cache.put(self, 'value', cfg)