public void Reprioritize(WorkItemPriorityEnum priority) { if (WorkItem == null) return; if (WorkItem.Status == WorkItemStatusEnum.Deleted) return; WorkItemUpdateResponse response = null; Platform.GetService<IWorkItemService>(s => response = s.Update(new WorkItemUpdateRequest { Priority = priority, Identifier = WorkItem.Identifier, ProcessTime = priority == WorkItemPriorityEnum.Stat ? Platform.Time : default(DateTime?) })); // TODO (CR Jun 2012): The passed-in WorkItem contract should not be updated; // it should be done by the service and a new instance returned, or something should be returned by this // method to let the caller decide what to do. if (response.Item == null) WorkItem.Status = WorkItemStatusEnum.Deleted; else WorkItem = response.Item; }
public static List<WorkItem> GetWorkItems(int count, WorkItemPriorityEnum priority) { using (var query = new WorkItemQuery()) { return query.InternalGetWorkItems(count, priority); } }
public void Reprioritize(WorkItemPriorityEnum priority) { if (WorkItem == null) { return; } if (WorkItem.Status == WorkItemStatusEnum.Deleted) { return; } WorkItemUpdateResponse response = null; Platform.GetService <IWorkItemService>(s => response = s.Update(new WorkItemUpdateRequest { Priority = priority, Identifier = WorkItem.Identifier, ProcessTime = priority == WorkItemPriorityEnum.Stat ? Platform.Time : default(DateTime?) })); // TODO (CR Jun 2012): The passed-in WorkItem contract should not be updated; // it should be done by the service and a new instance returned, or something should be returned by this // method to let the caller decide what to do. if (response.Item == null) { WorkItem.Status = WorkItemStatusEnum.Deleted; } else { WorkItem = response.Item; } }
private IWorkItemProcessor InsertStudyDelete(DicomMessageBase msg, WorkItemPriorityEnum priority, WorkItemStatusEnum status) { var rq = new WorkItemInsertRequest { Request = new DeleteStudyRequest { Patient = new WorkItemPatient(msg.DataSet), Study = new WorkItemStudy(msg.DataSet), Priority = priority } }; var rsp = WorkItemService.Instance.Insert(rq); var updateRequest = new WorkItemUpdateRequest { Status = status, Identifier = rsp.Item.Identifier }; WorkItemService.Instance.Update(updateRequest); using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex)) { var broker = context.GetWorkItemBroker(); var d = new DeleteStudyItemProcessor(); d.Initialize(new WorkItemStatusProxy(broker.GetWorkItem(rsp.Item.Identifier))); return d; } }
private IWorkItemProcessor InsertImportFiles(WorkItemPriorityEnum priority, WorkItemStatusEnum status) { var rq = new WorkItemInsertRequest { Request = new ImportFilesRequest() { Priority = priority, BadFileBehaviour = BadFileBehaviourEnum.Delete, FileImportBehaviour = FileImportBehaviourEnum.Save, FilePaths = new List<string>(), } }; var rsp = WorkItemService.Instance.Insert(rq); var updateRequest = new WorkItemUpdateRequest { Status = status, Identifier = rsp.Item.Identifier }; // TODO (CR Jul 2012): Can I actually force an item to "In Progress" this way? Probably shouldn't be able to do that. WorkItemService.Instance.Update(updateRequest); using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex)) { var broker = context.GetWorkItemBroker(); var d = new ImportItemProcessor(); d.Initialize(new WorkItemStatusProxy(broker.GetWorkItem(rsp.Item.Identifier))); return d; } }
public void SendStudy(IDicomServiceNode remoteAEInfo, IStudyRootData study, WorkItemPriorityEnum priority) { EventResult result = EventResult.Success; try { var request = new DicomSendStudyRequest { DestinationServerName = remoteAEInfo.Name, Priority = priority, Study = new WorkItemStudy(study), Patient = new WorkItemPatient(study) }; InsertRequest(request, new DicomSendProgress()); } catch (Exception ex) { result = EventResult.MajorFailure; Exception = ex; Platform.Log(LogLevel.Error, ex, Common.SR.MessageFailedToSendStudy); throw; } finally { var instances = new AuditedInstances(); instances.AddInstance(study.PatientId, study.PatientsName, study.StudyInstanceUid); AuditHelper.LogBeginSendInstances(remoteAEInfo.AETitle, remoteAEInfo.ScpParameters.HostName, instances, string.IsNullOrEmpty(Request.UserName) ? EventSource.CurrentProcess : EventSource.CurrentUser, result); } }
public static List <WorkItem> GetWorkItems(int count, WorkItemPriorityEnum priority) { using (var query = new WorkItemQuery()) { return(query.InternalGetWorkItems(count, priority)); } }
public List <WorkItem> GetWorkItems(int count, WorkItemPriorityEnum priority) { _nowRunningWorkItems.Clear(); using (var context = new WorkItemContext()) { var now = DateTime.Now; var workItems = (from row in context.WorkItems where row.ScheduledTime < now && row.Priority == priority && (row.Status == WorkItemStatusEnum.Pending || row.Status == WorkItemStatusEnum.Idle) orderby row.Priority, row.ScheduledTime select row).Take(count); foreach (var item in workItems) { // 标记任务处于正在运行状态,避免任务的多次执行 item.Status = WorkItemStatusEnum.InProgress; _nowRunningWorkItems.Add(item); } return(new List <WorkItem>(_nowRunningWorkItems)); } }
/// <summary> /// Method for getting next <see cref="WorkItem"/> entry. /// </summary> /// <param name="count">The count.</param> /// <param name="priority">Search for stat items.</param> /// <remarks> /// </remarks> /// <returns> /// A <see cref="WorkItem"/> entry if found, or else null; /// </returns> private List <WorkItem> InternalGetWorkItems(int count, WorkItemPriorityEnum priority) { _nowRunningWorkItems.Clear(); _postponedWorkItems.Clear(); var itemsToPublish = new List <WorkItemData>(); try { var workItemBroker = _context.GetWorkItemBroker(); List <WorkItem> workItems = workItemBroker.GetWorkItemsForProcessing(count * 4, priority); foreach (var item in workItems) { string reason; if (CanStart(item, out reason)) { item.Status = WorkItemStatusEnum.InProgress; _nowRunningWorkItems.Add(item); } else { Postpone(item); _postponedWorkItems.Add(item); WorkItemProgress progress = item.Progress; if (progress != null) { progress.StatusDetails = reason; item.Progress = progress; itemsToPublish.Add(WorkItemDataHelper.FromWorkItem(item)); } } if (_nowRunningWorkItems.Count >= count) { break; } } _context.Commit(); return(new List <WorkItem>(_nowRunningWorkItems)); } catch (Exception x) { Platform.Log(LogLevel.Warn, x, "Unexpected error querying for {0} {1} priority WorkItems", count, priority.GetDescription()); return(null); } finally { if (itemsToPublish.Count > 0) { WorkItemPublishSubscribeHelper.PublishWorkItemsChanged(WorkItemsChangedEventType.Update, itemsToPublish); } } }
/// <summary> /// Method for getting next <see cref="WorkItem"/> entry. /// </summary> /// <param name="count">The count.</param> /// <param name="priority">Search for stat items.</param> /// <remarks> /// </remarks> /// <returns> /// A <see cref="WorkItem"/> entry if found, or else null; /// </returns> private List<WorkItem> InternalGetWorkItems(int count, WorkItemPriorityEnum priority) { _nowRunningWorkItems.Clear(); _postponedWorkItems.Clear(); var itemsToPublish = new List<WorkItemData>(); try { var workItemBroker = _context.GetWorkItemBroker(); List<WorkItem> workItems = workItemBroker.GetWorkItemsForProcessing(count * 4, priority); foreach (var item in workItems) { string reason; if (CanStart(item, out reason)) { item.Status = WorkItemStatusEnum.InProgress; _nowRunningWorkItems.Add(item); } else { Postpone(item); _postponedWorkItems.Add(item); WorkItemProgress progress = item.Progress; if (progress != null) { progress.StatusDetails = reason; item.Progress = progress; itemsToPublish.Add(WorkItemDataHelper.FromWorkItem(item)); } } if (_nowRunningWorkItems.Count >= count) break; } _context.Commit(); return new List<WorkItem>(_nowRunningWorkItems); } catch (Exception x) { Platform.Log(LogLevel.Warn, x, "Unexpected error querying for {0} {1} priority WorkItems", count, priority.GetDescription()); return null; } finally { if (itemsToPublish.Count > 0) { WorkItemPublishSubscribeHelper.PublishWorkItemsChanged(WorkItemsChangedEventType.Update, itemsToPublish); } } }
/// <summary> /// Gets the specified number of pending work items. /// </summary> /// <param name="n"></param> /// <param name="priority"> </param> /// <returns></returns> public List<WorkItem> GetWorkItemsForProcessing(int n, WorkItemPriorityEnum? priority = null) { IQueryable<WorkItem> query = from w in Context.WorkItems select w; query = WorkItemStatusFilter.WaitingToProcess.Apply(query); query = query.Where(w => w.ProcessTime < DateTime.Now); if (priority.HasValue) query = query.Where(w => w.Priority == priority.Value); query = query.OrderBy(w => w.ProcessTime); if (!priority.HasValue) query = query.OrderBy(w => w.Priority); return query.Take(n).ToList(); }
public static string GetDescription(this WorkItemPriorityEnum value) { switch (value) { case WorkItemPriorityEnum.Normal: return(SR.WorkItemPriorityEnumNormal); case WorkItemPriorityEnum.Stat: return(SR.WorkItemPriorityEnumStat); case WorkItemPriorityEnum.High: return(SR.WorkItemPriorityEnumHigh); } throw new NotImplementedException(); }
internal static WorkItem CreateWorkItem(string type, string studyInstanceUid, WorkItemPriorityEnum priority = WorkItemPriorityEnum.Normal, WorkItemStatusEnum status = WorkItemStatusEnum.Pending, int failureCount = 0, DateTime?requestedTime = null, DateTime?scheduledTime = null, DateTime?processTime = null, DateTime?expirationTime = null, DateTime?deleteTime = null, string requestData = null, string progressData = null) { var requestedTimeValue = requestedTime.GetValueOrDefault(DateTime.Now.Truncate()); var scheduledTimeValue = scheduledTime.GetValueOrDefault(requestedTimeValue); var processTimeValue = processTime.GetValueOrDefault(scheduledTimeValue.AddSeconds(10)); var expirationTimeValue = expirationTime.GetValueOrDefault(processTimeValue.AddSeconds(10)); var deleteTimeValue = deleteTime.GetValueOrDefault(expirationTimeValue.AddMinutes(10)); return(new WorkItem { Type = type, StudyInstanceUid = studyInstanceUid, Priority = priority, Status = status, FailureCount = failureCount, RequestedTime = ConstrainSqlDateTime(requestedTimeValue), ScheduledTime = ConstrainSqlDateTime(scheduledTimeValue), ProcessTime = ConstrainSqlDateTime(processTimeValue), ExpirationTime = ConstrainSqlDateTime(expirationTimeValue), DeleteTime = ConstrainSqlDateTime(deleteTimeValue), Request = new MockRequest { WorkItemType = requestData }, Progress = new MockProgress { StatusDetails = progressData } }); }
public void SendSeries(IDicomServiceNode remoteAEInfo, IStudyRootData study, string[] seriesInstanceUids, WorkItemPriorityEnum priority) { EventResult result = EventResult.Success; try { var request = new DicomSendSeriesRequest { DestinationServerName = remoteAEInfo.Name, SeriesInstanceUids = new List <string>(), Priority = priority, Study = new WorkItemStudy(study), Patient = new WorkItemPatient(study) }; request.SeriesInstanceUids.AddRange(seriesInstanceUids); InsertRequest(request, new DicomSendProgress()); } catch (Exception ex) { result = EventResult.MajorFailure; Exception = ex; throw; } finally { var instances = new AuditedInstances(); instances.AddInstance(study.PatientId, study.PatientsName, study.StudyInstanceUid); AuditHelper.LogBeginSendInstances(remoteAEInfo.AETitle, remoteAEInfo.ScpParameters.HostName, instances, string.IsNullOrEmpty(Request.UserName) ? EventSource.CurrentProcess : EventSource.CurrentUser, result); } }
private IWorkItemProcessor InsertReapplyRules(WorkItemPriorityEnum priority, WorkItemStatusEnum status) { var rq = new WorkItemInsertRequest { Request = new ReapplyRulesRequest { ApplyDeleteActions = true, ApplyRouteActions = true, Priority = priority } }; var rsp = WorkItemService.Instance.Insert(rq); var updateRequest = new WorkItemUpdateRequest { Status = status, Identifier = rsp.Item.Identifier }; WorkItemService.Instance.Update(updateRequest); using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex)) { var broker = context.GetWorkItemBroker(); var d = new ReapplyRulesItemProcessor(); d.Initialize(new WorkItemStatusProxy(broker.GetWorkItem(rsp.Item.Identifier))); return d; } }
public void SendSeries(IDicomServiceNode remoteAEInfo, IStudyRootData study, string[] seriesInstanceUids, WorkItemPriorityEnum priority) { EventResult result = EventResult.Success; try { var request = new DicomSendSeriesRequest { DestinationServerName = remoteAEInfo.Name, DestinationServerAETitle = remoteAEInfo.AETitle, DestinationServerHostname = remoteAEInfo.ScpParameters != null ? remoteAEInfo.ScpParameters.HostName : null, DestinationServerPort = remoteAEInfo.ScpParameters != null ? remoteAEInfo.ScpParameters.Port : 0, SeriesInstanceUids = new List<string>(), Priority = priority, Study = new WorkItemStudy(study), Patient = new WorkItemPatient(study) }; request.SeriesInstanceUids.AddRange(seriesInstanceUids); InsertRequest(request, new DicomSendProgress()); } catch (Exception ex) { result = EventResult.MajorFailure; Exception = ex; throw; } finally { var instances = new AuditedInstances(); instances.AddInstance(study.PatientId, study.PatientsName, study.StudyInstanceUid); AuditHelper.LogBeginSendInstances(remoteAEInfo.AETitle, remoteAEInfo.ScpParameters.HostName, instances, string.IsNullOrEmpty(Request.UserName) ? EventSource.CurrentProcess : EventSource.CurrentUser, result); } }
public void SendSops(IDicomServiceNode remoteAEInfo, IStudyRootData study, string seriesInstanceUid, string[] sopInstanceUids, WorkItemPriorityEnum priority) { EventResult result = EventResult.Success; try { var request = new DicomSendSopRequest { DestinationServerName = remoteAEInfo.Name, SeriesInstanceUid = seriesInstanceUid, SopInstanceUids = new List<string>(), Priority = priority, Study = new WorkItemStudy(study), Patient = new WorkItemPatient(study) }; request.SopInstanceUids.AddRange(sopInstanceUids); InsertRequest(request, new DicomSendProgress()); } catch (Exception ex) { result = EventResult.MajorFailure; Exception = ex; Platform.Log(LogLevel.Error, ex, Common.SR.MessageFailedToSendSops); throw; } finally { var instances = new AuditedInstances(); instances.AddInstance(study.PatientId, study.PatientsName, study.StudyInstanceUid); AuditHelper.LogBeginSendInstances(remoteAEInfo.AETitle, remoteAEInfo.ScpParameters.HostName, instances, string.IsNullOrEmpty(Request.UserName) ? EventSource.CurrentProcess : EventSource.CurrentUser, result); } }
/// <summary> /// Gets the specified number of pending work items. /// </summary> /// <param name="n"></param> /// <param name="priority"> </param> /// <returns></returns> public List<WorkItem> GetWorkItemsForProcessing(int n, WorkItemPriorityEnum? priority = null) { return priority.HasValue ? _getWorkItemsForProcessingByPriority(Context, n, Platform.Time, priority.Value).ToList() : _getWorkItemsForProcessing(Context, n, Platform.Time).ToList(); }
/// <summary> /// Get the WorkItems scheduled before <paramref name="scheduledTime"/> for <paramref name="studyInstanceUid"/> /// and/or that are a higher priority and have not yet terminated (e.g. Waiting to run, or actively running). /// </summary> /// <param name="scheduledTime">The scheduled time to get related WorkItems for.</param> /// <param name="priority">The priority of the workitem to compare with.</param> /// <param name="statusFilter">A filter for the work item status.</param> /// <param name="studyInstanceUid">The Study Instance UID to search for matching WorkItems. Can be null.</param> /// <param name="workItemTypes">The work item type(s) of the items to be returned. Can be null.</param> /// <returns></returns> public IEnumerable<WorkItem> GetWorkItemsScheduledBeforeOrHigherPriority(DateTime scheduledTime, WorkItemPriorityEnum priority, WorkItemStatusFilter statusFilter, string studyInstanceUid, params string[] workItemTypes) { IQueryable<WorkItem> query = from w in Context.WorkItems select w; query = query.Where(w => w.ScheduledTime < Platform.Time); query = query.Where(w => (w.ScheduledTime < scheduledTime && w.Priority <= priority) || w.Priority < priority); statusFilter = statusFilter ?? WorkItemStatusFilter.Active; query = statusFilter.Apply(query); if (!string.IsNullOrEmpty(studyInstanceUid)) query = query.Where(w => w.StudyInstanceUid == studyInstanceUid); if (workItemTypes != null && workItemTypes.Length > 0) { if (workItemTypes.Length == 1) query = query.Where(w => workItemTypes[0] == w.Type); else query = query.Where(w => workItemTypes.Contains(w.Type)); } query = query.OrderBy(w => w.ScheduledTime); query = query.OrderBy(w => w.Priority); return query.AsEnumerable(); }
/// <summary> /// Get the WorkItems scheduled before <paramref name="scheduledTime"/> for <paramref name="studyInstanceUid"/> /// and/or that are a higher priority and have not yet terminated (e.g. Waiting to run, or actively running). /// </summary> /// <param name="scheduledTime">The scheduled time to get related WorkItems for.</param> /// <param name="priority">The priority of the workitem to compare with.</param> /// <param name="statusFilter">A filter for the work item status.</param> /// <param name="studyInstanceUid">The Study Instance UID to search for matching WorkItems. Can be null.</param> /// <param name="concurrency">The concurrency type of the items to be returned.</param> /// <returns></returns> public IEnumerable<WorkItem> GetWorkItemsScheduledBeforeOrHigherPriority(DateTime scheduledTime, WorkItemPriorityEnum priority, WorkItemStatusFilter statusFilter, string studyInstanceUid, WorkItemConcurrency concurrency) { return GetWorkItemsScheduledBeforeOrHigherPriority(scheduledTime, priority, statusFilter, studyInstanceUid, concurrency.GetWorkItemTypes().ToArray()); }
internal static WorkItem CreateWorkItem(string type, string studyInstanceUid, WorkItemPriorityEnum priority = WorkItemPriorityEnum.Normal, WorkItemStatusEnum status = WorkItemStatusEnum.Pending, int failureCount = 0, DateTime? requestedTime = null, DateTime? scheduledTime = null, DateTime? processTime = null, DateTime? expirationTime = null, DateTime? deleteTime = null, string requestData = null, string progressData = null) { var requestedTimeValue = requestedTime.GetValueOrDefault(DateTime.Now.Truncate()); var scheduledTimeValue = scheduledTime.GetValueOrDefault(requestedTimeValue); var processTimeValue = processTime.GetValueOrDefault(scheduledTimeValue.AddSeconds(10)); var expirationTimeValue = expirationTime.GetValueOrDefault(processTimeValue.AddSeconds(10)); var deleteTimeValue = deleteTime.GetValueOrDefault(expirationTimeValue.AddMinutes(10)); return new WorkItem { Type = type, StudyInstanceUid = studyInstanceUid, Priority = priority, Status = status, FailureCount = failureCount, RequestedTime = ConstrainSqlDateTime(requestedTimeValue), ScheduledTime = ConstrainSqlDateTime(scheduledTimeValue), ProcessTime = ConstrainSqlDateTime(processTimeValue), ExpirationTime = ConstrainSqlDateTime(expirationTimeValue), DeleteTime = ConstrainSqlDateTime(deleteTimeValue), Request = new MockRequest {WorkItemType = requestData}, Progress = new MockProgress {StatusDetails = progressData} }; }
private IWorkItemProcessor InsertStudyProcess(DicomMessageBase msg, WorkItemPriorityEnum priority, WorkItemStatusEnum status, DateTime? processTime = null) { var rq = new WorkItemInsertRequest { Request = new DicomReceiveRequest { Patient = new WorkItemPatient(msg.DataSet), Study = new WorkItemStudy(msg.DataSet), SourceServerName = "TEST", Priority = priority } }; var rsp = WorkItemService.Instance.Insert(rq); var updateRequest = new WorkItemUpdateRequest { Status = status, Identifier = rsp.Item.Identifier, ProcessTime = processTime }; WorkItemService.Instance.Update(updateRequest); using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex)) { var broker = context.GetWorkItemBroker(); var d = new StudyProcessProcessor(); d.Initialize(new WorkItemStatusProxy(broker.GetWorkItem(rsp.Item.Identifier))); return d; } }
/// <summary> /// Get the WorkItems scheduled before <paramref name="scheduledTime"/> for <paramref name="studyInstanceUid"/> /// and/or that are a higher priority and have not yet terminated (e.g. Waiting to run, or actively running). /// </summary> /// <param name="scheduledTime">The scheduled time to get related WorkItems for.</param> /// <param name="priority">The priority of the workitem to compare with.</param> /// <param name="statusFilter">A filter for the work item status.</param> /// <param name="studyInstanceUid">The Study Instance UID to search for matching WorkItems. Can be null.</param> /// <param name="workItemTypes">The work item type(s) of the items to be returned. Can be null.</param> /// <returns></returns> public IEnumerable <WorkItem> GetWorkItemsScheduledBeforeOrHigherPriority(DateTime scheduledTime, WorkItemPriorityEnum priority, WorkItemStatusFilter statusFilter, string studyInstanceUid, params string[] workItemTypes) { IQueryable <WorkItem> query = from w in Context.WorkItems select w; query = query.Where(w => w.ScheduledTime < Platform.Time); query = query.Where(w => (w.ScheduledTime < scheduledTime && w.Priority <= priority) || w.Priority < priority); statusFilter = statusFilter ?? WorkItemStatusFilter.Active; query = statusFilter.Apply(query); if (!string.IsNullOrEmpty(studyInstanceUid)) { query = query.Where(w => w.StudyInstanceUid == studyInstanceUid); } if (workItemTypes != null && workItemTypes.Length > 0) { if (workItemTypes.Length == 1) { query = query.Where(w => workItemTypes[0] == w.Type); } else { query = query.Where(w => workItemTypes.Contains(w.Type)); } } query = query.OrderBy(w => w.ScheduledTime); query = query.OrderBy(w => w.Priority); return(query.AsEnumerable()); }
/// <summary> /// Get the WorkItems scheduled before <paramref name="scheduledTime"/> for <paramref name="studyInstanceUid"/> /// and/or that are a higher priority and have not yet terminated (e.g. Waiting to run, or actively running). /// </summary> /// <param name="scheduledTime">The scheduled time to get related WorkItems for.</param> /// <param name="priority">The priority of the workitem to compare with.</param> /// <param name="statusFilter">A filter for the work item status.</param> /// <param name="studyInstanceUid">The Study Instance UID to search for matching WorkItems. Can be null.</param> /// <param name="concurrency">The concurrency type of the items to be returned.</param> /// <returns></returns> public IEnumerable <WorkItem> GetWorkItemsScheduledBeforeOrHigherPriority(DateTime scheduledTime, WorkItemPriorityEnum priority, WorkItemStatusFilter statusFilter, string studyInstanceUid, WorkItemConcurrency concurrency) { return(GetWorkItemsScheduledBeforeOrHigherPriority(scheduledTime, priority, statusFilter, studyInstanceUid, concurrency.GetWorkItemTypes().ToArray())); }