Ejemplo n.º 1
0
    def __init__(self, **kwargs):

        for p in self.init_needed_keywords:
            if p not in kwargs.keys():
                raise PIDanticUsageException("The driver %s must have the parameter %s." % (self.driver_name, p))
        for p in kwargs.keys():
            if p not in self.init_needed_keywords and p not in self.init_optional_keywords:
                raise PIDanticUsageException("The driver %s does not know the parameter %s." % (self.driver_name, p))
        self._name = kwargs.get('name')
        self.container = kwargs.get('pyon_container')
        self._working_dir = kwargs.get('directory')
        self._log = kwargs.get('log')

        db_url = "sqlite:///%s/pyon.db" % (self._working_dir)
        self._db = PyonDB(db_url)

        pyon_instances = self._db.get_all_pyons()
        if len(pyon_instances) > 1:
            raise PIDanticUsageException("The driver %s should have at most 1 pyon instance in its table." % (self.driver_name))

        if not pyon_instances:
            self._pyon = Pyon(self._db, pyon_container=self.container, name=self._name, log=self._log, dirpath=self._working_dir)
        else:
            pyon_instance = pyon_instances.pop(0)
            if pyon_instance.name != self._name:
                raise PIDanticUsageException("The requested pyon name %s is not in the db" % (self._name))
            self._pyon = Pyon(self._db, data_object=pyon_instance,
                    pyon_container=self.container)

        self.container.proc_manager.add_proc_state_changed_callback(self._pyon_process_state_change_callback)
Ejemplo n.º 2
0
    def __init__(self, **kwargs):

        for p in self.init_needed_keywords:
            if p not in kwargs.keys():
                raise PIDanticUsageException(
                    "The driver %s must have the parameter %s." %
                    (self.driver_name, p))
        for p in kwargs.keys():
            if p not in self.init_needed_keywords and p not in self.init_optional_keywords:
                raise PIDanticUsageException(
                    "The driver %s does not know the parameter %s." %
                    (self.driver_name, p))
        self._name = kwargs.get('name')
        self.container = kwargs.get('pyon_container')
        self._working_dir = kwargs.get('directory')
        self._log = kwargs.get('log')

        db_url = "sqlite:///%s/pyon.db" % (self._working_dir)
        self._db = PyonDB(db_url)

        pyon_instances = self._db.get_all_pyons()
        if len(pyon_instances) > 1:
            raise PIDanticUsageException(
                "The driver %s should have at most 1 pyon instance in its table."
                % (self.driver_name))

        if not pyon_instances:
            self._pyon = Pyon(self._db,
                              pyon_container=self.container,
                              name=self._name,
                              log=self._log,
                              dirpath=self._working_dir)
        else:
            pyon_instance = pyon_instances.pop(0)
            if pyon_instance.name != self._name:
                raise PIDanticUsageException(
                    "The requested pyon name %s is not in the db" %
                    (self._name))
            self._pyon = Pyon(self._db,
                              data_object=pyon_instance,
                              pyon_container=self.container)

        self.container.proc_manager.add_proc_state_changed_callback(
            self._pyon_process_state_change_callback)
Ejemplo n.º 3
0
class PyonPidanticFactory(PidanticFactory):

    driver_name = "pyon"

    init_needed_keywords = [
        "pyon_container",
        "directory",
        "name",
    ]
    init_optional_keywords = [
        "log",
    ]

    run_needed_keywords = [
        "directory",
        "process_name",
        "pyon_name",
        "module",
        "cls",
        "config",
    ]

    run_optional_keywords = [
        "log",
        "module_uri",
    ]

    _watched_processes = {}

    def __init__(self, **kwargs):

        for p in self.init_needed_keywords:
            if p not in kwargs.keys():
                raise PIDanticUsageException("The driver %s must have the parameter %s." % (self.driver_name, p))
        for p in kwargs.keys():
            if p not in self.init_needed_keywords and p not in self.init_optional_keywords:
                raise PIDanticUsageException("The driver %s does not know the parameter %s." % (self.driver_name, p))
        self._name = kwargs.get('name')
        self.container = kwargs.get('pyon_container')
        self._working_dir = kwargs.get('directory')
        self._log = kwargs.get('log')

        db_url = "sqlite:///%s/pyon.db" % (self._working_dir)
        self._db = PyonDB(db_url)

        pyon_instances = self._db.get_all_pyons()
        if len(pyon_instances) > 1:
            raise PIDanticUsageException("The driver %s should have at most 1 pyon instance in its table." % (self.driver_name))

        if not pyon_instances:
            self._pyon = Pyon(self._db, pyon_container=self.container, name=self._name, log=self._log, dirpath=self._working_dir)
        else:
            pyon_instance = pyon_instances.pop(0)
            if pyon_instance.name != self._name:
                raise PIDanticUsageException("The requested pyon name %s is not in the db" % (self._name))
            self._pyon = Pyon(self._db, data_object=pyon_instance,
                    pyon_container=self.container)

        self.container.proc_manager.add_proc_state_changed_callback(self._pyon_process_state_change_callback)

    def get_pidantic(self, **kwargs):

        for p in self.run_needed_keywords:
            if p not in kwargs.keys():
                raise PIDanticUsageException("The driver %s must have the parameter %s." % (self.driver_name, p))

        for p in kwargs.keys():
            if p not in self.run_needed_keywords and p not in self.run_optional_keywords:
                raise PIDanticUsageException("The driver %s does not know the parameter %s." % (self.driver_name, p))

        kwargs['pyon_process_id'], _round = kwargs['process_name'].split('-')
        program_object = self._pyon.create_process_db(**kwargs)

        pidpyon = PIDanticPyon(program_object, self._pyon, log=self._log, state_change_callback=self._pyon_process_state_change_callback)
        self._watched_processes[program_object.pyon_process_id] = pidpyon

        return pidpyon

    def reload_instances(self):
        self._watched_processes = {}
        data_obj = self._pyon.get_data_object()
        for p in data_obj.processes:
            pidpyon = PIDanticPyon(p, self._pyon, log=self._log, state_change_callback=self._pyon_process_state_change_callback)
            self._watched_processes[p.pyon_process_id] = pidpyon
        self._mark_all_failed()

        return self._watched_processes

    def _mark_all_failed(self):
        for pyon_process_id, pidpyon in self._watched_processes.iteritems():
            self._log.error("Marking %s failed after a restart" % pyon_process_id)
            pidpyon._exit_code = 100
            pidpyon._process_state_change(ProcessStateEnum.FAILED)

    def _pyon_process_state_change_callback(self, process, state, container):

        if not hasattr(process, 'id'):
            # Process is in Pending state, which we ignore, because we don't
            # Have a process id for it
            return

        pidpyon = self._watched_processes.get(process.id)
        if pidpyon is None:
            self._log.warning("Got callback for unknown process %s" % process.id)
            return

        self._log.info("Got callback for process %s with state %s" % (process.id, ProcessStateEnum._str_map[state]))
        pidpyon._process_state_change(state)

    def poll(self):
        """For Pyon, this function is handled by callbacks from the container
        """
        pass

    def terminate(self):
        self.container.proc_manager.remove_proc_state_changed_callback(self._pyon_process_state_change_callback)
        self._pyon.terminate()
Ejemplo n.º 4
0
class PyonPidanticFactory(PidanticFactory):

    driver_name = "pyon"

    init_needed_keywords = [
        "pyon_container",
        "directory",
        "name",
    ]
    init_optional_keywords = [
        "log",
    ]

    run_needed_keywords = [
        "directory",
        "process_name",
        "pyon_name",
        "module",
        "cls",
        "config",
    ]

    run_optional_keywords = [
        "log",
        "module_uri",
    ]

    _watched_processes = {}

    def __init__(self, **kwargs):

        for p in self.init_needed_keywords:
            if p not in kwargs.keys():
                raise PIDanticUsageException(
                    "The driver %s must have the parameter %s." %
                    (self.driver_name, p))
        for p in kwargs.keys():
            if p not in self.init_needed_keywords and p not in self.init_optional_keywords:
                raise PIDanticUsageException(
                    "The driver %s does not know the parameter %s." %
                    (self.driver_name, p))
        self._name = kwargs.get('name')
        self.container = kwargs.get('pyon_container')
        self._working_dir = kwargs.get('directory')
        self._log = kwargs.get('log')

        db_url = "sqlite:///%s/pyon.db" % (self._working_dir)
        self._db = PyonDB(db_url)

        pyon_instances = self._db.get_all_pyons()
        if len(pyon_instances) > 1:
            raise PIDanticUsageException(
                "The driver %s should have at most 1 pyon instance in its table."
                % (self.driver_name))

        if not pyon_instances:
            self._pyon = Pyon(self._db,
                              pyon_container=self.container,
                              name=self._name,
                              log=self._log,
                              dirpath=self._working_dir)
        else:
            pyon_instance = pyon_instances.pop(0)
            if pyon_instance.name != self._name:
                raise PIDanticUsageException(
                    "The requested pyon name %s is not in the db" %
                    (self._name))
            self._pyon = Pyon(self._db,
                              data_object=pyon_instance,
                              pyon_container=self.container)

        self.container.proc_manager.add_proc_state_changed_callback(
            self._pyon_process_state_change_callback)

    def get_pidantic(self, **kwargs):

        for p in self.run_needed_keywords:
            if p not in kwargs.keys():
                raise PIDanticUsageException(
                    "The driver %s must have the parameter %s." %
                    (self.driver_name, p))

        for p in kwargs.keys():
            if p not in self.run_needed_keywords and p not in self.run_optional_keywords:
                raise PIDanticUsageException(
                    "The driver %s does not know the parameter %s." %
                    (self.driver_name, p))

        kwargs['pyon_process_id'], _round = kwargs['process_name'].split('-')
        program_object = self._pyon.create_process_db(**kwargs)

        pidpyon = PIDanticPyon(
            program_object,
            self._pyon,
            log=self._log,
            state_change_callback=self._pyon_process_state_change_callback)
        self._watched_processes[program_object.pyon_process_id] = pidpyon

        return pidpyon

    def reload_instances(self):
        self._watched_processes = {}
        data_obj = self._pyon.get_data_object()
        for p in data_obj.processes:
            pidpyon = PIDanticPyon(
                p,
                self._pyon,
                log=self._log,
                state_change_callback=self._pyon_process_state_change_callback)
            self._watched_processes[p.pyon_process_id] = pidpyon
        self._mark_all_failed()

        return self._watched_processes

    def _mark_all_failed(self):
        for pyon_process_id, pidpyon in self._watched_processes.iteritems():
            self._log.error("Marking %s failed after a restart" %
                            pyon_process_id)
            pidpyon._exit_code = 100
            pidpyon._process_state_change(ProcessStateEnum.FAILED)

    def _pyon_process_state_change_callback(self, process, state, container):

        if not hasattr(process, 'id'):
            # Process is in Pending state, which we ignore, because we don't
            # Have a process id for it
            return

        pidpyon = self._watched_processes.get(process.id)
        if pidpyon is None:
            self._log.warning("Got callback for unknown process %s" %
                              process.id)
            return

        self._log.info("Got callback for process %s with state %s" %
                       (process.id, ProcessStateEnum._str_map[state]))
        pidpyon._process_state_change(state)

    def poll(self):
        """For Pyon, this function is handled by callbacks from the container
        """
        pass

    def terminate(self):
        self.container.proc_manager.remove_proc_state_changed_callback(
            self._pyon_process_state_change_callback)
        self._pyon.terminate()