private bool CanReadZip(string zipFile, RestoreQueue queueItem) { // Do a test read of the zip file. If it succeeds, the file is available, if it // fails, we just set back to pending and recheck. try { FileStream stream = File.OpenRead(zipFile); // Read a byte, just in case that makes a difference. stream.ReadByte(); stream.Close(); stream.Dispose(); return(true); } catch (DirectoryNotFoundException ex) { Platform.Log(LogLevel.Error, ex, "Archive {0} for Study {1} cannot be found, failing", zipFile, _studyStorage == null ? (_location == null ? string.Empty : _location.StudyInstanceUid) : _studyStorage.StudyInstanceUid); // Just "Fail", the directory is not found. queueItem.FailureDescription = string.Format("Directory not found for file, cannot restore: {0}", zipFile); _hsmArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Failed, Platform.Time); return(false); } catch (Exception ex) { DateTime scheduledTime = Platform.Time.AddSeconds(HsmSettings.Default.ReadFailRescheduleDelaySeconds); Platform.Log(LogLevel.Error, ex, "Archive {0} for Study {1} is unreadable, rescheduling restore to {2}", zipFile, _studyStorage == null ? (_location == null ? string.Empty : _location.StudyInstanceUid) : _studyStorage.StudyInstanceUid, scheduledTime); // Just reschedule in "Restoring" state, the file is unreadable. _hsmArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Restoring, scheduledTime); return(false); } }
/// <summary> /// Run the service. /// </summary> protected override void Run() { while (true) { if ((_threadPool.QueueCount + _threadPool.ActiveCount) < _threadPool.Concurrency) { try { RestoreQueue queueItem = _hsmArchive.GetRestoreCandidate(); if (queueItem != null) { HsmStudyRestore archiver = new HsmStudyRestore(_hsmArchive); _threadPool.Enqueue(queueItem, archiver.Run); } else if (CheckStop(HsmSettings.Default.PollDelayMilliseconds)) { Platform.Log(LogLevel.Info, "Shutting down {0} restore service.", _hsmArchive.PartitionArchive.Description); return; } } catch (Exception e) { Platform.Log(LogLevel.Error, e, "Unexpected exception when querying for restore candidates. Rescheduling."); if (CheckStop(HsmSettings.Default.PollDelayMilliseconds)) { Platform.Log(LogLevel.Info, "Shutting down {0} restore service.", _hsmArchive.PartitionArchive.Description); return; } } } else { if (CheckStop(HsmSettings.Default.PollDelayMilliseconds)) { Platform.Log(LogLevel.Info, "Shutting down {0} restore service.", _hsmArchive.PartitionArchive.Description); return; } } } }
public void Insert(Guid Guid, Guid ArchiveStudyStorageGUID, Guid StudyStorageGUID, DateTime ScheduledTime, short RestoreQueueStatusEnum, string ProcessorId, string FailureDescription) { var item = new RestoreQueue(); item.Guid = Guid; item.ArchiveStudyStorageGUID = ArchiveStudyStorageGUID; item.StudyStorageGUID = StudyStorageGUID; item.ScheduledTime = ScheduledTime; item.RestoreQueueStatusEnum = RestoreQueueStatusEnum; item.ProcessorId = ProcessorId; item.FailureDescription = FailureDescription; item.Save(UserName); }
/// <summary> /// Update a <see cref="RestoreQueue"/> entry. /// </summary> /// <param name="item">The item to update.</param> /// <param name="status">The status to set the entry to.</param> /// <param name="scheduledTime">The scheduled time to set the entry to.</param> /// <param name="updateContext">The update context</param> public bool UpdateRestoreQueue(IUpdateContext updateContext, RestoreQueue item, RestoreQueueStatusEnum status, DateTime scheduledTime) { UpdateRestoreQueueParameters parms = new UpdateRestoreQueueParameters(); parms.RestoreQueueKey = item.GetKey(); parms.RestoreQueueStatusEnum = status; parms.ScheduledTime = scheduledTime; parms.StudyStorageKey = item.StudyStorageKey; if (!String.IsNullOrEmpty(item.FailureDescription)) { parms.FailureDescription = item.FailureDescription; } IUpdateRestoreQueue broker = updateContext.GetBroker <IUpdateRestoreQueue>(); if (broker.Execute(parms)) { return(true); } Platform.Log(LogLevel.Error, "Unexpected failure updating RestoreQueue entry {0}", item.GetKey()); return(false); }
private IList <RestoreQueue> InternalSelect(int startRowIndex, int maximumRows, out int resultCount) { resultCount = 0; if (maximumRows == 0) { return(new List <RestoreQueue>()); } if (SearchKeys != null) { IList <RestoreQueue> archiveQueueList = new List <RestoreQueue>(); foreach (ServerEntityKey key in SearchKeys) { archiveQueueList.Add(RestoreQueue.Load(key)); } resultCount = archiveQueueList.Count; return(archiveQueueList); } WebQueryRestoreQueueParameters parameters = new WebQueryRestoreQueueParameters(); parameters.StartIndex = startRowIndex; parameters.MaxRowCount = maximumRows; if (Partition != null) { parameters.ServerPartitionKey = Partition.Key; } if (!string.IsNullOrEmpty(PatientId)) { string key = PatientId.Replace("*", "%"); key = key.Replace("?", "_"); parameters.PatientId = key; } if (!string.IsNullOrEmpty(PatientName)) { string key = PatientName.Replace("*", "%"); key = key.Replace("?", "_"); parameters.PatientsName = key; } if (String.IsNullOrEmpty(ScheduledDate)) { parameters.ScheduledTime = null; } else { parameters.ScheduledTime = DateTime.ParseExact(ScheduledDate, DateFormats, null); } if (StatusEnum != null) { parameters.RestoreQueueStatusEnum = StatusEnum; } List <string> groupOIDs = new List <string>(); CustomPrincipal user = Thread.CurrentPrincipal as CustomPrincipal; if (user != null) { if (!user.IsInRole(MatrixPACS.Enterprise.Common.AuthorityTokens.DataAccess.AllStudies)) { foreach (var oid in user.Credentials.DataAccessAuthorityGroups) { groupOIDs.Add(oid.ToString()); } parameters.CheckDataAccess = true; parameters.UserAuthorityGroupGUIDs = StringUtilities.Combine(groupOIDs, ","); } } IList <RestoreQueue> list = _searchController.FindRestoreQueue(parameters); resultCount = parameters.ResultCount; return(list); }
/// <summary> /// Creates an instance of <see cref="RestoreProcessorContext"/> /// </summary> /// <param name="item"></param> public RestoreProcessorContext(RestoreQueue item) : base(item.GetKey().Key.ToString()) { Platform.CheckForNullReference(item, "item"); _item = item; }
/// <summary> /// Do the restore. /// </summary> /// <param name="queueItem">The queue item to restore.</param> public void Run(RestoreQueue queueItem) { using (var context = new RestoreProcessorContext(queueItem)) { try { // Load up related classes. using (IReadContext readContext = _hsmArchive.PersistentStore.OpenReadContext()) { _archiveStudyStorage = ArchiveStudyStorage.Load(readContext, queueItem.ArchiveStudyStorageKey); _serverSyntax = ServerTransferSyntax.Load(readContext, _archiveStudyStorage.ServerTransferSyntaxKey); _syntax = TransferSyntax.GetTransferSyntax(_serverSyntax.Uid); var parms = new StudyStorageLocationQueryParameters { StudyStorageKey = queueItem.StudyStorageKey }; var broker = readContext.GetBroker <IQueryStudyStorageLocation>(); _location = broker.FindOne(parms); if (_location == null) { _studyStorage = StudyStorage.Load(readContext, queueItem.StudyStorageKey); if (_studyStorage == null) { DateTime scheduleTime = Platform.Time.AddMinutes(5); Platform.Log(LogLevel.Error, "Unable to find storage location, rescheduling restore request to {0}", scheduleTime); queueItem.FailureDescription = "Unable to find storage location, rescheduling request."; _hsmArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Pending, scheduleTime); return; } } } if (_location == null) { Platform.Log(LogLevel.Info, "Starting restore of nearline study: {0}", _studyStorage.StudyInstanceUid); // Get the zip file path from the xml data in the ArchiveStudyStorage entry // Also store the "StudyFolder" for use below string studyFolder; string zipFile = GetZipFileName(out studyFolder); // Do a test read of the zip file. If it succeeds, the file is available, if it // fails, we just set back to pending and recheck. if (!CanReadZip(zipFile, queueItem)) { return; } RestoreNearlineStudy(queueItem, zipFile, studyFolder); } else { Platform.Log(LogLevel.Info, "Starting restore of online study: {0}", _location.StudyInstanceUid); // Get the zip file path from the xml data in the ArchiveStudyStorage entry // Also store the "StudyFolder" for use below string studyFolder; string zipFile = GetZipFileName(out studyFolder); // Do a test read of the zip file. If it succeeds, the file is available, if it // fails, we just set back to pending and recheck. if (!CanReadZip(zipFile, queueItem)) { return; } RestoreOnlineStudy(queueItem, zipFile, _location.GetStudyPath()); } } catch (Exception e) { Platform.Log(LogLevel.Error, e, "Unexpected exception processing restore request for {0} on archive {1}", _studyStorage == null ? (_location == null ? string.Empty : _location.StudyInstanceUid) : _studyStorage.StudyInstanceUid, _hsmArchive.PartitionArchive.Description); queueItem.FailureDescription = e.Message; _hsmArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Failed, Platform.Time); } } }
private void RestoreOnlineStudy(RestoreQueue queueItem, string zipFile, string destinationFolder) { try { using (var processor = new ServerCommandProcessor("HSM Restore Online Study")) { var zipService = Platform.GetService <IZipService>(); using (var zipWriter = zipService.OpenWrite(zipFile)) { foreach (string file in zipWriter.EntryFileNames) { processor.AddCommand(new ExtractZipFileAndReplaceCommand(zipFile, file, destinationFolder)); } } // We rebuild the StudyXml, in case any settings or issues have happened since archival processor.AddCommand(new RebuildStudyXmlCommand(_location.StudyInstanceUid, destinationFolder)); StudyStatusEnum status; if (_syntax.Encapsulated && _syntax.LosslessCompressed) { status = StudyStatusEnum.OnlineLossless; } else if (_syntax.Encapsulated && _syntax.LossyCompressed) { status = StudyStatusEnum.OnlineLossy; } else { status = StudyStatusEnum.Online; } processor.AddCommand(new UpdateStudyStateCommand(_location, status, _serverSyntax)); // Apply the rules engine. var context = new ServerActionContext(null, _location.FilesystemKey, _hsmArchive.ServerPartition, queueItem.StudyStorageKey) { CommandProcessor = processor }; processor.AddCommand( new ApplyRulesCommand(destinationFolder, _location.StudyInstanceUid, context)); if (!processor.Execute()) { Platform.Log(LogLevel.Error, "Unexpected error processing restore request for {0} on archive {1}", _location.StudyInstanceUid, _hsmArchive.PartitionArchive.Description); queueItem.FailureDescription = processor.FailureReason; _hsmArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Failed, Platform.Time); } else { // Unlock the Queue Entry and set to complete using (IUpdateContext update = PersistentStoreRegistry.GetDefaultStore().OpenUpdateContext(UpdateContextSyncMode.Flush)) { _hsmArchive.UpdateRestoreQueue(update, queueItem, RestoreQueueStatusEnum.Completed, Platform.Time.AddSeconds(60)); var studyLock = update.GetBroker <ILockStudy>(); var parms = new LockStudyParameters { StudyStorageKey = queueItem.StudyStorageKey, QueueStudyStateEnum = QueueStudyStateEnum.Idle }; bool retVal = studyLock.Execute(parms); if (!parms.Successful || !retVal) { Platform.Log(LogLevel.Info, "Study {0} on partition {1} failed to unlock.", _location.StudyInstanceUid, _hsmArchive.ServerPartition.Description); } update.Commit(); Platform.Log(LogLevel.Info, "Successfully restored study: {0} on archive {1}", _location.StudyInstanceUid, _hsmArchive.PartitionArchive.Description); _location = ReloadStorageLocation(); OnStudyRestored(_location); } } } } catch (StudyIntegrityValidationFailure ex) { // study has been restored but it seems corrupted. Need to reprocess it. ReprocessStudy(_location, ex.Message); } catch (Exception e) { Platform.Log(LogLevel.Error, e, "Unexpected exception processing restore request for {0} on archive {1}", _location.StudyInstanceUid, _hsmArchive.PartitionArchive.Description); queueItem.FailureDescription = e.Message; _hsmArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Failed, Platform.Time); } }
public void RestoreNearlineStudy(RestoreQueue queueItem, string zipFile, string studyFolder) { ServerFilesystemInfo fs = _hsmArchive.Selector.SelectFilesystem(); if (fs == null) { DateTime scheduleTime = Platform.Time.AddMinutes(5); Platform.Log(LogLevel.Error, "No writeable filesystem for restore, rescheduling restore request to {0}", scheduleTime); queueItem.FailureDescription = "No writeable filesystem for restore, rescheduling restore request"; _hsmArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Pending, scheduleTime); return; } string destinationFolder = Path.Combine(fs.Filesystem.FilesystemPath, _hsmArchive.ServerPartition.PartitionFolder); StudyStorageLocation restoredLocation = null; try { using (var processor = new ServerCommandProcessor("HSM Restore Offline Study")) { processor.AddCommand(new CreateDirectoryCommand(destinationFolder)); destinationFolder = Path.Combine(destinationFolder, studyFolder); processor.AddCommand(new CreateDirectoryCommand(destinationFolder)); destinationFolder = Path.Combine(destinationFolder, _studyStorage.StudyInstanceUid); processor.AddCommand(new CreateDirectoryCommand(destinationFolder)); processor.AddCommand(new ExtractZipCommand(zipFile, destinationFolder)); // We rebuild the StudyXml, in case any settings or issues have happened since archival processor.AddCommand(new RebuildStudyXmlCommand(_studyStorage.StudyInstanceUid, destinationFolder)); // Apply the rules engine. var context = new ServerActionContext(null, fs.Filesystem.GetKey(), _hsmArchive.ServerPartition, queueItem.StudyStorageKey) { CommandProcessor = processor }; processor.AddCommand( new ApplyRulesCommand(destinationFolder, _studyStorage.StudyInstanceUid, context)); // Do the actual insert into the DB var insertStorageCommand = new InsertFilesystemStudyStorageCommand( _hsmArchive.PartitionArchive.ServerPartitionKey, _studyStorage.StudyInstanceUid, studyFolder, fs.Filesystem.GetKey(), _syntax); processor.AddCommand(insertStorageCommand); if (!processor.Execute()) { Platform.Log(LogLevel.Error, "Unexpected error processing restore request for {0} on archive {1}", _studyStorage.StudyInstanceUid, _hsmArchive.PartitionArchive.Description); queueItem.FailureDescription = processor.FailureReason; _hsmArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Failed, Platform.Time); } else { restoredLocation = insertStorageCommand.Location; // Unlock the Queue Entry using ( IUpdateContext update = PersistentStoreRegistry.GetDefaultStore().OpenUpdateContext(UpdateContextSyncMode.Flush)) { bool retVal = _hsmArchive.UpdateRestoreQueue(update, queueItem, RestoreQueueStatusEnum.Completed, Platform.Time.AddSeconds(60)); var studyLock = update.GetBroker <ILockStudy>(); var parms = new LockStudyParameters { StudyStorageKey = queueItem.StudyStorageKey, QueueStudyStateEnum = QueueStudyStateEnum.Idle }; retVal = retVal && studyLock.Execute(parms); if (!parms.Successful || !retVal) { string message = String.Format("Study {0} on partition {1} failed to unlock.", _studyStorage.StudyInstanceUid, _hsmArchive.ServerPartition.Description); Platform.Log(LogLevel.Info, message); throw new ApplicationException(message); } update.Commit(); Platform.Log(LogLevel.Info, "Successfully restored study: {0} on archive {1}", _studyStorage.StudyInstanceUid, _hsmArchive.PartitionArchive.Description); OnStudyRestored(restoredLocation); } } } } catch (StudyIntegrityValidationFailure ex) { Debug.Assert(restoredLocation != null); // study has been restored but it seems corrupted. Need to reprocess it. ReprocessStudy(restoredLocation, ex.Message); } catch (Exception e) { Platform.Log(LogLevel.Error, e, "Unexpected exception processing restore request for {0} on archive {1}", _studyStorage.StudyInstanceUid, _hsmArchive.PartitionArchive.Description); _hsmArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Failed, Platform.Time); } }
/// <summary> /// Do the restore. /// </summary> /// <param name="queueItem">The queue item to restore.</param> public void Run(RestoreQueue queueItem) { using (RestoreProcessorContext context = new RestoreProcessorContext(queueItem)) { try { // Load up related classes. using (IReadContext readContext = _nasArchive.PersistentStore.OpenReadContext()) { _archiveStudyStorage = ArchiveStudyStorage.Load(readContext, queueItem.ArchiveStudyStorageKey); _serverSyntax = ServerTransferSyntax.Load(readContext, _archiveStudyStorage.ServerTransferSyntaxKey); _syntax = TransferSyntax.GetTransferSyntax(_serverSyntax.Uid); StudyStorageLocationQueryParameters parms = new StudyStorageLocationQueryParameters { StudyStorageKey = queueItem.StudyStorageKey }; IQueryStudyStorageLocation broker = readContext.GetBroker <IQueryStudyStorageLocation>(); _location = broker.FindOne(parms); if (_location == null) { _studyStorage = StudyStorage.Load(readContext, queueItem.StudyStorageKey); if (_studyStorage == null) { DateTime scheduleTime = Platform.Time.AddMinutes(5); Platform.Log(LogLevel.Error, "Unable to find storage location, rescheduling restore request to {0}", scheduleTime); queueItem.FailureDescription = "Unable to find storage location, rescheduling request."; _nasArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Pending, scheduleTime); return; } } } if (_location == null) { Platform.Log(LogLevel.Info, "Starting restore of nearline study: {0}", _studyStorage.StudyInstanceUid); } else { Platform.Log(LogLevel.Info, "Starting restore of online study: {0}", _location.StudyInstanceUid); } // If restoring a Nearline study, select a filesystem string destinationFolder; if (_location == null) { ServerFilesystemInfo fs = _nasArchive.Selector.SelectFilesystem(); if (fs == null) { DateTime scheduleTime = Platform.Time.AddMinutes(5); Platform.Log(LogLevel.Error, "No writeable filesystem for restore, rescheduling restore request to {0}", scheduleTime); queueItem.FailureDescription = "No writeable filesystem for restore, rescheduling request."; _nasArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Pending, scheduleTime); return; } destinationFolder = Path.Combine(fs.Filesystem.FilesystemPath, _nasArchive.ServerPartition.PartitionFolder); } else { destinationFolder = _location.GetStudyPath(); } // Get the zip file path from the xml data in the ArchiveStudyStorage entry // Also store the "StudyFolder" for use below string studyFolder = String.Empty; string filename = String.Empty; string studyInstanceUid = String.Empty; XmlElement element = _archiveStudyStorage.ArchiveXml.DocumentElement; if (element != null) { foreach (XmlElement node in element.ChildNodes) { if (node.Name.Equals("StudyFolder")) { studyFolder = node.InnerText; } else if (node.Name.Equals("Filename")) { filename = node.InnerText; } else if (node.Name.Equals("Uid")) { studyInstanceUid = node.InnerText; } } } string zipFile = Path.Combine(_nasArchive.NasPath, studyFolder); zipFile = Path.Combine(zipFile, studyInstanceUid); zipFile = Path.Combine(zipFile, filename); // Do a test read of the zip file. If it succeeds, the file is available, if it // fails, we just set back to pending and recheck. try { FileStream stream = File.OpenRead(zipFile); // Read a byte, just in case that makes a difference. stream.ReadByte(); stream.Close(); stream.Dispose(); } catch (Exception ex) { DateTime scheduledTime = Platform.Time.AddSeconds(NasSettings.Default.ReadFailRescheduleDelaySeconds); Platform.Log(LogLevel.Error, ex, "Archive {0} for Study {1} is unreadable, rescheduling restore to {2}", zipFile, _studyStorage == null ? (_location == null ? string.Empty : _location.StudyInstanceUid) : _studyStorage.StudyInstanceUid, scheduledTime); // Just reschedule in "Restoring" state, the file is unreadable. _nasArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Restoring, scheduledTime); return; } if (_location == null) { RestoreNearlineStudy(queueItem, zipFile, destinationFolder, studyFolder); } else { RestoreOnlineStudy(queueItem, zipFile, destinationFolder); } } catch (Exception e) { Platform.Log(LogLevel.Error, e, "Unexpected exception processing restore request for {0} on archive {1}", _studyStorage == null ? (_location == null ? string.Empty : _location.StudyInstanceUid) : _studyStorage.StudyInstanceUid, _nasArchive.PartitionArchive.Description); queueItem.FailureDescription = e.Message; _nasArchive.UpdateRestoreQueue(queueItem, RestoreQueueStatusEnum.Failed, Platform.Time); } } }