public IEnumerable<RecoveryMessage> GetMessagesFromQueue(IQueueOptions options) { using (var connection = RabbitConnection.FromOptions(options)) using (var channel = connection.CreateModel()) { try { channel.QueueDeclarePassive(options.ErrorQueueName); } catch (OperationInterruptedException exception) { _log.ErrorFormat("Error Occurred Getting a message from queue because {0}", exception, exception.Message); yield break; } var count = 0; while (count++ < options.MaxNumberOfMessagesToRetrieve) { var basicGetResult = channel.BasicGet(options.ErrorQueueName, noAck: options.RequireHandshake); if (basicGetResult == null) break; // no more messages on the queue var properties = new MessageProperties(basicGetResult.BasicProperties); var info = new MessageReceivedInfo( "recoveries", basicGetResult.DeliveryTag, basicGetResult.Redelivered, basicGetResult.Exchange, basicGetResult.RoutingKey, options.ErrorQueueName); yield return new RecoveryMessage(Encoding.UTF8.GetString(basicGetResult.Body), properties, info); } } }
public void Handle(IQueueOptions options) { var results = new StringBuilder(); var succeeded = true; Func<string, Action> messsage = m => () => { results.AppendLine(m); succeeded = false; }; try { Delete(options); ErrorDump(options); Purge(options); Retry(options); } catch (EasyNetQException ex) { Log.ErrorFormat("Operation Failed in Recovery Service because of {0}", ex, ex.Message); } if (succeeded) return; Log.Error("Operation failed"); Log.Error(results.ToString()); }
public void Write(IEnumerable<RecoveryMessage> messages, IQueueOptions options) { var count = 0; foreach (var message in messages) { var uniqueFileName = SanitiseQueueName(options.ErrorQueueName) + "." + count.ToString(); var bodyPath = Path.Combine(options.MessageFilePath, uniqueFileName + ".message.txt"); var propertiesPath = Path.Combine(options.MessageFilePath, uniqueFileName + ".properties.txt"); var infoPath = Path.Combine(options.MessageFilePath, uniqueFileName + ".info.txt"); if (File.Exists(bodyPath)) Log.InfoFormat("Overwriting existing messsage file: {0}", bodyPath); try { File.WriteAllText(bodyPath, message.Body); File.WriteAllText(propertiesPath, Newtonsoft.Json.JsonConvert.SerializeObject(message.Properties)); File.WriteAllText(infoPath, Newtonsoft.Json.JsonConvert.SerializeObject(message.Info)); } catch (DirectoryNotFoundException) { throw new EasyNetQException(string.Format("Directory '{0}' does not exist", options.MessageFilePath)); } count++; } }
public void RepublishError(Error error, IQueueOptions options) { using (var connection = RabbitConnection.FromOptions(options)) using (var model = connection.CreateModel()) { try { if (error.Exchange != string.Empty) { model.ExchangeDeclarePassive(error.Exchange); } var properties = model.CreateBasicProperties(); error.BasicProperties.CopyTo(properties); var body = Encoding.UTF8.GetBytes(error.Message); model.BasicPublish(error.Exchange, error.RoutingKey, properties, body); } catch (OperationInterruptedException) { Log.ErrorFormat("The exchange, '{0}', described in the error message does not exist on '{1}', '{2}'", error.Exchange, options.HostName, options.VirtualHost); } } }
public IEnumerable<RecoveryMessage> ReadMessages(IQueueOptions options, string messageName) { if (!Directory.Exists(options.MessageFilePath)) { Log.ErrorFormat("Directory '{0}' does not exist", options.MessageFilePath); Log.InfoFormat("Creating Directory at '{0}'", options.MessageFilePath); options.MessageFilePath.CreateDirectory(); Log.InfoFormat("Directory created at '{0}'", options.MessageFilePath); } var bodyPattern = (messageName ?? "*") + ".*.message.txt"; foreach (var file in Directory.GetFiles(options.MessageFilePath, bodyPattern)) { const string messageTag = ".message."; var directoryName = Path.GetDirectoryName(file); var fileName = Path.GetFileName(file); var propertiesFileName = Path.Combine(directoryName, fileName.Replace(messageTag, ".properties.")); var infoFileName = Path.Combine(directoryName, fileName.Replace(messageTag, ".info.")); var body = File.ReadAllText(file); var propertiesJson = File.ReadAllText(propertiesFileName); var properties = Newtonsoft.Json.JsonConvert.DeserializeObject<MessageProperties>(propertiesJson); var infoJson = File.ReadAllText(infoFileName); var info = Newtonsoft.Json.JsonConvert.DeserializeObject<MessageReceivedInfo>(infoJson); yield return new RecoveryMessage(body, properties, info); } }
private void Dump(IQueueOptions options) { var count = 0; _messageWriter.Write(WithEach(_queueRetreival.GetMessagesFromQueue(options), () => count++), options); Log.InfoFormat("{0} Messages from queue '{1}'\r\noutput to directory '{2}'", count, options.ErrorQueueName, options.MessageFilePath); }
public void RetryErrors(IEnumerable<RecoveryMessage> rawErrorMessages, IQueueOptions options) { foreach (var rawErrorMessage in rawErrorMessages) { var error = _serializer.BytesToMessage<Error>(Encoding.UTF8.GetBytes(rawErrorMessage.Body)); RepublishError(error, options); } }
public IEventQueuer BuildQueueProvider(string queueName, IQueueOptions queueOptions) { if (queueOptions.ProviderType != this.Name) { throw new Exceptions.BadQueueProvider(this.Name, queueName); } return(this.BuildQueueInternal(queueName, queueOptions.ParseQueueOptions <TParsedOptions, TInstanceOptions, TQueueOptions>(options))); }
public void PublishMessagesToQueue(IEnumerable<RecoveryMessage> messages, IQueueOptions options) { using (var connection = RabbitConnection.FromOptions(options)) using (var channel = connection.CreateModel()) { foreach (var message in messages) { var body = Encoding.UTF8.GetBytes(message.Body); var properties = new BasicProperties(); message.Properties.CopyTo(properties); channel.BasicPublish(message.Info.Exchange, message.Info.RoutingKey, properties, body); } } }
public IEnumerable<int> DeleteMessages(IQueueOptions options) { if (!Directory.Exists(options.MessageFilePath)) { Log.ErrorFormat("Directory '{0}' does not exist", options.MessageFilePath); Log.InfoFormat("Creating Directory at '{0}'", options.MessageFilePath); options.MessageFilePath.CreateDirectory(); Log.InfoFormat("Directory created at '{0}'", options.MessageFilePath); } var count = 0; foreach (var file in new DirectoryInfo(options.MessageFilePath).GetFiles()) { file.Delete(); count++; yield return count; } }
public static IConnection FromOptions(IQueueOptions options) { var connectionFactory = new ConnectionFactory { HostName = options.HostName, VirtualHost = options.VirtualHost, UserName = options.Username, Password = options.Password }; try { return connectionFactory.CreateConnection(); } catch (BrokerUnreachableException) { throw new EasyNetQException( string.Format("The broker at '{0}', VirtualHost '{1}', is unreachable. This message can also be caused by incorrect credentials.", options.HostName, options.VirtualHost)); } }
private IEventProvider GetProvider(IQueueOptions configuration, bool throwIfNotFound = true) { string providerTypeName = null; if (!string.IsNullOrEmpty(configuration.ProviderType)) { providerTypeName = configuration.ProviderType; } else if (!string.IsNullOrEmpty(configuration.ProviderName)) { this.options.ParsedProviderInstances.TryGetValue(configuration.ProviderName, out var providersInstanceOptions); if (providersInstanceOptions != null) { providerTypeName = providersInstanceOptions.Type; } else if (throwIfNotFound) { throw new Exceptions.BadProviderConfiguration(configuration.ProviderName, "Unable to find it in the configuration "); } } else if (throwIfNotFound) { throw new Exceptions.BadQueueConfiguration(configuration.Name, "You must set either 'ProviderType or 'ProviderName' in event configuration."); } if (string.IsNullOrEmpty(providerTypeName)) { return(null); } this.queueProviders.TryGetValue(providerTypeName, out var provider); if (provider == null && throwIfNotFound) { throw new Exceptions.ProviderNotFound(providerTypeName); } return(provider); }
public IEnumerable<uint> PurgeQueue(IQueueOptions options) { using (var connection = RabbitConnection.FromOptions(options)) using (var channel = connection.CreateModel()) { uint count = 0; try { var messages = channel.BasicGet(options.ErrorQueueName, true); if (messages == null) yield break; channel.QueueBind(options.ErrorQueueName, messages.Exchange,messages.RoutingKey); count = messages.MessageCount; channel.QueuePurge(options.ErrorQueueName); } catch (OperationInterruptedException exception) { Log.ErrorFormat("Error Occurred Getting a message from queue because {0}", exception, exception.Message); yield break; } yield return count; } }
public IEnumerable<RecoveryMessage> ReadMessages(IQueueOptions options) { return ReadMessages(options, null); }
public IEventQueuer GetQueuer(string queueName, IQueueOptions configuration) { return(this.GetProvider(configuration).BuildQueueProvider(queueName, configuration)); }
private void Retry(IQueueOptions options) { var count = 0; _errorRetry.RetryErrors( WithEach( _messageReader.ReadMessages(options, options.ErrorQueueName), () => count++), options); Log.InfoFormat("{0} Error messages from directory '{1}' republished", count, options.MessageFilePath); }
private void ErrorDump(IQueueOptions options) { Dump(options); }
private void Purge(IQueueOptions options) { var count = 0; var messages = WithEach(_errorPurge.PurgeQueue(options), () => count++).ToList(); Log.InfoFormat("{0}/{2} messages from Queue '{1}' have been Purged", count, options.ErrorQueueName, messages.Any() ? messages.Max() : 0); }
private void Delete(IQueueOptions options) { var count = 0; var files = WithEach(_errorDelete.DeleteMessages(options), () => count++).ToList(); Log.InfoFormat("{0}/{2} Error messages from directory '{1}' have been deleted", count, options.MessageFilePath, files.Any() ? files.Max() : 0); }
public static TQueueOptions ParseQueueOptions <TParsedOptions, TInstanceOptions, TQueueOptions>(this IQueueOptions queueOptions, TParsedOptions options) where TParsedOptions : class, IParsedOptions <TInstanceOptions, TQueueOptions>, new() where TInstanceOptions : class, IProviderInstanceOptions, new() where TQueueOptions : class, IQueueOptions, new() { if (!(queueOptions is TQueueOptions parsedQueueOptions)) { parsedQueueOptions = new TQueueOptions { Name = queueOptions.Name, ProviderName = queueOptions.ProviderName, ProviderType = queueOptions.ProviderType }; } parsedQueueOptions.Compute <TParsedOptions, TInstanceOptions, TQueueOptions>(options); return(parsedQueueOptions); }