public WorkItemInsertResponse Insert(WorkItemInsertRequest request)
        {
            // TODO (CR Jun 2012): The fact that there is special processing in here for particular types of work items
            // indicates there is something wrong with the design that may make adding custom work item types difficult.
            // Maybe the different "processors" need to perform the insert, or at least have some kind of method (rule)
            // for processing the insert?

            var response = new WorkItemInsertResponse();

            using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
            {
                DateTime now    = Platform.Time;
                var      broker = context.GetWorkItemBroker();

                if (request.Request.WorkItemType.Equals(ReindexRequest.WorkItemTypeString))
                {
                    var list = broker.GetWorkItems(request.Request.WorkItemType, null, null);
                    foreach (var workItem in list)
                    {
                        if (workItem.Status == WorkItemStatusEnum.Pending ||
                            workItem.Status == WorkItemStatusEnum.InProgress)
                        {
                            response.Item = WorkItemDataHelper.FromWorkItem(workItem);
                            return(response);
                        }
                    }
                }

                var deleteStudyRequest = request.Request as DeleteStudyRequest;
                if (deleteStudyRequest != null)
                {
                    var list = broker.GetWorkItems(request.Request.WorkItemType, null, deleteStudyRequest.Study.StudyInstanceUid);
                    foreach (var workItem in list)
                    {
                        if (workItem.Status == WorkItemStatusEnum.Pending ||
                            workItem.Status == WorkItemStatusEnum.InProgress)
                        {
                            // Mark studies to delete as "deleted" in the database.
                            var studyBroker = context.GetStudyBroker();
                            var study       = studyBroker.GetStudy(deleteStudyRequest.Study.StudyInstanceUid);
                            if (study != null)
                            {
                                study.Deleted = true;
                                context.Commit();
                            }

                            response.Item = WorkItemDataHelper.FromWorkItem(workItem);
                            return(response);
                        }
                    }
                }

                var item = new WorkItem
                {
                    Request        = request.Request,
                    Progress       = request.Progress,
                    Type           = request.Request.WorkItemType,
                    Priority       = request.Request.Priority,
                    ScheduledTime  = now.AddSeconds(WorkItemServiceSettings.Default.InsertDelaySeconds),
                    ProcessTime    = now.AddSeconds(WorkItemServiceSettings.Default.InsertDelaySeconds),
                    DeleteTime     = now.AddMinutes(WorkItemServiceSettings.Default.DeleteDelayMinutes),
                    ExpirationTime = now.AddSeconds(WorkItemServiceSettings.Default.ExpireDelaySeconds),
                    RequestedTime  = now,
                    Status         = WorkItemStatusEnum.Pending
                };

                var studyRequest = request.Request as WorkItemStudyRequest;
                if (studyRequest != null)
                {
                    item.StudyInstanceUid = studyRequest.Study.StudyInstanceUid;

                    if (request.Request.WorkItemType.Equals(DeleteStudyRequest.WorkItemTypeString))
                    {
                        // Mark studies to delete as "deleted" in the database.
                        var studyBroker = context.GetStudyBroker();
                        var study       = studyBroker.GetStudy(studyRequest.Study.StudyInstanceUid);
                        if (study != null)
                        {
                            study.Deleted = true;
                        }
                    }
                }

                broker.AddWorkItem(item);

                context.Commit();

                response.Item = WorkItemDataHelper.FromWorkItem(item);
            }

            // Cache the UserIdentityContext for later use by the shred
            if (request.Request.WorkItemType.Equals(ImportFilesRequest.WorkItemTypeString))
            {
                UserIdentityCache.Put(response.Item.Identifier, UserIdentityContext.CreateFromCurrentThreadPrincipal());
            }

            WorkItemPublishSubscribeHelper.PublishWorkItemChanged(WorkItemsChangedEventType.Update, response.Item);
            if (WorkItemProcessor.Instance != null)
            {
                WorkItemProcessor.Instance.SignalThread();
            }

            return(response);
        }
Exemplo n.º 2
0
        public override void Process()
        {
            Progress.TotalFilesToImport     = 0;
            Progress.NumberOfFilesImported  = 0;
            Progress.NumberOfImportFailures = 0;
            Progress.PathsImported          = 0;
            Progress.PathsToImport          = 0;
            Progress.CompletedEnumeration   = null;

            Progress.StatusDetails = Request.FilePaths.Count > 1
                                       ? String.Format(SR.FormatMultipleFilesDescription, Request.FilePaths[0])
                                       : Request.FilePaths[0];
            Proxy.UpdateProgress();

            if (CancelPending)
            {
                Proxy.Cancel();
                return;
            }
            if (StopPending)
            {
                Proxy.Postpone();
                return;
            }

            bool fatalError = false;

            //it's ok to read this property unsynchronized because this is the only thread that is adding to the queue for the particular job.
            if (Request.FilePaths.Count == 0)
            {
                Progress.StatusDetails = SR.MessageNoFilesToImport;
                Progress.IsCancelable  = false;
            }
            else
            {
                using (UserIdentityCache.Get(Proxy.Item.Oid).Impersonate())
                {
                    string failureReason;
                    if (!ValidateRequest(Request, out failureReason))
                    {
                        Proxy.Fail(failureReason, WorkItemFailureType.Fatal);
                        return;
                    }

                    fatalError = ImportFiles(Request.FilePaths, Request.FileExtensions, Request.Recursive);
                }

                GC.Collect();
            }

            if (CancelPending)
            {
                Proxy.Cancel();
            }
            else if (StopPending)
            {
                Proxy.Postpone();
            }
            else if (fatalError || Progress.NumberOfImportFailures > 0)
            {
                Proxy.Fail(string.Format(SR.ImportFailedPartialStudies, Progress.StatusDetails), WorkItemFailureType.Fatal);
            }
            else
            {
                Proxy.Complete();
            }
        }
        public WorkItemUpdateResponse Update(WorkItemUpdateRequest request)
        {
            var response = new WorkItemUpdateResponse();

            using (var context = new DataAccessContext(DataAccessContext.WorkItemMutex))
            {
                var broker   = context.GetWorkItemBroker();
                var workItem = broker.GetWorkItem(request.Identifier);
                if (workItem == null)
                {
                    response.Item = null;
                    return(response);
                }

                bool deleted = false;

                if (request.Delete.HasValue && request.Delete.Value)
                {
                    if (workItem.Status != WorkItemStatusEnum.InProgress)
                    {
                        workItem.Status = WorkItemStatusEnum.Deleted;
                        deleted         = true;

                        // If StudyDelete we're removing, "undelete" the study
                        CheckDeleteStudyCanceled(context, workItem);
                    }
                }
                if (!deleted)
                {
                    if (request.ExpirationTime.HasValue)
                    {
                        workItem.ExpirationTime = request.ExpirationTime.Value;
                    }
                    if (request.Priority.HasValue)
                    {
                        workItem.Priority = request.Priority.Value;
                    }
                    if (request.Status.HasValue && workItem.Status != WorkItemStatusEnum.InProgress)
                    {
                        workItem.Status = request.Status.Value;
                        if (request.Status.Value == WorkItemStatusEnum.Canceled)
                        {
                            workItem.DeleteTime = Platform.Time.AddMinutes(WorkItemServiceSettings.Default.DeleteDelayMinutes);
                        }
                        else if (request.Status.Value == WorkItemStatusEnum.Pending)
                        {
                            workItem.ScheduledTime = Platform.Time;
                            workItem.FailureCount  = 0;
                        }

                        // Cache the UserIdentityContext for later use by the shred
                        if (workItem.Request.WorkItemType.Equals(ImportFilesRequest.WorkItemTypeString) && request.Status.Value == WorkItemStatusEnum.Pending)
                        {
                            UserIdentityCache.Put(workItem.Oid, UserIdentityContext.CreateFromCurrentThreadPrincipal());
                        }
                    }
                    if (request.ProcessTime.HasValue)
                    {
                        workItem.ProcessTime = request.ProcessTime.Value;
                    }

                    if (request.Cancel.HasValue && request.Cancel.Value)
                    {
                        if (workItem.Progress == null || workItem.Progress.IsCancelable)
                        {
                            if (workItem.Status.Equals(WorkItemStatusEnum.Idle) ||
                                workItem.Status.Equals(WorkItemStatusEnum.Pending))
                            {
                                workItem.Status = WorkItemStatusEnum.Canceled;

                                // If StudyDelete we're removing, "undelete" the study
                                CheckDeleteStudyCanceled(context, workItem);
                            }
                            else if (workItem.Status.Equals(WorkItemStatusEnum.InProgress))
                            {
                                // Abort the WorkItem
                                WorkItemProcessor.Instance.Cancel(workItem.Oid);
                            }
                        }
                    }
                }

                context.Commit();

                response.Item = WorkItemDataHelper.FromWorkItem(workItem);
            }

            WorkItemPublishSubscribeHelper.PublishWorkItemChanged(WorkItemsChangedEventType.Update, response.Item);

            return(response);
        }