コード例 #1
0
def test_class_yaml():
    class DummyClass:
        pass

    yaml.register_class(DummyClass)

    a = yaml.load('!DummyClass {}')
    assert type(a) == DummyClass

    with open(
            resource_filename(
                'jina', '/'.join(
                    ('resources',
                     'executors.requests.BaseExecutor.yml')))) as fp:
        b = fp.read()
        print(b)
        c = yaml.load(b)
        print(c)

    args = set_pea_parser().parse_args([])

    with BasePea(args):
        pass

    from jina.executors.requests import _defaults
    assert _defaults is not None
コード例 #2
0
    def test_class_yaml(self):
        class DummyClass:
            pass

        yaml.register_class(DummyClass)

        a = yaml.load('!DummyClass {}')
        self.assertEqual(type(a), DummyClass)

        with open(
                resource_filename(
                    'jina', '/'.join(
                        ('resources', 'executors.requests.%s.yml' %
                         'BaseExecutor')))) as fp:
            b = fp.read()
            print(b)
            c = yaml.load(b)
            print(c)

        args = set_pea_parser().parse_args([])

        with BasePea(args) as p:
            pass

        from jina.executors.requests import _defaults
        self.assertIsNotNone(_defaults)
コード例 #3
0
    def _create(self,
                config: Union[str, SpooledTemporaryFile,
                              List[PodModel]] = None,
                files: List[UploadFile] = None):
        """ Creates Flow using List[PodModel] or yaml spec """
        # This makes sure `uses` & `py_modules` are created locally in `cwd`
        # TODO: Handle file creation, deletion better
        if files:
            [
                create_meta_files_from_upload(current_file)
                for current_file in files
            ]

        # FastAPI treats UploadFile as a tempfile.SpooledTemporaryFile
        # I think this needs to be handled by some `FlowBuilder` class, transfrom a yaml_load to a config
        if isinstance(config, str) or isinstance(config, SpooledTemporaryFile):
            yamlspec = config.read().decode() if isinstance(
                config, SpooledTemporaryFile) else config
            try:
                yaml.register_class(Flow)
                flow = yaml.load(yamlspec)
            except Exception as e:
                self.logger.error(
                    f'Got error while loading from yaml {repr(e)}')
                raise FlowYamlParseException
        elif isinstance(config, list):
            try:
                flow = Flow()
                # it is strange to build from a given flow, it seems like a lazy construction pattern could be used?
                flow = self._build_with_pods(flow=flow, pod_args=config)
            except Exception as e:
                self.logger.error(
                    f'Got error while creating flows via pods: {repr(e)}')
                raise FlowCreationException
        else:
            raise FlowBadInputException(
                f'Not valid Flow config input {type(config)}')

        try:
            flow.args.log_id = flow.args.identity if 'identity' in flow.args else get_random_identity(
            )
            flow_id = uuid.UUID(flow.args.log_id)
            flow = self._start(context=flow)
        except PeaFailToStart as e:
            self.logger.critical(
                f'Flow couldn\'t get started - Invalid Pod {repr(e)} ')
            self.logger.critical(
                'Possible causes - invalid/not uploaded pod yamls & pymodules')
            # TODO: Send correct error message
            raise FlowStartException(repr(e))
        except Exception as e:
            self.logger.critical(
                f'Got following error while starting the flow: {repr(e)}')
            raise FlowStartException(repr(e))

        self._store[flow_id] = {}
        self._store[flow_id]['flow'] = flow
        self._store[flow_id]['files'] = files
        self.logger.info(
            f'Started flow with flow_id {colored(flow_id, "cyan")}')
        return flow_id, flow.host, flow.port_expose