private async Task <bool> ProcessImageStudy(LifeImageCloudConnection Connection, ImagingStudy imagingStudy, IHttpManager httpManager, string taskInfo) { string duplicatesDirName = Connection.name; if (_profileStorage.Current.duplicatesDetectionDownload) { _duplicatesDetectionService.DuplicatesPurge(); lock (imagingStudy) { if (!_duplicatesDetectionService.DuplicatesReference1(duplicatesDirName, imagingStudy.uid)) { //studies.ImagingStudy.Remove(imagingStudy); return(false); } } } _logger.Log(LogLevel.Information, $"{taskInfo} checking study: {imagingStudy.uid} downloadStarted:{imagingStudy.downloadStarted:yyyy-MM-dd HH:mm:ss.ffff} downloadCompleted:{imagingStudy.downloadCompleted:yyyy-MM-dd HH:mm:ss.ffff} attempts: {imagingStudy.attempts} seriesOverMaxAttempts:{imagingStudy.series?.FindAll(e => e.attempts > Connection.maxAttempts).Count}"); if (await _taskManager.CountByReference(imagingStudy.uid) != 0) //not in task { _logger.Log(LogLevel.Information, $"{taskInfo} study: {imagingStudy.uid} in current tasks. Skipping."); return(false); } _logger.Log(LogLevel.Debug, $"{taskInfo} study: {imagingStudy.uid} not in current tasks."); if (imagingStudy.downloadCompleted != DateTime.MinValue) //not completed { _logger.Log(LogLevel.Information, $"{taskInfo} study: {imagingStudy.uid} completed. Skipping."); return(false); } _logger.Log(LogLevel.Debug, $"{taskInfo} study: {imagingStudy.uid} not completed."); if (imagingStudy.downloadStarted >= DateTime.Now.AddMinutes(-Connection.retryDelayMinutes)) //not attempted lately { _logger.Log(LogLevel.Information, $"{taskInfo} study: {imagingStudy.uid} attempted lately. Skipping."); return(false); } _logger.Log(LogLevel.Debug, $"{taskInfo} study: {imagingStudy.uid} not attempted lately."); if ((imagingStudy.series?.FindAll(e => e.attempts > Connection.maxAttempts).Count) != 0) //not exceeded max attempts { _logger.Log(LogLevel.Information, $"{taskInfo} study: {imagingStudy.uid} has exceeded max attempts. Skipping."); return(false); } _logger.Log(LogLevel.Debug, $"{taskInfo} study: {imagingStudy.uid} has not exceeded max attempts."); _logger.Log(LogLevel.Information, $"{taskInfo} study: {imagingStudy.uid} attempts: {imagingStudy.attempts} selected for download."); imagingStudy.downloadStarted = DateTime.Now; imagingStudy.attempts++; var newTaskID = _taskManager.NewTaskID(); Task task = new Task(new Action(async() => await _studyManager.DownloadStudy(newTaskID, imagingStudy, Connection, httpManager)), _taskManager.cts.Token); await _taskManager.Start(newTaskID, task, $"{Connection.name}.downloadStudy", $"{imagingStudy.uid}", isLongRunning : false); return(true); }
public async Task DownloadStudy(int taskID, ImagingStudy imagingStudy, LifeImageCloudConnection connection, IHttpManager httpManager) { var Connection = connection; var stopWatch = new Stopwatch(); var taskInfo = $"task: {taskID} connection: {Connection.name}"; try { stopWatch.Start(); _logger.Log(LogLevel.Debug, $"{taskInfo} downloading study: {imagingStudy.uid} downloadStarted: {imagingStudy.downloadStarted.ToString("yyyy-MM-dd HH:mm:ss.ffff")} downloadCompleted: {imagingStudy.downloadCompleted.ToString("yyyy-MM-dd HH:mm:ss.ffff")} attempts: {imagingStudy.attempts}"); foreach (var series in imagingStudy?.series) { _logger.Log(LogLevel.Debug, $"{taskInfo} checking series: {series.uid} downloadStarted: {series.downloadStarted.ToString("yyyy-MM-dd HH:mm:ss.ffff")} downloadCompleted: {series.downloadCompleted.ToString("yyyy-MM-dd HH:mm:ss.ffff")} attempts: {series.attempts}"); if (series.downloadCompleted == DateTime.MinValue) //not completed { _logger.Log(LogLevel.Debug, $"{taskInfo} series: {series.uid} not completed."); if (series.downloadStarted < DateTime.Now.AddMinutes(-Connection.retryDelayMinutes)) //not attempted lately { _logger.Log(LogLevel.Debug, $"{taskInfo} series: {series.uid} not attempted lately."); if (imagingStudy.series?.FindAll(e => e.attempts > Connection.maxAttempts).Count == 0) //not exceeded max attempts { _logger.Log(LogLevel.Debug, $"{taskInfo} series: {series.uid} not exceeded max attempts."); var url = $"{imagingStudy.url}/series/{series.uid.Substring(8)}"; //if the tasklist already contains this series don't add it again //equal is determined by the reference field only //so in this case it is the imagingStudy.url if (await _taskManager.CountByReference(url) == 0) { _logger.Log(LogLevel.Debug, $"{taskInfo} series: {series.uid} not in task list."); _logger.Log(LogLevel.Debug, $"{taskInfo} series: {series.uid} selected for download downloadStarted: {series.downloadStarted.ToString("yyyy-MM-dd HH:mm:ss.ffff")} downloadCompleted: {series.downloadCompleted.ToString("yyyy-MM-dd HH:mm:ss.ffff")} attempts: {series.attempts}"); series.downloadStarted = DateTime.Now; series.attempts++; var newTaskID = _taskManager.NewTaskID(); Task task = new Task(new Action(async() => await _studiesDownloadManager.wadoAsFileStream(connection: connection, newTaskID, httpManager: httpManager, study: imagingStudy, series: series)), _taskManager.cts.Token); await _taskManager.Start(newTaskID, task, $"{Connection.name}.Wado", url, isLongRunning : false); } else { _logger.Log(LogLevel.Debug, $"{taskInfo} series: {series.uid} in task list. Skipping."); } } else { _logger.Log(LogLevel.Debug, $"{taskInfo} series: {series.uid} exceeded max attempts. Skipping."); } } else { _logger.Log(LogLevel.Debug, $"{taskInfo} series: {series.uid} attempted lately. Skipping."); } } else { _logger.Log(LogLevel.Debug, $"{taskInfo} series: {series.uid} completed. Skipping."); } } stopWatch.Stop(); _logger.Log(LogLevel.Information, $"{taskInfo} method level elapsed: {stopWatch.Elapsed} study: {imagingStudy.uid}"); } catch (TaskCanceledException) { _logger.Log(LogLevel.Information, $"{taskInfo} Task was canceled."); } catch (Exception e) { _logger.LogFullException(e, taskInfo); } finally { _taskManager.Stop($"{Connection.name}.downloadStudy"); } }