private async Task ProcessMessage(IModel channel, BasicDeliverEventArgs args)
        {
            using (metrics.Measure.Timer.Time(this.queueProcessTimer))
            {
                string gaugeId = null;
                var    sw      = Stopwatch.StartNew();

                try
                {
                    var json = Encoding.UTF8.GetString(args.Body);
                    this.logger.LogTrace("Received river flow request:{0}{1}", Environment.NewLine, json);

                    var request = JsonConvert.DeserializeObject <RiverFlowRequest>(json);
                    gaugeId = Usgs.FormatGaugeId(request.UsgsGaugeId);
                    await this.riverFlowProcessor.Process(gaugeId);

                    channel.BasicAck(args.DeliveryTag, this.queueConfig.MultipleAck);
                }
                catch (Exception ex)
                {
                    this.logger.LogError(ex, "Error processing gauge {gauge}", gaugeId ?? "unknown");
                    channel.BasicNack(
                        args.DeliveryTag,
                        multiple: this.queueConfig.MultipleAck,
                        requeue: this.queueConfig.FailureRequeue);
                    this.metrics.Measure.Counter.Increment(this.failureCounter);
                    throw;
                }
                finally
                {
                    sw.Stop();
                    this.logger.LogInformation("Processed gauge {gauge} in {time}", gaugeId, sw.Elapsed.Humanize());
                }
            }
        }
示例#2
0
        public void Publish(int?top = null)
        {
            this.logger.LogDebug("Initializing connection to {host}", this.queueConnectionFactory.HostName);

            using (var queueConn = this.queueConnectionFactory.CreateConnection())
                using (var queueChannel = queueConn.CreateModel())
                {
                    InitializeQueue(queueConn, queueChannel);

                    var type     = typeof(QueuePublisher).GetTypeInfo();
                    var resource = $"{type.Namespace}.usgs-sitecodes-filtered.csv";
                    this.logger.LogInformation("Reading {resource}", resource);

                    using (var resourceStream = type.Assembly.GetManifestResourceStream(resource))
                        using (var streamReader = new StreamReader(resourceStream))
                            using (var csv = new CsvReader(streamReader))
                            {
                                csv.Read();
                                csv.ReadHeader();
                                var read = 0;

                                while (csv.Read() && (top == null || read < top))
                                {
                                    var usgsGaugeId = Usgs.FormatGaugeId(csv["UsgsGaugeId"]);
                                    PublishOne(queueChannel, usgsGaugeId);
                                    read++;
                                }
                            }
                }
        }
        public async Task Process(string usgsGaugeId)
        {
            usgsGaugeId = Usgs.FormatGaugeId(usgsGaugeId);
            RiverFlowSnapshot flowData = null;

            using (metrics.Measure.Timer.Time(this.requestTimer))
            {
                flowData = await this.GetRiverFlowData(usgsGaugeId);
            }

            if (flowData != null)
            {
                using (metrics.Measure.Timer.Time(this.recordTimer))
                {
                    await this.flowClient.RecordFlow(flowData);
                }
            }
        }
        public void FormatGaugeId_Empty_ThrowsArgumentException()
        {
            Action act = () => Usgs.FormatGaugeId(string.Empty);

            act.Should().Throw <ArgumentException>();
        }
        public void FormatGaugeId_Null_ThrowsArgumentNullException()
        {
            Action act = () => Usgs.FormatGaugeId(null);

            act.Should().Throw <ArgumentNullException>();
        }
 public void FormatGaugeId_MoreThan8Characters_NoChange()
 {
     Usgs.FormatGaugeId("013539600").Should().Be("013539600");
 }
 public void FormatGaugeId_LessThan8Characters_PadLeftZeros()
 {
     Usgs.FormatGaugeId("3539600").Should().Be("03539600");
 }