AzureWebFarm.Services.SyncService.PackageSitesToLocal C# (CSharp) Method

PackageSitesToLocal() public method

Packages sites that are in IIS but not in local temp storage. There are new sites that have been deployed to this instance using Web Deploy.
public PackageSitesToLocal ( ) : void
return void
        public void PackageSitesToLocal()
        {
            _logger.DebugFormat("[IIS => Local Storage] - Site deploy times: {0}", string.Join(",", _siteDeployTimes.Select(t => t.Key + " - " + t.Value).ToArray()));

            using (var serverManager = new ServerManager())
            {
                foreach (var site in serverManager.Sites.Where(s => !_sitesToExclude.Contains(s.Name)))
                {
                    var siteName = site.Name.Replace("-", ".").ToLowerInvariant();

                    if (!site.Name.Equals(AzureRoleEnvironment.RoleWebsiteName(), StringComparison.OrdinalIgnoreCase))
                    {
                        var sitePath = Path.Combine(_localSitesPath, siteName);
                        var siteLastModifiedTime = GetFolderLastModifiedTimeUtc(sitePath);

                        if (!_siteDeployTimes.ContainsKey(siteName))
                        {
                            _siteDeployTimes.Add(siteName, siteLastModifiedTime);
                        }

                        _logger.DebugFormat("[IIS => Local Storage] - Site last modified time: '{0}'", siteLastModifiedTime);

                        // If the site has been modified since the last deploy, but not within the last {SyncWait}s (otherwise it might be mid-sync)
                        if (_siteDeployTimes[siteName] < siteLastModifiedTime && siteLastModifiedTime < DateTime.UtcNow.AddSeconds(-SyncWait))
                        {
                            // Update status to deployed
                            UpdateSyncStatus(siteName, SyncInstanceStatus.Deployed);

                            // Ensure the temp path exists
                            var tempSitePath = Path.Combine(_localTempPath, siteName);
                            if (!Directory.Exists(tempSitePath))
                            {
                                Directory.CreateDirectory(tempSitePath);
                            }

                            // Create a package of the site and move it to local temp sites
                            var packageFile = Path.Combine(tempSitePath, siteName + ".zip");
                            _logger.InfoFormat("[IIS => Local Storage] - Creating a package of the site '{0}' and moving it to local temp sites '{1}'", siteName, packageFile);
                            try
                            {
                                using (var deploymentObject = DeploymentManager.CreateObject(DeploymentWellKnownProvider.DirPath, sitePath))
                                {
                                    deploymentObject.SyncTo(DeploymentWellKnownProvider.Package, packageFile, new DeploymentBaseOptions(), new DeploymentSyncOptions());
                                }

                                _logger.DebugFormat(string.Format("Calling OnSiteUpdated event for {0}...", siteName));
                                OnSiteUpdated(siteName);
                                _siteDeployTimes[siteName] = DateTime.UtcNow;
                            }
                            catch (Exception ex)
                            {
                                UpdateSyncStatus(siteName, SyncInstanceStatus.Error, ex);
                                throw;
                            }
                        }
                    }
                }
            }
        }