Example #1
0
def start_backend(config):
    """
    Start the backend by checking the currently running backend. Basically 2 cases/scenarios:
        1. If no running backend => run the requested one.
        2. If there is running backend =>
            a. If the running backend is same as requested one => do nothing
            b. If the running backend is different from the requested one => stop the running, and run the requested one.
    In case the backend fails to start a IOError will be raised.
    config (str): path to the microscope config file.
    """
    # check if a backend is running
    if driver.get_backend_status() in (driver.BACKEND_RUNNING,
                                       driver.BACKEND_STARTING):
        current_model = model.getMicroscope().model
        try:
            req_model = modelgen.Instantiator(open(config)).ast
        except Exception as exp:
            raise ValueError(exp)
        if current_model == req_model:
            logging.info("Backend for %s already running", config)
            return
        else:
            logging.info(
                "There is a backend running already, it will be turned off, and the backend \
                                %s will be run instead.", config)
            stop_backend()
            run_backend(config)

    # check if no backend is running
    else:
        run_backend(config)
Example #2
0
    def __init__(self,
                 model_file,
                 settings_file,
                 create_sub_containers=False,
                 dry_run=False,
                 name=model.BACKEND_NAME):
        """
        inst_file (file): opened file that contains the yaml
        settings_file (file): opened file that contains the persistent data
        container (Container): container in which to instantiate the components
        create_sub_containers (bool): whether the leave components (components which
           have no children created separately) are running in isolated containers
        dry_run (bool): if True, it will check the semantic and try to instantiate the
          model without actually any driver contacting the hardware.
        """
        model.Container.__init__(self, name)

        self._model = model_file
        self._settings = settings_file
        self._mdupdater = None
        self._inst_thread = None  # thread running the component instantiation
        self._must_stop = threading.Event()
        self._dry_run = dry_run
        # TODO: have an argument to ask for disabling parallel start? same as create_sub_containers?

        # parse the instantiation file
        logging.debug("model instantiation file is: %s", self._model.name)
        try:
            self._instantiator = modelgen.Instantiator(model_file,
                                                       settings_file, self,
                                                       create_sub_containers,
                                                       dry_run)
            # save the model
            logging.info("model has been successfully parsed")
        except modelgen.ParseError as exp:
            raise ValueError("Error while parsing file %s:\n%s" %
                             (self._model.name, exp))
        except modelgen.SemanticError as exp:
            raise ValueError("When instantiating file %s:\n%s" %
                             (self._model.name, exp))
        except Exception:
            logging.exception("When instantiating file %s", self._model.name)
            raise IOError("Unexpected error at instantiation")

        # Initialize persistent data, will be updated and written to the settings_file in the
        # end and every time a va is changed.
        self._persistent_listeners = [
        ]  # keep reference to persistent va listeners
        self._persistent_data = self._instantiator.read_yaml(settings_file)