/// <summary> /// /// </summary> /// <param name="rid"></param> /// <param name="userManager"></param> /// <returns>необходимоть постановки в очередь и сам запрос</returns> public Request RegisterRequest(string rid) { Request request = new Request { IncomingIdentifier = rid, State = RequestState.Registered, }; lock (request_locker)//Долбитьcя с отменой можно в любой id(даже случайный) и не хотлось бы, чтобы это получилось прям сразу после создания { _repo.AddRequest(request); RequestsJournalRecord record = new RequestsJournalRecord { State = RequestState.Registered, Request = request, StateChanged = DateTime.Now }; using (RequestsJournalManager jm = new RequestsJournalManager()) { jm.AddRecord(record); } } return(request); }
public void ErrorRequest(int requestId, CustomUserManager <CustomIdentityUser> userManager, IHubContext <IncomingHub> hubContext) { Request request = _repo.Requests.First(r => r.Id == requestId); lock (request_locker) { using (RequestsJournalManager jm = new RequestsJournalManager()) { //var last = jm.FindLastRecordForRequest(request); if (request.State != RequestState.Aborted && request.State != RequestState.Finalized && request.State != RequestState.Error) { request.State = RequestState.Error; _repo.SaveChanges(); // RequestsJournalRecord record = new RequestsJournalRecord { State = RequestState.Error, Request = request, StateChanged = DateTime.Now }; jm.AddRecord(record); } } if (request.UserId != null) { userManager.FreeUser(userManager.FindById(request.UserId)); hubContext.Clients.User(request.UserId).SendAsync("Send", JsonConvert.SerializeObject(new { message = $"Обработка запроса № {requestId} завершилась ошибкой" })); } } }
public bool TryAssignRequestToUser(int requestId, CustomUserManager <CustomIdentityUser> userManager, IHubContext <IncomingHub> hubContext) { Request request = _repo.Requests.First(r => r.Id == requestId); lock (request_locker) { using (RequestsJournalManager jm = new RequestsJournalManager()) { if (request.State != RequestState.Registered) { throw new Exception($"Попытка назначить запрос \"{request.Id}\", хотя он уже в работе или завершен."); } var Volunteer = userManager.GetFirstFreeUser(jm.GetRegisterTimeForRequest(request), Startup.Settings.Tm, Startup.Settings.Td, hubContext); if (Volunteer != null) { request.UserId = Volunteer.Id; request.State = RequestState.Assigned; _repo.SaveChanges(); // RequestsJournalRecord record = new RequestsJournalRecord { State = RequestState.Assigned, Request = request, StateChanged = DateTime.Now }; jm.AddRecord(record); // hubContext.Clients.User(Volunteer.Id).SendAsync("Send", JsonConvert.SerializeObject(new { message = $"Поздравляю, вы обрабатываете запрос № {request.Id}!!!" })); //Стартуем поток завершения Random rand = new Random(DateTime.Now.Millisecond); StartFinalization(Startup.Settings.FinalizeRangeFrom, Startup.Settings.FinalizeRangeTo, request.Id, Volunteer.Id, userManager, hubContext, rand); // return(true); } } } return(false); }
//public List<RequestsJournalRecord> GetOrderedJournal() => _repo.JournalRecords.Include(jr=>jr.Request).OrderByDescending(r => r.Id).ToList(); //public List<RequestsJournalRecord> GetOrderedJournalForUser(string userId) => _repo.JournalRecords.Include(jr => jr.Request).Where(jr=>jr.Request.UserId==userId).OrderByDescending(r => r.Id).ToList(); public void AddRecord(RequestsJournalRecord record) => _repo.AddJournalRecord(record);
/// <summary> /// /// </summary> /// <param name="record"></param> /// <returns>Request Id</returns> public void AddJournalRecord(RequestsJournalRecord record) { _context.Requests.Attach(record.Request); _context.JournalRecords.Add(record); _context.SaveChanges(); }