Example #1
0
        /// <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);
        }
Example #2
0
        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} завершилась ошибкой" }));
                }
            }
        }
Example #3
0
        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);
        }