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
def initiate(self, state, doc_id, allocation_id, kwargs=dict(), static_name=None): if IDescriptor.providedBy(doc_id): doc_id = doc_id.doc_id assert isinstance(doc_id, (str, unicode)), "doc_id is %r" % (doc_id,) state.doc_id = doc_id state.descriptor = None state.allocation_id = allocation_id state.static_name = static_name f = fiber.succeed() f.add_callback(fiber.drop_param, self._fetch_descriptor) f.add_callback(fiber.drop_param, self._check_requirements) f.add_callback(fiber.drop_param, self._validate_allocation) f.add_callback(self._update_descriptor) f.add_callback(state.agent.medium_start_agent, **kwargs) f.add_callback(recipient.IRecipient) f.add_callback(self._establish_partnership) return f
def clean_all_descriptors(connection, dry_run=False): rows = yield connection.query_view(view.DocumentByType, group_level=1, parse_results=False) to_delete = list() for row in rows: type_name = row[0][0] restorator = serialization.lookup(type_name) if not restorator: log.info('cleanup', 'Could not lookup restorator for type name: %s. ' 'There is %s documents of this type.', type_name, row[1]) continue if IDescriptor.implementedBy(restorator): log.info('cleanup', 'I will delete %s documents of type name: %s', row[1], type_name) to_delete.append(type_name) if dry_run: log.info("cleanup", "Not deleting anything, this is just a dry run.") return for type_name in to_delete: keys = view.DocumentByType.fetch(type_name) keys['include_docs'] = False rows = yield connection.query_view( view.DocumentByType, parse_results=False, **keys) for (key, value, doc_id) in rows: try: yield connection.update_document(doc_id, update.delete) except Exception as e: log.error("cleanup", "Cannot delete the documents of type %s with ID: %s." " Reason: %s", type_name, doc_id, e)
def lookup_descriptor(name): r = serialization.lookup(name) if r is not None and not IDescriptor.implementedBy(r): raise TypeError("lookup_descriptor() tried to return %r" % (r,)) return r