Ejemplo n.º 1
0
    def aquire(self, key, timeout=3600, owner="None"):
        try:
            with session.begin(subtransactions=True):
                dblock = DbLock(key=key, timeout=timeout, owner=owner)
                session.add(dblock)

            return Lock(dblock)

        except SQLAlchemyError:

            old_lock = DbLock.query.filter_by(key=key).first()

            timeout_secs = time.mktime(
                old_lock.creation.timetuple()) + old_lock.timeout
            now_secs = time.mktime(timezone.now().timetuple())

            if now_secs > timeout_secs:
                raise InternalError(
                    "A lock went over the limit timeout, this could mine the integrity of the system. Reload the Daemon to fix the problem."
                )
            else:
                raise LockPresent("A lock is present.")

        except:
            raise InternalError("Something went wrong, try to keep on.")
Ejemplo n.º 2
0
    def test_settings(self):
        """
        Test the settings table (similar to Attributes, but without the key.
        """
        from aiida.backends.sqlalchemy.models.settings import DbSetting
        from aiida.backends.sqlalchemy import session
        from pytz import UTC
        from aiida.utils import timezone
        from sqlalchemy.exc import IntegrityError

        DbSetting.set_value(key='pippo', value=[1, 2, 3])

        # s1 = DbSetting.objects.get(key='pippo')
        s1 = DbSetting.query.filter_by(key='pippo').first()

        self.assertEqual(s1.getvalue(), [1, 2, 3])

        s2 = DbSetting(key='pippo')
        s2.time = timezone.datetime.now(tz=UTC)
        with self.assertRaises(IntegrityError):
            with session.begin_nested():
                # same name...
                session.add(s2)

        # Should replace pippo
        DbSetting.set_value(key='pippo', value="a")
        s1 = DbSetting.query.filter_by(key='pippo').first()

        self.assertEqual(s1.getvalue(), "a")
Ejemplo n.º 3
0
    def add_comment(self, content, user=None):
        from aiida.backends.sqlalchemy import session
        if self._to_be_stored:
            raise ModificationNotAllowed("Comments can be added only after "
                                         "storing the node")

        comment = DbComment(dbnode=self._dbnode, user=user, content=content)
        session.add(comment)
        session.commit()
Ejemplo n.º 4
0
    def copy(self):
        if self.to_be_stored:
            raise InvalidOperation(
                "You can copy a computer only after having stored it")

        newdbcomputer = copy(self.dbcomputer)
        make_transient(newdbcomputer)
        session.add(newdbcomputer)

        newobject = self.__class__(newdbcomputer)

        return newobject
Ejemplo n.º 5
0
 def _do_create_link(self, src, label, link_type):
     from aiida.backends.sqlalchemy import session
     try:
         with session.begin_nested():
             link = DbLink(input_id=src.dbnode.id,
                           output_id=self.dbnode.id,
                           label=label,
                           type=link_type.value)
             session.add(link)
     except SQLAlchemyError as e:
         raise UniquenessError("There is already a link with the same "
                               "name (raw message was {})"
                               "".format(e))
Ejemplo n.º 6
0
    def copy(self):
        from aiida.backends.sqlalchemy import session

        if self.to_be_stored:
            raise InvalidOperation(
                "You can copy a computer only after having stored it")

        newdbcomputer = copy(self.dbcomputer)
        make_transient(newdbcomputer)
        session.add(newdbcomputer)

        newobject = self.__class__(newdbcomputer)

        return newobject