예제 #1
0
        public Task <IUpdateVersionHandle> SearchVersion()
        {
            _logger.LogTrace("Starting searching new version was triggerd. Waiting other operation finished");
            _semaphore.Wait();
            _logger.LogTrace("Lock accuired. Starting searching task");

            return(Task.Factory.StartNew(() =>
            {
                _logger.LogInformation("Starting searching new version");
                try
                {
                    IUpdateVersionHandle handle = new UpdateVersionHandle(null, this);
                    var versions = _versionSource.LoadAvailableVersions();

                    if (versions.Any())
                    {
                        _logger.LogDebug("Available version: {0}", string.Join(Environment.NewLine, versions.Select(x => x.ToString())));
                        var currentVersion = _currentVersionDeterminer.DetermineCurrentVersionNumber();
                        var latestVersion = versions.OrderByDescending(x => x.VersionNumber).First();

                        if (currentVersion < latestVersion.VersionNumber)
                        {
                            _logger.LogInformation("New version found. Latest [{0}] Current [{1}]", latestVersion, currentVersion);
                            handle = new UpdateVersionHandle(latestVersion, this);

                            foreach (var userInteraction in _userInteractions)
                            {
                                userInteraction.NewVersionAvailable(handle);
                            }
                        }
                        else
                        {
                            _logger.LogInformation("No new version found. Latest [{0}] Current [{1}]", latestVersion, currentVersion);
                        }
                    }
                    else
                    {
                        _logger.LogInformation("No versions found");
                    }

                    return handle;
                }
                catch (Exception ex)
                {
                    _logger.LogError(ex, "Error during searching new version occurred");
                    throw;
                }
                finally
                {
                    _logger.LogTrace("Releasing lock. Searching new version");
                    _semaphore.Release();
                }
            }));
        }
예제 #2
0
        internal void UpdateVersion(UpdateVersionHandle handle)
        {
            if (!handle.HasNewVersion)
            {
                return;
            }

            _logger.LogTrace("Starting update to version [{0}]. Waiting other operation finished", handle.NewVersion);
            _semaphore.Wait();
            _logger.LogTrace("Lock accuired. Starting update");

            try
            {
                _logger.LogInformation("Update to version [{0}] started", handle.NewVersion);

                var updateFolder = new DirectoryInfo(Path.Combine(Path.GetTempPath(), "AutoUpdate.Net", Guid.NewGuid().ToString()));
                _logger.LogDebug("Creating working folder '{0}'", updateFolder.FullName);
                updateFolder.Create();

                var artifactsFolder = new DirectoryInfo(Path.Combine(updateFolder.FullName, "Artifacts"));
                _logger.LogDebug("Creating artifacts folder '{0}'", artifactsFolder.FullName);
                artifactsFolder.Create();

                var workspace             = new UpdatePreparationWorkspaceInformation(handle.NewVersion, updateFolder, artifactsFolder);
                var executorConfiguration = new ExecutorConfiguration();

                var downloader = handle.NewVersion.Source.Accept(new DownloaderFactory());
                downloader.Download(workspace);

                executorConfiguration.Steps = _prepareSteps.SelectMany(x => x.Prepare(workspace))
                                              .ToArray();

                var curProcess = Process.GetCurrentProcess();
                executorConfiguration.Application.Path             = curProcess.MainModule.FileName;
                executorConfiguration.Application.RestartArguments = Environment.CommandLine;
                executorConfiguration.Application.CallingProcessId = Process.GetCurrentProcess().Id;

                _logger.LogDebug("Copy update executor application to working folder");
                CopyAndStartExecutor(workspace, executorConfiguration);

                _logger.LogDebug("Executor started. Closing current application");
                _applicationCloser.CloseApplication();
            }
            finally
            {
                _logger.LogTrace("Releasing lock. Updating");
                _semaphore.Release();
            }
        }