Пример #1
0
        public async Task <AvalancheRunResult> Run()
        {
            // This needs to be created or else all of the upload attempts will fail.
            // It's the container that the archives live in.
            using (var glacier = _glacierFactory.CreateWrapper())
            {
                await glacier.Item.AssertVaultExists(_parameters.Glacier.VaultName);
            }

            // In order to support upload parallelism, we're going to place
            // all of the work to be done in a Queue, then launch workers
            // which will pull from it. There's probably a more sophisticated
            // way to do this with stringing Tasks together, but the complexity
            // and number of edge cases are pretty intense. Having some longer-
            // running Tasks is way more foolproof.
            ConcurrentQueue <PictureModel> workQueue;
            Guid catalogId;

            using (var lightroom = _lightroomFactory.CreateWrapper())
                using (var avalanche = _avalancheFactory.CreateWrapper())
                {
                    var filteredPictures = lightroom.Item.GetAllPictures()
                                           // A Picture can be in multiple Catalogs, but
                                           // only needs to be backed up a single time
                                           .GroupBy(a => a.FileId)
                                           .Select(a => a.First())
                                           .Where(a => a.LibraryCount > 0
                                                  // Make sure we don't double archive
                                                  && !avalanche.Item.FileIsArchived(a.FileId));
                    workQueue = new ConcurrentQueue <PictureModel>(filteredPictures);

                    catalogId = lightroom.Item.GetCatalogId();
                }


            _logger.LogInformation("Backing up {0} images", workQueue.Count);

            var runState = new RunState
            {
                Index = 0,
                FilteredPictureCount = workQueue.Count,
                CatalogId            = catalogId,
                Result    = new AvalancheRunResult(),
                WorkQueue = workQueue
            };

            // Start up some long-running Tasks which will pull the
            // archive work from the Queue
            var workers = Enumerable.Range(0, UploadParallelism)
                          .Select(a => ArchiveWorker(runState));
            await Task.WhenAll(workers);

            _logger.LogInformation("Done");
            return(runState.Result);
        }