public static void CourierStatusThread(object state) { var diagnosticService = Program.Container.Resolve <IDiagnosticService>(); var courierService = Program.Container.Resolve <ICourierService>(); var systemEventService = Program.Container.Resolve <ISystemEventService>(); var timeout = 30; int.TryParse(XmlConfigReader.GetValue("CourierStatusTimeoutBySecond"), out timeout); try { var listCourier = courierService.ListAll(); var listUpdate = new List <Courier>(); //status online/ofline foreach (var courier in listCourier.Where(courier => (DateTime.UtcNow - courier.LastTime.GetValueOrDefault()).TotalSeconds >= timeout && courier.Status == (int)StatusCourier.Online)) { courier.Status = (int)StatusCourier.Offline; listUpdate.Add(courier); systemEventService.Add(EventMessage.CourierOffline, new Dictionary <EventMessageParam, string>() { { EventMessageParam.Courier, courier.User.LastName + " " + courier.User.FirstName + (string.IsNullOrEmpty(courier.User.MiddleName) ? "" : " " + courier.User.MiddleName) } }); } //reset request var current = DateTime.UtcNow; foreach (var courier in listCourier.Where(courier => courier.ServiceResetTime == null || (DateTime.UtcNow - courier.ServiceResetTime.GetValueOrDefault()).TotalSeconds >= 86400)) { courier.ServiceResetTime = current; courier.CurrentReq = 0; listUpdate.Add(courier); } courierService.UpdateListCourierForService(listUpdate); } catch (Exception ex) { diagnosticService.Error(ex); } }
public static void RequestStatusThread(object state) { var diagnosticService = Program.Container.Resolve <IDiagnosticService>(); var systemEventService = Program.Container.Resolve <ISystemEventService>(); var listRequestUpdate = new List <Request>(); var timeout = 600; int.TryParse(XmlConfigReader.GetValue("RequestStatusTimeoutBySecond"), out timeout); try { var requestService = Program.Container.Resolve <IRequestService>(); var requestHistoryService = Program.Container.Resolve <IRequestHistoryService>(); var listRequest = requestService.Get(p => p.Status == (int)StatusRequest.Waiting || p.Status == (int)StatusRequest.Sent); foreach (var request in listRequest.Where(request => (DateTime.UtcNow - request.SendingTime.GetValueOrDefault()).TotalSeconds >= timeout)) { request.Status = (int)StatusRequest.Abandoned; listRequestUpdate.Add(request); systemEventService.Add(EventMessage.RequestAbandoned, new Dictionary <EventMessageParam, string>() { { EventMessageParam.Request, request.RequestNo }, { EventMessageParam.Courier, request.Courier.User.LastName + " " + request.Courier.User.FirstName + (string.IsNullOrEmpty(request.Courier.User.MiddleName) ? "" : " " + request.Courier.User.MiddleName) } }); } var listRequestNotSend = requestService.Get(p => p.Status == (int)StatusRequest.NotSent); foreach (var request in listRequestNotSend.Where(request => (DateTime.UtcNow - request.SendingTime.GetValueOrDefault()).TotalSeconds >= 0)) { request.Status = (int)StatusRequest.Sent; listRequestUpdate.Add(request); systemEventService.Add(EventMessage.RequestSent, new Dictionary <EventMessageParam, string>() { { EventMessageParam.Request, request.RequestNo }, { EventMessageParam.Courier, request.Courier.User.LastName + " " + request.Courier.User.FirstName + (string.IsNullOrEmpty(request.Courier.User.MiddleName) ? "" : " " + request.Courier.User.MiddleName) } }); } var history = new List <RequestHistory>(); var listRequestExpired = requestService.Get(p => p.Status != (int)StatusRequest.Cancelled && p.Status != (int)StatusRequest.Completed && !(p.IsExpired ?? false)); foreach (var request in listRequestExpired.Where(request => (DateTime.UtcNow - request.CreatedOn.GetValueOrDefault()).TotalSeconds >= request.ExpiredTime.GetValueOrDefault(0))) { request.IsExpired = true; if (request.Courier.CurrentReq == request.Id) { request.Courier.CurrentReq = 0; } listRequestUpdate.Add(request); //add to history history.Add(new RequestHistory() { ActionType = (int)HistoryRequestActionType.Expired, Comment = "Request expired", CourierId = request.CourierId.GetValueOrDefault(), LastRequestStatus = request.Status, TimeChanged = DateTime.UtcNow, RequestId = request.Id }); systemEventService.Add(EventMessage.RequestExpired, new Dictionary <EventMessageParam, string>() { { EventMessageParam.Request, request.RequestNo }, { EventMessageParam.Courier, request.Courier.User.LastName + " " + request.Courier.User.FirstName + (string.IsNullOrEmpty(request.Courier.User.MiddleName) ? "" : " " + request.Courier.User.MiddleName) } }); } requestService.UpdateListRequestForService(listRequestUpdate); requestHistoryService.AddListRequestHistoryForWindowsService(history); } catch (Exception ex) { diagnosticService.Error(ex); } }