Exemplo n.º 1
0
        public void itShouldFailIfDeleteFails()
        {
            var fileSysCommand = new MockFileSystemCommand();
            var fakeFileSystem = new FakeFileSystem(fileSysCommand);

            fileSysCommand.FileDeleteFunc = (filename) => {
                throw new ApplicationException("Exception raised by MockFileSystemCommand during FileDelete");
            };
            var source = @"c:\dummysourcefile.txt";

            fakeFileSystem.AddFiles(source);
            var fileDeleteCommand = new FileDeleteCommand(@"c:\dummybackupdir", fileSysCommand, source);
            var reports           = new List <CommandReport>();

            fileDeleteCommand.OnReportSent += (command, args) => {
                reports.Add(new CommandReport {
                    Reporter   = command,
                    Message    = args.Message,
                    ReportType = args.ReportType
                });
            };

            fileDeleteCommand.Do();
            Assert.IsFalse(fileDeleteCommand.DidCommandSucceed);
            Assert.IsTrue(reports.Any(r => r.ReportType == ReportType.DoneTaskWithFailure &&
                                      r.Reporter.Id == fileDeleteCommand.Id));
        }
Exemplo n.º 2
0
        private void ProcessUid(WorkQueueUid uid)
        {
            Platform.CheckForNullReference(uid, "uid");

            string imagePath = GetUidPath(uid);

            using (ServerCommandProcessor processor = new ServerCommandProcessor(String.Format("Deleting {0}", uid.SopInstanceUid)))
            {
                // If the file for some reason doesn't exist, we just ignore it
                if (File.Exists(imagePath))
                {
                    Platform.Log(ServerPlatform.InstanceLogLevel, "Deleting {0}", imagePath);
                    FileDeleteCommand deleteFile = new FileDeleteCommand(imagePath, true);
                    processor.AddCommand(deleteFile);
                }
                else
                {
                    Platform.Log(LogLevel.Warn, "WARNING {0} is missing.", imagePath);
                }

                DeleteWorkQueueUidCommand deleteUid = new DeleteWorkQueueUidCommand(uid);
                processor.AddCommand(deleteUid);
                if (!processor.Execute())
                {
                    throw new Exception(String.Format("Unable to delete image {0}", uid.SopInstanceUid));
                }
            }
        }
        protected override void OnExecute(CommandProcessor theProcessor)
        {
            Platform.CheckForNullReference(Context, "Context");
            Platform.CheckForNullReference(Context.ReconcileWorkQueueData, "Context.ReconcileWorkQueueData");

            foreach (WorkQueueUid uid in Context.WorkQueueUidList)
            {
                string imagePath = GetReconcileUidPath(uid);

                try
                {
                    using (var processor = new ServerCommandProcessor(String.Format("Deleting {0}", uid.SopInstanceUid)))
                    {
                        var deleteFile = new FileDeleteCommand(imagePath, true);
                        var deleteUid  = new DeleteWorkQueueUidCommand(uid);
                        processor.AddCommand(deleteFile);
                        processor.AddCommand(deleteUid);
                        Platform.Log(ServerPlatform.InstanceLogLevel, deleteFile.ToString());
                        if (!processor.Execute())
                        {
                            throw new Exception(String.Format("Unable to discard image {0}", uid.SopInstanceUid));
                        }
                    }
                }
                catch (Exception e)
                {
                    Platform.Log(LogLevel.Error, e, "Unexpected exception discarding file: {0}", imagePath);
                    SopInstanceProcessor.FailUid(uid, true);
                }
            }
        }
Exemplo n.º 4
0
        public async Task HandleAsync(FileDeleteCommand command)
        {
            var sidebarContents = _context.SidebarContentFile.Where(x => x.FileId == command.Id);

            _context.SidebarContentFile.RemoveRange(sidebarContents);

            var file = await _context.File.Where(x => x.Id == command.Id).FirstOrDefaultAsync();

            _context.File.Remove(file);

            await _context.SaveChangesAsync();
        }
Exemplo n.º 5
0
        public void itShouldDeleteBackupFileOnCleanup()
        {
            var fileSysCommand = new MockFileSystemCommand();
            var fakeFileSystem = new FakeFileSystem(fileSysCommand);

            var fileToDelete = @"c:\dummysourcefile.txt";

            fakeFileSystem.AddFiles(fileToDelete);
            var fileDeleteCommand = new FileDeleteCommand(@"c:\dummybackupdir", fileSysCommand, fileToDelete);

            fileDeleteCommand.Do();
            fileDeleteCommand.Cleanup();

            Assert.IsFalse(fakeFileSystem.FileExists(fileDeleteCommand.BackupFilename));
        }
Exemplo n.º 6
0
        public void itShouldRestoreBackupOnUndo()
        {
            var fileSysCommand = new MockFileSystemCommand();
            var fakeFileSystem = new FakeFileSystem(fileSysCommand);
            var fileToDelete   = @"c:\dummysourcefile.txt";

            fakeFileSystem.AddFiles(fileToDelete);
            var fileDeleteCommand = new FileDeleteCommand(@"c:\dummybackupdir", fileSysCommand, fileToDelete);

            fileDeleteCommand.Do();
            Assert.IsFalse(fakeFileSystem.FileExists(fileToDelete));

            fileDeleteCommand.Undo();
            Assert.IsTrue(fakeFileSystem.FileExists(fileToDelete));
        }
Exemplo n.º 7
0
        public void itShouldDeleteMultipleFiles()
        {
            var fileSysCommand = new MockFileSystemCommand();
            var fakeFileSystem = new FakeFileSystem(fileSysCommand);
            var fileToDelete1  = @"c:\dummysourcefile1.txt";
            var fileToDelete2  = @"c:\dummysourcefile2.txt";
            var fileToDelete3  = @"c:\dummysourcefile3.txt";

            fakeFileSystem.AddFiles(fileToDelete1, fileToDelete2, fileToDelete3);
            Assert.IsTrue(fakeFileSystem.FileExists(fileToDelete1));
            Assert.IsTrue(fakeFileSystem.FileExists(fileToDelete2));
            Assert.IsTrue(fakeFileSystem.FileExists(fileToDelete3));

            var fileDeleteCommand = new FileDeleteCommand(@"c:\dummybackupdir", fileSysCommand, fileToDelete1, fileToDelete2, fileToDelete3);

            fileDeleteCommand.Do();

            Assert.IsFalse(fakeFileSystem.FileExists(fileToDelete1));
            Assert.IsFalse(fakeFileSystem.FileExists(fileToDelete2));
            Assert.IsFalse(fakeFileSystem.FileExists(fileToDelete3));
        }
Exemplo n.º 8
0
        public void itShouldSucceedIfSourceFileDoesNotExist()
        {
            var fileSysCommand    = new MockFileSystemCommand();
            var fakeFileSystem    = new FakeFileSystem(fileSysCommand);
            var fileToDelete      = @"c:\dummysourcefile.txt";
            var fileDeleteCommand = new FileDeleteCommand(@"c:\dummybackupdir", fileSysCommand, fileToDelete);
            var reports           = new List <CommandReport>();

            fileDeleteCommand.OnReportSent += (command, args) => {
                reports.Add(new CommandReport {
                    Reporter   = command,
                    Message    = args.Message,
                    ReportType = args.ReportType
                });
            };

            fileDeleteCommand.Do();
            Assert.IsTrue(fileDeleteCommand.DidCommandSucceed);
            Assert.IsTrue(reports.Any(r => r.ReportType == ReportType.DoneTaskWithSuccess &&
                                      r.Reporter.Id == fileDeleteCommand.Id));
        }
Exemplo n.º 9
0
        /// <summary>
        /// Removes all WorkQueueUids from the database and delete the corresponding DICOM files from the filesystem.
        /// </summary>
        private void ProcessWorkQueueUids()
        {
            if (Study == null)
            {
                Platform.Log(LogLevel.Info, "Begin StudyProcess Cleanup (Study has not been created): Attempt #{0}. {1} unprocessed files will be removed",
                             WorkQueueItem.FailureCount + 1,
                             WorkQueueUidList.Count);
            }
            else
            {
                Platform.Log(LogLevel.Info,
                             "Begin StudyProcess Cleanup for study {0},  Patient {1} (PatientId:{2} A#:{3}) on Partition {4}. Attempt #{5}. {6} unprocessed files will be removed",
                             Study.StudyInstanceUid, Study.PatientsName, Study.PatientId,
                             Study.AccessionNumber, ServerPartition.Description,
                             WorkQueueItem.FailureCount + 1,
                             WorkQueueUidList.Count
                             );
            }



            foreach (WorkQueueUid sop in WorkQueueUidList)
            {
                string path = GetFileStoredPath(sop);

                Platform.Log(LogLevel.Info, "Cleaning up {0}", path);

                using (ServerCommandProcessor processor = new ServerCommandProcessor(String.Format("Deleting {0}", sop.SopInstanceUid)))
                {
                    // delete the file
                    FileDeleteCommand deleteFile = new FileDeleteCommand(path, true);
                    processor.AddCommand(deleteFile);

                    // delete the WorkQueueUID from the database
                    DeleteWorkQueueUidCommand deleteUid = new DeleteWorkQueueUidCommand(sop);
                    processor.AddCommand(deleteUid);

                    try
                    {
                        // delete the directory (if empty)
                        var fileInfo = new FileInfo(path);
                        ClearCanvas.ImageServer.Core.Command.DeleteDirectoryCommand deleteDir = new ClearCanvas.ImageServer.Core.Command.DeleteDirectoryCommand(fileInfo.Directory.FullName, false, true);
                        processor.AddCommand(deleteDir);
                    }
                    catch (DirectoryNotFoundException ex)
                    {
                        // ignore
                    }

                    if (!processor.Execute())
                    {
                        throw new Exception(String.Format("Unable to delete SOP {0}", sop.SopInstanceUid), processor.FailureException);
                    }
                }

                // Delete the base directory if it's empty
                var baseDir = GetBaseDirectory(sop);
                if (Directory.Exists(baseDir))
                {
                    using (ServerCommandProcessor processor = new ServerCommandProcessor(String.Format("Deleting {0}", sop.SopInstanceUid)))
                    {
                        ClearCanvas.ImageServer.Core.Command.DeleteDirectoryCommand deleteDir = new ClearCanvas.ImageServer.Core.Command.DeleteDirectoryCommand(baseDir, false, true);
                        processor.AddCommand(deleteDir);

                        if (!processor.Execute())
                        {
                            throw new Exception(String.Format("Unable to delete {0}", baseDir), processor.FailureException);
                        }
                    }
                }
            }
        }
Exemplo n.º 10
0
        private void ProcessInstanceLevelDelete(Model.WorkQueue item)
        {
            // ensure the Study is loaded.
            Study study = StorageLocation.Study;

            Platform.CheckForNullReference(study, "Study record doesn't exist");

            Platform.Log(LogLevel.Info, "Processing Instance Level Deletion for Study {0}, A#: {1}",
                         study.StudyInstanceUid, study.AccessionNumber);

            bool completed = false;

            try
            {
                // Load the list of Sop Instances to be deleted from the WorkQueueUid
                LoadUids(item);

                // Go through the list of series and add commands
                // to delete each of them. It's all or nothing.
                using (var processor = new ServerCommandProcessor(String.Format("Deleting Series from study {0}, A#:{1}, Patient: {2}, ID:{3}", study.StudyInstanceUid, study.AccessionNumber, study.PatientsName, study.PatientId)))
                {
                    StudyXml studyXml = StorageLocation.LoadStudyXml();
                    IDictionary <string, Series> existingSeries = StorageLocation.Study.Series;


                    // Add commands to delete the folders and update the xml
                    foreach (WorkQueueUid uid in WorkQueueUidList)
                    {
                        // Delete from study XML
                        if (studyXml.Contains(uid.SeriesInstanceUid, uid.SopInstanceUid))
                        {
                            //Note: DeleteDirectoryCommand  doesn't throw exception if the folder doesn't exist
                            var xmlUpdate = new RemoveInstanceFromStudyXmlCommand(StorageLocation, studyXml, uid.SeriesInstanceUid, uid.SopInstanceUid);
                            processor.AddCommand(xmlUpdate);
                        }

                        // Delete from filesystem
                        string path = StorageLocation.GetSopInstancePath(uid.SeriesInstanceUid, uid.SopInstanceUid);
                        if (File.Exists(path))
                        {
                            var delDir = new FileDeleteCommand(path, true);
                            processor.AddCommand(delDir);
                        }
                    }

                    // flush the updated xml to disk
                    processor.AddCommand(new SaveXmlCommand(studyXml, StorageLocation));


                    // Update the db.. NOTE: these commands are executed at the end.
                    foreach (WorkQueueUid uid in WorkQueueUidList)
                    {
                        // Delete from DB
                        if (studyXml.Contains(uid.SeriesInstanceUid, uid.SopInstanceUid))
                        {
                            var delInstance = new UpdateInstanceCountCommand(StorageLocation, uid.SeriesInstanceUid, uid.SopInstanceUid);
                            processor.AddCommand(delInstance);
                            delInstance.Executing += DeleteSeriesFromDbExecuting;
                        }
                        else
                        {
                            // SOP doesn't exist
                            Platform.Log(LogLevel.Info, "SOP {0} is invalid or no longer exists", uid.SopInstanceUid);
                        }

                        // The WorkQueueUid must be cleared before the entry can be removed from the queue
                        var deleteUid = new DeleteWorkQueueUidCommand(uid);
                        processor.AddCommand(deleteUid);

                        // Force a re-archival if necessary
                        processor.AddCommand(new InsertArchiveQueueCommand(item.ServerPartitionKey, item.StudyStorageKey));
                    }

                    if (!processor.Execute())
                    {
                        throw new ApplicationException(
                                  String.Format("Error occurred when series from Study {0}, A#: {1}",
                                                study.StudyInstanceUid, study.AccessionNumber), processor.FailureException);
                    }
                }

                completed = true;
            }
            finally
            {
                if (completed)
                {
                    OnCompleted();
                    PostProcessing(item, WorkQueueProcessorStatus.Complete, WorkQueueProcessorDatabaseUpdate.ResetQueueState);
                }
                else
                {
                    PostProcessing(item, WorkQueueProcessorStatus.Pending, WorkQueueProcessorDatabaseUpdate.None);
                }
            }
        }