예제 #1
0
 private static NotificationChannel GetChannel(NotificationSenderConfig config, string channel)
 {
     return(config.Channels.FirstOrDefault(x => x.Name == channel));
 }
예제 #2
0
        private async Task SendOneMessage(
            string customerCode,
            string instanceId,
            NotificationSenderConfig config,
            NotificationChannel channel,
            IMessageService service,
            Message message,
            SemaphoreSlim semaphore,
            TaskFactory factory,
            ChannelState state,
            INotificationChannelService channelService)
        {
            await factory.StartNew(() =>
            {
                lock (state)
                {
                    if (state.ErrorsCount >= config.ErrorCountBeforeWait)
                    {
                        return;
                    }
                }

                var timer = new Stopwatch();
                timer.Start();
                string url = GetUrl(customerCode, instanceId, channel, message);

                try
                {
                    semaphore.Wait();
                    _logger.Debug("Start processing message {messageId} ", message.Id);


                    var request           = (HttpWebRequest)WebRequest.Create(url);
                    request.Method        = message.Method.ToUpper();
                    request.Timeout       = 1000 * config.TimeOut;
                    var mediaType         = !string.IsNullOrEmpty(channel.MediaType) ? channel.MediaType : "text/xml";
                    request.ContentType   = $"{mediaType}; charset=utf-8";
                    byte[] data           = Encoding.UTF8.GetBytes(message.Xml);
                    request.ContentLength = data.Length;

                    using (var streamWriter = request.GetRequestStream())
                    {
                        streamWriter.Write(data, 0, data.Length);
                        streamWriter.Flush();
                    }

                    using (var httpResponse = (HttpWebResponse)request.GetResponse())
                    {
                        timer.Stop();
                        _logger.Info()
                        .Message(
                            "Message {message} for channel {channel} has been sent on url {url}",
                            message.Method, channel.Name, Uri.UnescapeDataString(url)
                            )
                        .Property("productId", message.Key)
                        .Property("statusCode", httpResponse.StatusCode)
                        .Property("timeTaken", timer.ElapsedMilliseconds)
                        .Property("messageId", message.Id)
                        .Property("customerCode", customerCode)
                        .Write();

                        channelService.UpdateNotificationChannel(customerCode, channel.Name, message.Key,
                                                                 message.Created, httpResponse.StatusCode.ToString());
                    }

                    ;

                    service.RemoveMessage(message.Id);
                }
                catch (WebException ex)
                {
                    timer.Stop();

                    lock (state)
                    {
                        state.ErrorsCount++;
                    }

                    var httpResponse = ex.Response as HttpWebResponse;

                    if (httpResponse != null)
                    {
                        _logger.Info()
                        .Message(
                            "Message {message} for channel {channel} has been sent on url {url}",
                            message.Method, channel.Name, Uri.UnescapeDataString(url)
                            )
                        .Property("productId", message.Key)
                        .Property("statusCode", httpResponse.StatusCode)
                        .Property("timeTaken", timer.ElapsedMilliseconds)
                        .Property("messageId", message.Id)
                        .Property("customerCode", customerCode)
                        .Write();

                        channelService.UpdateNotificationChannel(customerCode, channel.Name, message.Key,
                                                                 message.Created, httpResponse.StatusCode.ToString());
                    }
                    else
                    {
                        _logger.Info()
                        .Message(
                            "Message {message} for channel {channel} has not been sent on url {url}",
                            message.Method, channel.Name, Uri.UnescapeDataString(url)
                            )
                        .Property("productId", message.Key)
                        .Property("statusCode", ex.Status)
                        .Property("timeTaken", timer.ElapsedMilliseconds)
                        .Property("messageId", message.Id)
                        .Property("customerCode", customerCode)
                        .Write();

                        channelService.UpdateNotificationChannel(customerCode, channel.Name, message.Key,
                                                                 message.Created, ex.Status.ToString());
                    }

                    _logger.Error().Exception(ex)
                    .Message(
                        "Message {message} for channel {channel} has not been sent on url {url}",
                        message.Method, channel.Name, Uri.UnescapeDataString(url)
                        )
                    .Property("productId", message.Key)
                    .Property("timeTaken", timer.ElapsedMilliseconds)
                    .Property("messageId", message.Id)
                    .Property("customerCode", customerCode)
                    .Write();
                }
                finally
                {
                    semaphore.Release();
                }
            });
        }