private void EnsureMessagingEntityExists(MessageTypeMessagingEntityMappingDetails mappingDetails, IEnumerable <MessageTypeMessagingEntityMappingDetails> allMessageTypePathMappings) { MessagingEntityCreationOptions creationOptions = mappingDetails.CreationOptions; if (creationOptions != MessagingEntityCreationOptions.None && !_verifiedExistingMessagingEntities.Contains(mappingDetails)) { Func <bool> exists; Action create; Action delete; string path = mappingDetails.Path; switch (mappingDetails.MessagingEntityType) { case MessagingEntityType.Queue: exists = () => _namespaceManager.QueueExists(path); create = () => _namespaceManager.CreateQueue(path); delete = () => _namespaceManager.DeleteQueue(path); break; case MessagingEntityType.Topic: exists = () => _namespaceManager.TopicExists(path); create = () => _namespaceManager.CreateTopic(path); delete = () => _namespaceManager.DeleteTopic(path); break; case MessagingEntityType.Subscription: string[] parts = path.Split('/'); string topicPath = parts[0]; string subscriptionName = parts[2]; exists = () => _namespaceManager.SubscriptionExists(topicPath, subscriptionName); create = () => { MessageTypeMessagingEntityMappingDetails topicMessageTypePathMapping = allMessageTypePathMappings.FirstOrDefault(mtpmd => mtpmd.MessagingEntityType == MessagingEntityType.Topic && mtpmd.Path == topicPath); if (topicMessageTypePathMapping == null) { topicMessageTypePathMapping = new MessageTypeMessagingEntityMappingDetails(mappingDetails.MessageType, topicPath, MessagingEntityType.Topic, MessagingEntityCreationOptions.VerifyAlreadyExists); } EnsureMessagingEntityExists(topicMessageTypePathMapping, allMessageTypePathMappings); _namespaceManager.CreateSubscription(topicPath, subscriptionName); }; delete = () => _namespaceManager.DeleteSubscription(topicPath, subscriptionName); break; default: throw new NotSupportedException(string.Format("Unsupported messaging entity type, {0}, requested for creation (path {1}).", mappingDetails.MessagingEntityType, mappingDetails.Path)); } bool alreadyExists = exists(); if (alreadyExists) { if ((creationOptions & MessagingEntityCreationOptions.CreateAsTemporary) != 0) { if ((creationOptions & MessagingEntityCreationOptions.RecreateExistingTemporary) == 0) { throw new MessagingEntityAlreadyExistsException(mappingDetails.Path, mappingDetails.MessagingEntityType); } try { delete(); alreadyExists = false; } catch (UnauthorizedAccessException exception) { throw new UnauthorizedAccessException(string.Format("Unable to delete temporary messaging that already exists at path \"{0}\" due to insufficient access. Make sure the policy being used has 'Manage' permission for the namespace.", mappingDetails.Path), exception); } } } if (!alreadyExists) { if ((creationOptions & (MessagingEntityCreationOptions.CreateIfDoesntExist | MessagingEntityCreationOptions.CreateAsTemporary)) == 0) { throw new MessagingEntityDoesNotAlreadyExistException(mappingDetails.Path, mappingDetails.MessagingEntityType); } try { create(); } catch (UnauthorizedAccessException exception) { throw new UnauthorizedAccessException(string.Format("Unable to create messaging entity at path \"{0}\" due to insufficient access. Make sure the policy being used has 'Manage' permission for the namespace.", mappingDetails.Path), exception); } } _verifiedExistingMessagingEntities.Add(mappingDetails); } }
public async Task <TopicDescription> Create(string topicPath, INamespaceManager namespaceManager) { var topicDescription = topicDescriptionFactory(topicPath, settings); try { if (!await ExistsAsync(topicPath, namespaceManager).ConfigureAwait(false)) { await namespaceManager.CreateTopic(topicDescription).ConfigureAwait(false); logger.InfoFormat("Topic '{0}' in namespace '{1}' created.", topicDescription.Path, namespaceManager.Address); var key = GenerateTopicKey(topicPath, namespaceManager); await rememberExistence.AddOrUpdate(key, notFoundTopicPath => Task.FromResult(true), (updateTopicPath, previousValue) => Task.FromResult(true)).ConfigureAwait(false); } else { logger.InfoFormat("Topic '{0}' in namespace '{1}' already exists, skipping creation.", topicDescription.Path, namespaceManager.Address); logger.InfoFormat("Checking if topic '{0}' in namespace '{1}' needs to be updated.", topicDescription.Path, namespaceManager.Address); var existingTopicDescription = await namespaceManager.GetTopic(topicDescription.Path).ConfigureAwait(false); if (MembersAreNotEqual(existingTopicDescription, topicDescription)) { OverrideImmutableMembers(existingTopicDescription, topicDescription); logger.InfoFormat("Updating topic '{0}' in namespace '{1}' with new description.", topicDescription.Path, namespaceManager.Address); await namespaceManager.UpdateTopic(topicDescription).ConfigureAwait(false); } } } catch (MessagingEntityAlreadyExistsException) { // the topic already exists or another node beat us to it, which is ok logger.InfoFormat("Topic '{0}' in namespace '{1}' already exists, another node probably beat us to it.", topicDescription.Path, namespaceManager.Address); } catch (TimeoutException) { logger.InfoFormat("Timeout occurred on topic creation for '{0}' in namespace '{1}' going to validate if it doesn't exist.", topicDescription.Path, namespaceManager.Address); // there is a chance that the timeout occurred, but the topic was still created, check again if (!await ExistsAsync(topicDescription.Path, namespaceManager, removeCacheEntry: true).ConfigureAwait(false)) { throw; } logger.InfoFormat("Looks like topic '{0}' in namespace '{1}' exists anyway.", topicDescription.Path, namespaceManager.Address); } catch (MessagingException ex) { var loggedMessage = string.Format("{1} {2} occurred on topic creation '{0}' in namespace {3}.", topicDescription.Path, (ex.IsTransient ? "Transient" : "Non transient"), ex.GetType().Name, namespaceManager.Address); if (!ex.IsTransient) { logger.Fatal(loggedMessage, ex); throw; } logger.Info(loggedMessage, ex); } return(topicDescription); }