/// <summary>
        /// Отправка веб-хука на uri из настроек
        /// </summary>
        /// <param name="state"></param>
        private void SendHook(object state)
        {
            Task.Run(async() => await _telemetryCollector.WithStopwatch(() =>
            {
                lock (_lock)
                {
                    try
                    {
                        WebHookRecord hookRecord;
                        WebHookInfo hooInfo;
                        if (_webHooks.TryDequeue(out hookRecord) &&//извлекаем хук
                            !WebHookRecord.IsEmpty(hookRecord) &&
                            _webHooksInfo.TryRemove(hookRecord.ResourceId, out hooInfo) &&     //извлекаем его доп инфу
                            !WebHookInfo.IsEmpty(hooInfo) &&
                            hooInfo.MustDieAt >= DateTime.Now)       //проверяем не протух ли веб-хук
                        {
                            var httpClient = new HttpClient();
                            var request    = new HttpRequestMessage(HttpMethod.Post, _options.Value.WebHookUri)
                            {
                                Content =
                                    new StringContent($"{{\"resourceid\":\"{hookRecord.ResourceId}\", \"eventtype\":\"{hookRecord.EventType}\",\"newvalue\":\"{hookRecord.NewValue}\"}}"
                                                      , Encoding.UTF8
                                                      , "application/json")
                            };
                            HttpResponseMessage responce = null;
                            Task.WhenAny(Task.Run(async() => { responce = await httpClient.SendAsync(request); }));

                            if (responce == null || responce.StatusCode != System.Net.HttpStatusCode.OK)
                            {
                                _webHooksInfo.TryAdd(hooInfo.ResourceId, hooInfo);
                                _webHooks.Enqueue(hookRecord);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        // необходимо добавить логирование ошибок
                    }
                }
            }));
        }
 /// <summary>
 /// Обработка получения веб-хука от шины сообщений
 /// </summary>
 /// <param name="webHook"></param>
 private void ReceiveWebHookRecord(WebHookRecord webHook)
 {
     Task.WhenAny(_telemetryCollector.WithStopwatch(() =>
     {
         lock (_lock)
         {
             var newWebHookInfo = new WebHookInfo {
                 ResourceId = webHook.ResourceId, MustDieAt = DateTime.Now.AddMinutes(_options.Value.WebHookLifetime)
             };
             if (_webHooksInfo.ContainsKey(webHook.ResourceId))
             {
                 _webHooksInfo.TryGetValue(webHook.ResourceId, out WebHookInfo oldValue);
                 _webHooksInfo.TryUpdate(webHook.ResourceId, newWebHookInfo, oldValue);
             }
             else
             {
                 _webHooksInfo.TryAdd(webHook.ResourceId, newWebHookInfo);
             }
             _webHooks.Enqueue(webHook);
         }
     }));
 }
Example #3
0
 public static bool IsEmpty(WebHookInfo hookInfo)
 {
     return(hookInfo.ResourceId == 0 && hookInfo.MustDieAt == DateTime.MinValue);
 }