Esempio n. 1
0
File: flow.py Progetto: JHP4911/JINA
    def add(self,
            config: BinaryIO,
            workspace_id: Optional[uuid.UUID] = None,
            **kwargs):
        try:
            if not workspace_id:
                workspace_id = random_uuid()

            flow_identity = random_uuid()
            with jina_workspace(workspace_id) as _workdir:
                y_spec = config.read().decode()
                f = Flow.load_config(y_spec)
                f.identity = str(flow_identity)
                f.workspace_id = str(workspace_id)
                f.start()

        except Exception as e:
            self._logger.error(f'{e!r}')
            raise
        else:
            self[flow_identity] = {
                'object': f,
                'arguments': vars(f.args),
                'yaml_source': y_spec,
                'workdir': _workdir,
                'workspace_id': workspace_id
            }
            self._logger.success(
                f'{colored(str(flow_identity), "cyan")} is added to workspace {colored(str(workspace_id), "cyan")}'
            )
            return flow_identity
Esempio n. 2
0
    def add(self, files: List[UploadFile], workspace_id, **kwargs):
        try:
            if not workspace_id:
                workspace_id = random_uuid()
            _workdir = get_workspace_path(workspace_id)
            Path(_workdir).mkdir(parents=True, exist_ok=True)

            for f in files:
                dest = os.path.join(_workdir, f.filename)
                if os.path.isfile(dest):
                    self._logger.warning(
                        f'file {f.filename} already exists in workspace {workspace_id}, will be replaced'
                    )
                with open(dest, 'wb+') as fp:
                    content = f.file.read()
                    fp.write(content)
                self._logger.info(f'saved uploads to {dest}')
        except Exception as e:
            self._logger.error(f'{e!r}')
            raise
        else:
            self[workspace_id] = {
                'arguments': [f.filename for f in files],
                'workdir': _workdir,
                'workspace_id': workspace_id,
            }
            return workspace_id
Esempio n. 3
0
    def add(self,
            config: BinaryIO,
            workspace_id: Optional[uuid.UUID] = None,
            **kwargs):
        try:
            if not workspace_id:
                workspace_id = random_uuid()

            with jina_workspace(workspace_id) as _workdir:
                y_spec = config.read().decode()
                f = Flow.load_config(y_spec)
                f.start()

            _id = uuid.UUID(f.args.identity)
        except Exception as e:
            self._logger.error(f'{e!r}')
            raise
        else:
            self[_id] = {
                'object': f,
                'arguments': vars(f.args),
                'yaml_source': y_spec,
                'workdir': _workdir,
                'workspace_id': workspace_id
            }
            return _id
Esempio n. 4
0
def test_jina_workspace():
    uid = random_uuid()
    assert 'JINA_LOG_WORKSPACE' not in os.environ
    assert not os.path.exists(get_workspace_path(uid))
    with jina_workspace(uid):
        assert os.path.exists(get_workspace_path(uid))
        assert get_workspace_path(uid) in os.getcwd()
        assert 'JINA_LOG_WORKSPACE' in os.environ
        assert os.environ['JINA_LOG_WORKSPACE'] in os.getcwd()

    assert 'JINA_LOG_WORKSPACE' not in os.environ
Esempio n. 5
0
def test_base_store_del():
    s = BaseStore()
    old_update = s._time_updated
    assert s._time_updated
    s._items.update(store_items)
    assert len(s) == 3
    del s[keys[0]]
    assert len(s) == 2
    s.pop(keys[1])
    assert len(s) == 1
    assert old_update < s._time_updated

    old_update = s._time_updated
    with pytest.raises(KeyError):
        del s[random_uuid()]
    assert old_update == s._time_updated
Esempio n. 6
0
 def add(self, files: List[UploadFile], **kwargs):
     try:
         workspace_id = random_uuid()
         _workdir = get_workspace_path(workspace_id)
         Path(_workdir).mkdir(parents=True, exist_ok=False)
         for f in files:
             dest = os.path.join(_workdir, f.filename)
             with open(dest, 'wb') as fp:
                 content = f.file.read()
                 fp.write(content)
             self._logger.info(f'save uploads to {dest}')
     except Exception as e:
         self._logger.error(f'{e!r}')
         raise
     else:
         self[workspace_id] = {
             'arguments': [f.filename for f in files],
             'workdir': _workdir,
             'workspace_id': workspace_id
         }
         return workspace_id
Esempio n. 7
0
    def add(self, args: Namespace,
            **kwargs):
        try:
            workspace_id = args.workspace_id
            if not workspace_id:
                workspace_id = random_uuid()

            with jina_workspace(workspace_id) as _workdir:
                p = self.peapod_cls(args).start()

            _id = uuid.UUID(args.identity)
        except Exception as e:
            self._logger.error(f'{e!r}')
            raise
        else:
            self[_id] = {
                'object': p,
                'arguments': vars(args),
                'workdir': _workdir,
                'workspace_id': workspace_id
            }
            return _id
Esempio n. 8
0
def test_workspace_path():
    uid = random_uuid()
    assert get_workspace_path(uid) == f'{jinad_args.workspace}/{uid}'
    assert get_workspace_path('123',
                              '456') == f'{jinad_args.workspace}/123/456'