public virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { _db.Dispose(); } this.disposed = true; } }
private static Task SendHooks(string accountId, List <AccountTask> accountTaskList) { return(Task.Run(async() => { Console.WriteLine(String.Format("{0}: Поток {1} запустился", DateTime.Now.ToString("T"), Thread.CurrentThread.ManagedThreadId)); var contextOptions = new DbContextOptionsBuilder <WebHooksContext>() .UseSqlServer("") .Options; ApiClient apiClient = new ApiClient(); List <EventHook> eventHooks = new List <EventHook>(); EventHook hook = new EventHook(); while (true) { using (var context = new WebHooksContext(contextOptions)) { context.Database.SetCommandTimeout(30); IRepository <EventHook> eventHookRepository = new GenericRepository <EventHook>(context); //IRepository<HookTry> hookTryRepository = new GenericRepository<HookTry>(context); try { eventHooks = await eventHookRepository .GetQuery(e => e.Subscription) .AsNoTracking() .Where(e => e.IsActual && e.Subscription.ExternalAccountId == accountId && e.Subscription.IsActual && e.SendTime <= DateTime.UtcNow && e.SendTime != null) .OrderBy(e => e.SendTime) .Take(50) .ToListAsync(); Console.WriteLine(String.Format("{0}: Поток {1} получил записи из базы данных", DateTime.Now.ToString("T"), Thread.CurrentThread.ManagedThreadId)); } catch (Exception ex) { Console.WriteLine(String.Format("{0}: Ошибка! {1}", DateTime.Now.ToString("T"), ex)); lock (accountTaskList) { accountTaskList.Remove(accountTaskList.Where(a => a.ExternalAccountId == accountId).FirstOrDefault()); Console.WriteLine(String.Format("{0}: В Потоке {1} удалена задача", DateTime.Now.ToString("T"), Thread.CurrentThread.ManagedThreadId)); } return; } } if (eventHooks.Count == 0) { Console.WriteLine(String.Format("{0}: В Потоке {1} нет записей, пытаемся удалить задачу", DateTime.Now.ToString("T"), Thread.CurrentThread.ManagedThreadId)); lock (accountTaskList) { accountTaskList.Remove(accountTaskList.Where(a => a.ExternalAccountId == accountId).FirstOrDefault()); Console.WriteLine(String.Format("{0}: В Потоке {1} удалена задача", DateTime.Now.ToString("T"), Thread.CurrentThread.ManagedThreadId)); } return; } using (var context = new WebHooksContext(contextOptions)) { context.Database.SetCommandTimeout(30); IRepository <EventHook> eventHookRepository = new GenericRepository <EventHook>(context); List <HookTry> hookTryes = new List <HookTry>(); for (int i = 0; i < eventHooks.Count(); i++) { HookTry hookTry = new HookTry(); hook = eventHooks[i]; hookTry.EventHookId = hook.Id; hookTry.CreateTime = DateTime.UtcNow; hookTry.IsActual = true; HttpResponseMessage response = new HttpResponseMessage(); try { response = await apiClient.SendRequest(hook.Subscription.Url, hook.Subscription.HttpMethod, hook.Subscription.SecretKey, hook.Subscription.ContentType, hook.Event); if (response.StatusCode != HttpStatusCode.OK && response.StatusCode != HttpStatusCode.Created && response.StatusCode != HttpStatusCode.NoContent) { hook.TryCount += 1; if (hook.TryCount >= 3) { hook.Status = "FAILURE"; hook.SendTime = null; } else { hook.Status = "REPEAT"; if (hook.SendTime != null) { hook.SendTime = DateTime.UtcNow.AddMinutes(5.0); } } } else { hook.TryCount += 1; hook.SendTime = null; hook.Status = "DONE"; } hookTry.Request = String.Format("Method: {0} {1}\nContent-Type: {2}\nParams: {3}", hook.Subscription.HttpMethod, hook.Subscription.Url, hook.Subscription.ContentType, hook.Event); hookTry.Response = apiClient.GetResponseContent(response); hookTry.HttpStatus = ((int)response.StatusCode).ToString(); hookTryes.Add(hookTry); } catch (Exception ex) { Console.WriteLine(String.Format("{0}: Ошибка! {1}", DateTime.Now.ToString("T"), ex)); continue; } finally { response.Dispose(); } try { eventHookRepository.ExecuteQuery("UPDATE EventHooks set Status = {0}, TryCount = {1}, SendTime = {3} where Id = {2}", hook.Status, hook.TryCount, hook.Id, hook.SendTime); } catch (Exception ex) { Console.WriteLine(String.Format("{0}: Ошибка! {1}", DateTime.Now.ToString("T"), ex)); context.Dispose(); lock (accountTaskList) { accountTaskList.Remove(accountTaskList.Where(a => a.ExternalAccountId == accountId).FirstOrDefault()); Console.WriteLine(String.Format("{0}: В Потоке {1} удалена задача", DateTime.Now.ToString("T"), Thread.CurrentThread.ManagedThreadId)); } return; } } try { context.ChangeTracker.AutoDetectChangesEnabled = false; context.HookTryes.AddRange(hookTryes.ToArray()); await context.SaveChangesAsync(); context.ChangeTracker.AutoDetectChangesEnabled = true; } catch (Exception ex) { Console.WriteLine(String.Format("{0}: Ошибка! {1}", DateTime.Now.ToString("T"), ex)); context.Dispose(); lock (accountTaskList) { accountTaskList.Remove(accountTaskList.Where(a => a.ExternalAccountId == accountId).FirstOrDefault()); Console.WriteLine(String.Format("{0}: В Потоке {1} удалена задача", DateTime.Now.ToString("T"), Thread.CurrentThread.ManagedThreadId)); } return; } Console.WriteLine(String.Format("{0}: Поток {1} обновил {2} записей", DateTime.Now.ToString("T"), Thread.CurrentThread.ManagedThreadId, eventHooks.Count())); } } })); }