Example #1
0
        private void ParallelGetPhotosFromPhotosetsProgressEvent(ParallelOperationDataWithInput <PhotosetsGetPhotosAllResult, Photoset> data)
        {
            var eta     = ((long)(data.Results.Count)).ToETA(data.InputData.Count, data.Duration);
            var message = string.Format("R:{0} I:{2} T:{1} E:{3}", data.Results.Count, data.Tasks.Count, data.InputData.Count, eta);

            if (data.JustFinishedResult != null)
            {
                message += string.Format(" FIN D:{0}", data.JustFinishedResult.Duration);  // TODO pouze debug hlaseni - nezbrazovat?
            }
            WriteProgressMsg(data.IsCompleted, message);
        }
Example #2
0
        /// <summary>
        /// zakladni sablona spusteni paralelnich akce
        ///
        /// zakladni idea spusteni vice async akci paralelne viz:
        /// "Start Multiple Async Tasks and Process Them As They Complete (C#)": https://msdn.microsoft.com/en-us/library/mt674889.aspx
        /// </summary>
        /// <typeparam name="TInput">typ input dat</typeparam>
        /// <typeparam name="TResult">typ vystupu</typeparam>
        /// <param name="inputData">vstupni data</param>
        /// <param name="singleAction">jednotliva akce jez bude spoustena paralelne</param>
        /// <param name="progressEvent">event pro reportovani</param>
        /// <param name="taskCount">kolik bude paralelnich behu</param>
        /// <returns>list TResult vysledku</returns>
        private async Task <List <TResult> > ParallelEngine <TInput, TResult>(List <TInput> inputData,
                                                                              Func <int, TInput, Task <TResult> > singleAction,
                                                                              Action <ParallelOperationDataWithInput <TResult, TInput> > progressEvent = null,
                                                                              int taskCount = DefaultParallelConnections)
            where TInput : class
            where TResult : ParallelOperationResultBase
        {
            // kopie puvodnich vstupnich dat - z fronty postupne ubiram,
            var workingQueue = inputData.ToQueue();

            // kolik paralelnich tasku budu pouzivat
            taskCount = Math.Min(workingQueue.Count, taskCount);

            // seznam tasku s http dotazy - zatim rozbehnu nekolik uvodnich dotazu a postupne po jejich jednotlivem splneni pridavam dalsi
            var taskList = Enumerable.Range(1, taskCount)
                           .Select(i => singleAction(i, workingQueue.Dequeue()))
                           .ToList();
            var taskId = taskList.Count;
            // data s vysledky procesu, tasky a vstupnimi daty - pro drzeni mezivyseldku i reportovani
            var data = new ParallelOperationDataWithInput <TResult, TInput>(GetTaskSheduler(), progressEvent, taskList, inputData);

            data.ReportProgress();

            while (taskList.Any())
            {
                var firstFinishedTask = await Task.WhenAny(taskList);   // pockam na prvni dokonceny task

                taskList.Remove(firstFinishedTask);                     // dokonceny vyhodit ze seznamu
                var finishedResult = await firstFinishedTask;           // ziskat result od dokonceneho tasku

                // pokud je jeste co, zalozim dalsi task
                if (workingQueue.Any())
                {
                    taskList.Add(singleAction(taskId++, workingQueue.Dequeue()));
                }
                data.AddResult(finishedResult);
                data.ReportProgress(finishedResult);
            }
            // finalni report
            data.SetCompletedState();
            data.ReportProgress();

            return(data.Results);
        }
Example #3
0
        private void ParallelUploadFilesReportAction(ParallelOperationDataWithInput <UploadFileResult, File4Flickr> data)
        {
            if (data.JustFinishedResult != null)
            {
                _logger.Write(string.Format("Upload file {0} of {1}, Filename:'{5}' PhotoId:{2} (R:{3} T:{4})",
                                            data.Results.Count,
                                            data.InputData.Count,
                                            data.JustFinishedResult.PhotoId,
                                            data.JustFinishedResult.RetryAttemp,
                                            data.Tasks.Count,
                                            data.JustFinishedResult.File4Flickr.RelativePathWithFileName));
            }

            if (data.IsCompleted)
            {
                var uploadFileSizeSum = data.Results.Where(p => p.File4Flickr != null).Sum(p => p.File4Flickr.Length);
                _logger.Write(string.Format("Upload finished: {0} files {1} at {2}",
                                            data.Results.Count,
                                            uploadFileSizeSum.ToFileSize(),
                                            uploadFileSizeSum.ToBitesPerSecSpeed(data.Duration)));
            }
        }
Example #4
0
 private void AddPhotosToPhotosetReportAction(ParallelOperationDataWithInput <AddPhotoToPhotosetResult, string> data)
 {
     if (data.JustFinishedResult != null)
     {
         if (data.JustFinishedResult.IsSuccessfullyCompleted)
         {
             _logger.Write(string.Format("File {0} added to photoset {1} (D:{2} T:{3})",
                                         data.JustFinishedResult.PhotoId, data.JustFinishedResult.Photoset?.PhotosetId, data.JustFinishedResult.Duration.ToDuration(),
                                         data.Tasks?.Count()));
         }
         else
         {
             _logger.Write(string.Format("AddPhotoToPhotoset error: {0} (PhotoId:{1} Photoset:{2})",
                                         data.JustFinishedResult.Exception?.Message, data.JustFinishedResult.PhotoId, data.JustFinishedResult.Photoset), VtLogState.Error);
         }
     }
     if (data.IsCompleted)
     {
         _logger.Write(string.Format("Added {0} files to photoset {1}. (D:{2})",
                                     data.Results.Count, data.Results.FirstOrDefault()?.Photoset?.PhotosetId,
                                     data.Duration.ToDuration()));
     }
 }