Beispiel #1
0
    def initiate(self, state, factory):
        self._init(factory)

        desc_factory = applications.lookup_descriptor(factory.agent_type)
        f = state.agent.save_document(desc_factory())
        f.add_callback(self._store_descriptor)
        f.add_callback(fiber.drop_param, self._try_next)
        return f
Beispiel #2
0
def build(document_type, **options):
    '''Builds document of selected class with default parameters for testing'''

    doc_class = applications.lookup_descriptor(document_type)
    if not doc_class:
        raise AttributeError("Unknown document type: %r", document_type)

    name = "%s_factory" % re.sub(r'-', '_', document_type.lower())
    module = sys.modules[__name__]
    members = inspect.getmembers(module, lambda x: inspect.isfunction(x) and\
                                 x.__name__ == name)
    if len(members) != 1:
        factory = descriptor_factory
    else:
        _, factory = members[0]
    return doc_class(**factory(**options))
Beispiel #3
0
 def spawn_agent(self, state, desc, static_name=None, **kwargs):
     """
     This method is used by the agency to spawn agents. The desc parameter
     can actually be a descriptor to be save into database, or just
     it's identifier, it which case it should be created and saved
     with default values.
     """
     if not IDescriptor.providedBy(desc):
         factory = applications.lookup_descriptor(desc)
         if factory is None:
             msg = ('No descriptor factory found for agent %r' % desc)
             raise error.FeatError(msg)
         desc = factory()
     f = self.save_document(desc)
     f.add_callback(self.start_agent, static_name=static_name, **kwargs)
     f.add_errback(self._spawn_agent_failed, desc, alert_name=static_name)
     return f
Beispiel #4
0
    def spawn_next_agent(self, state):

        def handle_success(entry):
            f = self.remove_import_entry(entry)
            f.add_callback(fiber.drop_param, self.spawn_next_agent)
            return f

        def handle_failure(fail, entry):
            msg = ("Failed to start agent, import entry: %r" % entry)
            self.raise_alert('spawning', msg)

        if state.spawn_running:
            return

        self._set_semaphore(True)

        entry = self.get_top_import_entry()
        if entry is not None:
            factory = applications.lookup_descriptor(entry.agent_type)
            if factory is None:
                raise ValueError('Unknown agent type: %r' %
                                 (entry.agent_type, ))
            doc = factory()
            kwargs = dict()
            if entry.blackbox:
                kwargs['blackbox'] == entry.blackbox
            f = self.save_document(doc)
            f.add_callback(fiber.inject_param, 2, self.initiate_protocol,
                           start_agent.GloballyStartAgent, **kwargs)
            f.add_callback(fiber.call_param, 'notify_finish')
            f.add_callbacks(fiber.drop_param, handle_failure,
                            cbargs=(handle_success, entry),
                            ebargs=(entry, ))
            f.add_both(fiber.bridge_param, self._set_semaphore, False)

            return f