/// <summary>
        /// Checks for local file changes, compares their versions to the ones on the server and synchronizes them if required.
        /// </summary>
        public override async Task <SynchronizationResult> SynchronizeAsync()
        {
            var result = new SynchronizationResult();

            var remote = await _api.GetRemoteFilesAsync(_app);

            var diffs = IOExtensions.Diff(remote, _fileManager.AsFileInfoCollection());

            _fileManager.Cleanup(diffs.Where(x => x.DifferenceType == IODifference.IODifferenceType.Removal));
            var toBeDownloaded = diffs
                                 .Where(x => x.Type == IODifference.ObjectType.File && (x.DifferenceType == IODifference.IODifferenceType.Addition || x.DifferenceType == IODifference.IODifferenceType.Update));

            if (toBeDownloaded.Count() != 0)
            {
                _fileManager.Cleanup(diffs.Where(x => x.DifferenceType == IODifference.IODifferenceType.Removal));
                //Pre-deployment actions
                var cmd = new CommandLine(_app.Location);
                _app.PredeployActions?.ToList().ForEach(action =>
                {
                    cmd.Invoke(action);
                });

                result = new SynchronizationResult();
                LogToConsole("Download started");

                var chunks = toBeDownloaded.ChunkBy(MAX_UPLOAD_COUNT); //Do multiple downloads at once instead of one at a time

                foreach (var chunk in chunks)
                {
                    var uploadTasks = chunk.Select(x => Task.Run(async() =>
                    {
                        try
                        {
                            LogToConsole($"Downloading {x.Path}...");
                            var bytes = await _api.DownloadFileBytesAsync(_app, x.Path);
                            _fileManager.WriteFileBytes(x.Path, bytes);
                        }
                        catch (Exception e)
                        {
                            LogToConsole($"Error downloading file {x.Path} ({e.GetType().ToString()})");
                        }
                    }));
                    await Task.WhenAll(uploadTasks);
                }

                //Post-Deployment actions
                _app.PostdeployActions?.ToList().ForEach(action =>
                {
                    cmd.Invoke(action);
                });
                cmd.Close();
                LogToConsole("Download completed");
            }
            else
            {
                LogToConsole("Up to date");
            }
            return(result);
        }
        /// <summary>
        /// Checks for local file changes, compares their versions to the ones on the server and synchronizes them if required.
        /// </summary>
        public override async Task <SynchronizationResult> SynchronizeAsync()
        {
            SynchronizationResult result = null;

            var remote = await _api.GetRemoteFilesAsync(_app);

            var diffs = IOExtensions.Diff(_fileManager.AsFileInfoCollection(), remote);
            await _api.DoCleanupAsync(_app, diffs.Where(x => x.DifferenceType == IODifference.IODifferenceType.Removal));

            var toBeUploaded = diffs.Where(x => x.DifferenceType == IODifference.IODifferenceType.Addition);

            if (toBeUploaded.Count() != 0)
            {
                result = new SynchronizationResult();
                LogToConsole("Upload started");

                await _api.LockApplicationAsync(_app);

                var chunks = toBeUploaded.ChunkBy(MAX_UPLOAD_COUNT); //Do multiple uploads at once instead of one at a time
                foreach (var chunk in chunks)
                {
                    var uploadTasks = chunk.Select(x => Task.Run(async() =>
                    {
                        try
                        {
                            LogToConsole($"Uploading {x.Path}...");
                            var res = await _api.UploadFileIfNotExistsAsync(_app, x.Path.Replace(_fileManager.BaseLocation, string.Empty), x.Checksum, _fileManager.GetFileBytes(x.Path));
                            if (res != "Exists")
                            {
                                result.NewFiles++;
                            }
                        }
                        catch (Exception e)
                        {
                            LogToConsole($"Error uploading file {x.Path} ({e.GetType().ToString()})");
                        }
                    }));
                    await Task.WhenAll(uploadTasks);
                }
                await _api.UnlockApplicationAsync(_app);

                LogToConsole("Upload completed");
            }
            else
            {
                LogToConsole("Up to date");
            }
            return(result);
        }
Beispiel #3
0
        /// <summary>
        /// Does the initial synchronization for all registered applications.
        /// </summary>
        public async Task <SynchronizationResult> SynchronizeAsync()
        {
            var result = new SynchronizationResult();

            foreach (var deployer in _applicationSynchronizers)
            {
                var res = await deployer.SynchronizeAsync();

                if (res != null)
                {
                    result += res;
                }
            }
            return(result);
        }