Esempio n. 1
0
def handle_related_topics_on_create(
        scheduler: TopicSnapshotScheduler, topic_service: TopicService,
        source_topic: Topic, principal_service: PrincipalService
) -> Tuple[Topic, Callable[[], None]]:
    target_topic: Optional[Topic] = topic_service.find_by_name_and_tenant(
        scheduler.targetTopicName, scheduler.tenantId)
    if target_topic is not None:
        raise_500(None,
                  f'Topic[name={scheduler.targetTopicName}] already exists.')

    # create target topic
    target_topic = create_snapshot_target_topic(scheduler, source_topic)
    target_topic, target_topic_tail = ask_save_topic_action(
        topic_service, principal_service)(target_topic)

    scheduler.targetTopicId = target_topic.topicId
    tail = target_topic_tail

    # find task topic, it might be created by another scheduler
    task_topic_name = as_snapshot_task_topic_name(source_topic)
    task_topic: Optional[Topic] = topic_service.find_by_name_and_tenant(
        task_topic_name, scheduler.tenantId)
    # create task topic only it is not declared
    if task_topic is None:
        task_topic = create_snapshot_task_topic(source_topic)
        task_topic, task_topic_tail = ask_save_topic_action(
            topic_service, principal_service)(task_topic)
        tail = combine_tail_actions([target_topic_tail, task_topic_tail])

    # create pipeline from task topic to target topic
    pipeline = create_snapshot_pipeline(task_topic, target_topic)
    pipeline_service = get_pipeline_service(topic_service)
    pipeline = ask_save_pipeline_action(pipeline_service,
                                        principal_service)(pipeline)

    scheduler.pipelineId = pipeline.pipelineId

    return target_topic, tail
	def find_schema_by_name(self, name: str, tenant_id: TenantId) -> Optional[TopicSchema]:
		if not self.principalService.is_super_admin():
			if self.principalService.get_tenant_id() != tenant_id:
				raise Exception('Forbidden')

		schema = None
		topic = CacheService.topic().get_by_name(name, tenant_id)
		if topic is not None:
			schema = CacheService.topic().get_schema(topic.topicId)
		if schema is not None:
			return schema

		storage_service = TopicStorageService(ask_meta_storage(), ask_snowflake_generator(), self.principalService)
		storage_service.begin_transaction()
		try:
			# noinspection PyTypeChecker
			topic: Topic = storage_service.find_by_name_and_tenant(name, tenant_id)
			if topic is None:
				return None

			CacheService.topic().put(topic)
			return CacheService.topic().get_schema(topic.topicId)
		finally:
			storage_service.close_transaction()