Esempio n. 1
0
        public IEnumerable <uSyncAction> ImportAll(string folder, HandlerSettings config, bool force, SyncUpdateCallback callback = null)
        {
            var sw = Stopwatch.StartNew();

            logger.Debug(handlerType, "{alias} ImportAll: {fileName}", this.Alias, Path.GetFileName(folder));

            var actions = new List <uSyncAction>();
            var updates = new Dictionary <string, TObject>();

            runtimeCache.ClearByKey($"keycache_{this.Alias}");

            actions.AddRange(ImportFolder(folder, config, updates, force, callback));

            if (updates.Any())
            {
                ProcessSecondPasses(updates, actions, config, callback);
            }

            runtimeCache.ClearByKey($"keycache_{this.Alias}");
            callback?.Invoke("Done", 3, 3);

            sw.Stop();
            logger.Debug(handlerType, "{alias} Import Complete {elapsedMilliseconds}ms", this.Alias, sw.ElapsedMilliseconds);
            return(actions);
        }
        private bool RunOnThisServer()
        {
            // WARNING - we shouldn't be using Current !
            var role = Current.ServerRegistrar?.GetCurrentServerRole();

            if (role == null)
            {
                return(true);
            }

            switch (role)
            {
            case ServerRole.Replica:
                logger.Debug <CustomBackgroundTask>("Not running on a replica");
                return(false);

            case ServerRole.Unknown:
                logger.Debug <CustomBackgroundTask>("Not running on a unknown server role");
                return(false);

            // case ServerRole.Master:
            // case ServerRole.Single:
            default:
                logger.Debug <CustomBackgroundTask>("Running on master or single");
                return(true);
            }
        }
Esempio n. 3
0
        public override async Task <bool> PerformRunAsync(CancellationToken token)
        {
            switch (_runtime.ServerRole)
            {
            case ServerRole.Replica:
                _logger.Debug <ScheduledTasks>("Does not run on replica servers.");
                return(true);    // DO repeat, server role can change

            case ServerRole.Unknown:
                _logger.Debug <ScheduledTasks>("Does not run on servers with unknown role.");
                return(true);    // DO repeat, server role can change
            }

            // ensure we do not run if not main domain, but do NOT lock it
            if (_runtime.IsMainDom == false)
            {
                _logger.Debug <ScheduledTasks>("Does not run if not MainDom.");
                return(false); // do NOT repeat, going down
            }

            using (_logger.DebugDuration <ScheduledTasks>("Scheduled tasks executing", "Scheduled tasks complete"))
            {
                try
                {
                    await ProcessTasksAsync(token);
                }
                catch (Exception ex)
                {
                    _logger.Error <ScheduledTasks>(ex, "Error executing scheduled task");
                }
            }

            return(true); // repeat
        }
Esempio n. 4
0
        public override bool PerformRun()
        {
            switch (_runtime.ServerRole)
            {
            case ServerRole.Replica:
                _logger.Debug <LogScrubber>("Does not run on replica servers.");
                return(true);    // DO repeat, server role can change

            case ServerRole.Unknown:
                _logger.Debug <LogScrubber>("Does not run on servers with unknown role.");
                return(true);    // DO repeat, server role can change
            }

            // ensure we do not run if not main domain, but do NOT lock it
            if (_runtime.IsMainDom == false)
            {
                _logger.Debug <LogScrubber>("Does not run if not MainDom.");
                return(false); // do NOT repeat, going down
            }

            // running on a background task, and Log.CleanLogs uses the old SqlHelper,
            // better wrap in a scope and ensure it's all cleaned up and nothing leaks
            using (var scope = _scopeProvider.CreateScope())
                using (_logger.DebugDuration <LogScrubber>("Log scrubbing executing", "Log scrubbing complete"))
                {
                    _auditService.CleanLogs(GetLogScrubbingMaximumAge(_settings));
                    scope.Complete();
                }

            return(true); // repeat
        }
Esempio n. 5
0
        public override bool PerformRun()
        {
            switch (_runtime.ServerRole)
            {
            case ServerRole.Replica:
                _logger.Debug <LogScrubber>("Does not run on replica servers.");
                return(true);    // DO repeat, server role can change

            case ServerRole.Unknown:
                _logger.Debug <LogScrubber>("Does not run on servers with unknown role.");
                return(true);    // DO repeat, server role can change
            }

            // ensure we do not run if not main domain, but do NOT lock it
            if (_runtime.IsMainDom == false)
            {
                _logger.Debug <LogScrubber>("Does not run if not MainDom.");
                return(false); // do NOT repeat, going down
            }

            // Ensure we use an explicit scope since we are running on a background thread.
            using (var scope = _scopeProvider.CreateScope())
                using (_logger.DebugDuration <LogScrubber>("Log scrubbing executing", "Log scrubbing complete"))
                {
                    _auditService.CleanLogs(GetLogScrubbingMaximumAge(_settings));
                    scope.Complete();
                }

            return(true); // repeat
        }
Esempio n. 6
0
        public override bool PerformRun()
        {
            // Do not run the code on replicas nor unknown role servers
            // ONLY run for Master server or Single
            switch (_runtime.ServerRole)
            {
            case ServerRole.Replica:
                _logger.Debug <CleanRoom>("Does not run on replica servers.");
                return(true);    // We return true to try again as the server role may change!

            case ServerRole.Unknown:
                _logger.Debug <CleanRoom>("Does not run on servers with unknown role.");
                return(true);    // We return true to try again as the server role may change!
            }

            if (_contentService.RecycleBinSmells())
            {
                // Take out the trash
                using (_logger.TraceDuration <CleanRoom>("Mum, I am emptying out the bin", "Its all clean now!"))
                {
                    _contentService.EmptyRecycleBin(userId: -1);
                }
            }

            // If we want to keep repeating - we need to return true
            // But if we run into a problem/error & want to stop repeating - return false
            return(true);
        }
Esempio n. 7
0
        // internal/virtual for tests (i.e. hack, do not port to netcore)
        internal virtual void DetermineRuntimeLevel(IUmbracoDatabaseFactory databaseFactory, IProfilingLogger profilingLogger)
        {
            using (var timer = profilingLogger.DebugDuration <CoreRuntime>("Determining runtime level.", "Determined."))
            {
                try
                {
                    _state.DetermineRuntimeLevel(databaseFactory);

                    profilingLogger.Debug <CoreRuntime, RuntimeLevel, RuntimeLevelReason>("Runtime level: {RuntimeLevel} - {RuntimeLevelReason}", _state.Level, _state.Reason);

                    if (_state.Level == RuntimeLevel.Upgrade)
                    {
                        profilingLogger.Debug <CoreRuntime>("Configure database factory for upgrades.");
                        databaseFactory.ConfigureForUpgrade();
                    }
                }
                catch
                {
                    _state.Level  = RuntimeLevel.BootFailed;
                    _state.Reason = RuntimeLevelReason.BootFailedOnException;
                    timer?.Fail();
                    throw;
                }
            }
        }
        public void Initialize()
        {
            // double check - is this on in config
            // are we in upgrade mode (we should be - we only register in upgrade)
            if (!SilentUpgradeOn || runtimeState.Level != RuntimeLevel.Upgrade)
            {
                return;
            }

            // Do we need to lock? the upgrade so it only happens once?
            logger.Debug <SilentUpgradeComponent>("Silently upgrading the Site");

            // The 'current' steps for upgrading Umbraco

            try
            {
                //
                // If these steps change in the core then this will be wrong.
                //
                // We don't run the file permission step, we assume you have that sorted already.
                // and besides its internal and we can't get to the tests
                //      FilePermissionHelper.RunFilePermissionTestSuite(out result);

                // Step: 'DatabaseInstallStep'
                var result = databaseBuilder.CreateSchemaAndData();
                if (!result.Success)
                {
                    // failed.
                    throw new Exception("Upgrade Failed - Create Schema");
                }

                // Step: 'DatabaseUpgradeStep'
                var plan = new UmbracoPlan();
                logger.Debug <SilentUpgradeComponent>("Running Migrations {initialState} to {finalState}", plan.InitialState, plan.FinalState);

                var upgrade = databaseBuilder.UpgradeSchemaAndData(plan);
                if (!upgrade.Success)
                {
                    throw new Exception("Upgrade Failed - Upgrade Schema");
                }

                // Step: 'SetUmbracoVersionStep'

                // Update the version number inside the web.config
                logger.Debug <SilentUpgradeComponent>("Updating version in the web.config {version}", UmbracoVersion.SemanticVersion.ToSemanticString());
                // Doing this essentially restats the site.
                globalSettings.ConfigurationStatus = UmbracoVersion.SemanticVersion.ToSemanticString();

                // put something in the log.
                logger.Info <SilentUpgradeComponent>("Silent Upgrade Completed {version}", UmbracoVersion.SemanticVersion.ToSemanticString());

                Upgraded = true;
            }
            catch (Exception ex)
            {
                logger.Warn <SilentUpgradeComponent>(ex, "Silent Upgrade Failed");
                Upgraded = false; // if this is false, we should fall through to the 'standard' upgrade path.
            }
        }
Esempio n. 9
0
        public IEnumerable <StatusWithMessage> PurgePages(IEnumerable <string> urls)
        {
            //If the setting is turned off, then don't do anything.
            if (!configuration.PurgeCacheOn)
            {
                return new List <StatusWithMessage>()
                       {
                           new StatusWithMessage(false, CloudflareMessages.CLOULDFLARE_DISABLED)
                       }
            }
            ;

            urls = domainManager.FilterToAllowedDomains(urls);

            //Separate all of these into individual groups where the domain is the same that way we save some cloudflare requests.
            IEnumerable <IGrouping <string, string> > groupings = urls.GroupBy(url => UrlHelper.GetDomainFromUrl(url, true));

            List <StatusWithMessage> results = new List <StatusWithMessage>();

            //Now loop through each group.
            foreach (IGrouping <string, string> domainUrlGroup in groupings)
            {
                //get the domain without the scheme or port.
                Uri domain = new UriBuilder(domainUrlGroup.Key).Uri;

                Zone websiteZone = GetZone(domain.DnsSafeHost);

                if (websiteZone == null)
                {
                    string errorMessage = $"Could not retrieve the zone from cloudflare with the domain(url) of {domain}";

                    logger.Error <CloudflareManager>(errorMessage);
                    //this will already be logged in the GetZone method so just relay that it was bad.
                    results.Add(new StatusWithMessage(false, errorMessage));
                    continue; //to the next domain group.
                }

                //Make the request to the api using the urls from this domain group.
                bool apiResult = this.cloudflareService.PurgeCache(websiteZone.Id, domainUrlGroup);

                if (!apiResult)
                {
                    logger.Error <CloudflareManager>(CloudflareMessages.CLOUDFLARE_API_ERROR);
                    results.Add(new StatusWithMessage(false, CloudflareMessages.CLOUDFLARE_API_ERROR));
                }
                else
                {
                    foreach (string url in domainUrlGroup)
                    {
                        logger.Debug <CloudflareManager>($"Purged for url {url}");
                        //We need to  add x number of statuswithmessages that are true where x is the number urls
                        results.Add(new StatusWithMessage(true, String.Format("Purged for url {0}", url)));
                    }
                }
            }
            //return the results of all of the api calls.
            return(results);
        }
Esempio n. 10
0
        private void InitBackOffice()
        {
            var sw = Stopwatch.StartNew();

            try
            {
                using (var reference = umbracoContextFactory.EnsureUmbracoContext())
                {
                    if (uSyncSettings.ExportAtStartup || (uSyncSettings.ExportOnSave && !syncFileService.RootExists(uSyncSettings.RootFolder)))
                    {
                        logger.Info <uSyncBackofficeComponent>("uSync: Running Export at startup");
                        uSyncService.Export(uSyncSettings.RootFolder, default(SyncHandlerOptions));
                    }

                    if (uSyncSettings.ImportAtStartup)
                    {
                        logger.Info <uSyncBackofficeComponent>("uSync: Running Import at startup");

                        if (!HasStopFile(uSyncSettings.RootFolder))
                        {
                            uSyncService.Import(uSyncSettings.RootFolder, false, new SyncHandlerOptions
                            {
                                Group = uSyncSettings.ImportAtStartupGroup
                            });

                            ProcessOnceFile(uSyncSettings.RootFolder);
                        }
                        else
                        {
                            logger.Info <uSyncBackofficeComponent>("Startup Import blocked by usync.stop file");
                        }
                    }

                    if (uSyncSettings.ExportOnSave)
                    {
                        var handlers = handlerFactory
                                       .GetValidHandlers(new SyncHandlerOptions(handlerFactory.DefaultSet, HandlerActions.Save))
                                       .ToList();

                        logger.Info <uSyncBackofficeComponent>("uSync: Initializing events for {count} Handlers", handlers.Count);

                        foreach (var syncHandler in handlers)
                        {
                            logger.Debug <uSyncBackofficeComponent>($"  Initializing up Handler {syncHandler.Handler.Name}");
                            syncHandler.Handler.Initialize(syncHandler.Settings);
                        }
                    }
                }

                sw.Stop();
                logger.Info <uSyncBackofficeComponent>("uSync: Startup Processes Complete {ElapsedMilliseconds}ms", sw.ElapsedMilliseconds);
            }
            catch (Exception ex)
            {
                logger.Warn <uSyncBackofficeComponent>($"uSync: Error during at startup {ex.Message}");
            }
        }
Esempio n. 11
0
        public override async Task <bool> PerformRunAsync(CancellationToken token)
        {
            if (_runtimeState.Level != RuntimeLevel.Run)
            {
                return(true); // repeat...
            }
            switch (_runtimeState.ServerRole)
            {
            case ServerRole.Replica:
                _logger.Debug <HealthCheckNotifier>("Does not run on replica servers.");
                return(true);    // DO repeat, server role can change

            case ServerRole.Unknown:
                _logger.Debug <HealthCheckNotifier>("Does not run on servers with unknown role.");
                return(true);    // DO repeat, server role can change
            }

            // ensure we do not run if not main domain, but do NOT lock it
            if (_runtimeState.IsMainDom == false)
            {
                _logger.Debug <HealthCheckNotifier>("Does not run if not MainDom.");
                return(false); // do NOT repeat, going down
            }

            // Ensure we use an explicit scope since we are running on a background thread and plugin health
            // checks can be making service/database calls so we want to ensure the CallContext/Ambient scope
            // isn't used since that can be problematic.
            using (var scope = _scopeProvider.CreateScope())
                using (_logger.DebugDuration <HealthCheckNotifier>("Health checks executing", "Health checks complete"))
                {
                    var healthCheckConfig = Current.Configs.HealthChecks();

                    // Don't notify for any checks that are disabled, nor for any disabled
                    // just for notifications
                    var disabledCheckIds = healthCheckConfig.NotificationSettings.DisabledChecks
                                           .Select(x => x.Id)
                                           .Union(healthCheckConfig.DisabledChecks
                                                  .Select(x => x.Id))
                                           .Distinct()
                                           .ToArray();

                    var checks = _healthChecks
                                 .Where(x => disabledCheckIds.Contains(x.Id) == false);

                    var results = new HealthCheckResults(checks);
                    results.LogResults();

                    // Send using registered notification methods that are enabled
                    foreach (var notificationMethod in _notifications.Where(x => x.Enabled))
                    {
                        await notificationMethod.SendAsync(results, token);
                    }
                }

            return(true); // repeat
        }
Esempio n. 12
0
        /// <summary>
        ///  save a stream to disk
        /// </summary>
        public void SaveFile(string filename, Stream stream)
        {
            logger.Debug <SyncFileService>("Saving File: {0}", filename);

            using (Stream fileStream = OpenWrite(filename))
            {
                stream.CopyTo(fileStream);
                fileStream.Flush();
                fileStream.Close();
            }
        }
        public override async Task <bool> PerformRunAsync(CancellationToken token)
        {
            if (_runtimeState.Level != RuntimeLevel.Run)
            {
                return(true); // repeat...
            }
            switch (_runtimeState.ServerRole)
            {
            case ServerRole.Replica:
                _logger.Debug <HealthCheckNotifier>("Does not run on replica servers.");
                return(true);    // DO repeat, server role can change

            case ServerRole.Unknown:
                _logger.Debug <HealthCheckNotifier>("Does not run on servers with unknown role.");
                return(true);    // DO repeat, server role can change
            }

            // ensure we do not run if not main domain, but do NOT lock it
            if (_runtimeState.IsMainDom == false)
            {
                _logger.Debug <HealthCheckNotifier>("Does not run if not MainDom.");
                return(false); // do NOT repeat, going down
            }

            using (_logger.DebugDuration <HealthCheckNotifier>("Health checks executing", "Health checks complete"))
            {
                var healthCheckConfig = Current.Configs.HealthChecks();

                // Don't notify for any checks that are disabled, nor for any disabled
                // just for notifications
                var disabledCheckIds = healthCheckConfig.NotificationSettings.DisabledChecks
                                       .Select(x => x.Id)
                                       .Union(healthCheckConfig.DisabledChecks
                                              .Select(x => x.Id))
                                       .Distinct()
                                       .ToArray();

                var checks = _healthChecks
                             .Where(x => disabledCheckIds.Contains(x.Id) == false);

                var results = new HealthCheckResults(checks);
                results.LogResults();

                // Send using registered notification methods that are enabled
                foreach (var notificationMethod in _notifications.Where(x => x.Enabled))
                {
                    await notificationMethod.SendAsync(results, token);
                }
            }

            return(true); // repeat
        }
Esempio n. 14
0
        public override bool PerformRun()
        {
            // Globally disabled by feature flag
            if (!_settings.EnableCleanup)
            {
                _logger.Info <ContentVersionCleanup>("ContentVersionCleanup task will not run as it has been globally disabled via configuration.");
                return(false);
            }

            if (_runtimeState.Level != RuntimeLevel.Run)
            {
                return(true); // repeat...
            }

            switch (_runtimeState.ServerRole)
            {
            case ServerRole.Replica:
                _logger.Debug <ContentVersionCleanup>("Does not run on replica servers.");
                return(true);    // DO repeat, server role can change

            case ServerRole.Unknown:
                _logger.Debug <ContentVersionCleanup>("Does not run on servers with unknown role.");
                return(true);    // DO repeat, server role can change

            case ServerRole.Single:
            case ServerRole.Master:
            default:
                break;
            }

            // Ensure we do not run if not main domain, but do NOT lock it
            if (!_runtimeState.IsMainDom)
            {
                _logger.Debug <ContentVersionCleanup>("Does not run if not MainDom.");
                return(false); // do NOT repeat, going down
            }

            var count = _service.PerformContentVersionCleanup(DateTime.Now).Count;

            if (count > 0)
            {
                _logger.Info <ContentVersionCleanup>("Deleted {count} ContentVersion(s).", count);
            }
            else
            {
                _logger.Debug <ContentVersionCleanup>("Task complete, no items were Deleted.");
            }

            return(true);
        }
Esempio n. 15
0
        public override async Task <bool> PerformRunAsync(CancellationToken token)
        {
            // not on replicas nor unknown role servers
            switch (_runtime.ServerRole)
            {
            case ServerRole.Replica:
                _logger.Debug <KeepAlive>("Does not run on replica servers.");
                return(true);    // role may change!

            case ServerRole.Unknown:
                _logger.Debug <KeepAlive>("Does not run on servers with unknown role.");
                return(true);    // role may change!
            }

            // ensure we do not run if not main domain, but do NOT lock it
            if (_runtime.IsMainDom == false)
            {
                _logger.Debug <KeepAlive>("Does not run if not MainDom.");
                return(false); // do NOT repeat, going down
            }

            using (_logger.DebugDuration <KeepAlive>("Keep alive executing", "Keep alive complete"))
            {
                var keepAlivePingUrl = _keepAliveSection.KeepAlivePingUrl;
                try
                {
                    if (keepAlivePingUrl.Contains("{umbracoApplicationUrl}"))
                    {
                        var umbracoAppUrl = _runtime.ApplicationUrl.ToString();
                        if (umbracoAppUrl.IsNullOrWhiteSpace())
                        {
                            _logger.Warn <KeepAlive>("No umbracoApplicationUrl for service (yet), skip.");
                            return(true); // repeat
                        }

                        keepAlivePingUrl = keepAlivePingUrl.Replace("{umbracoApplicationUrl}", umbracoAppUrl.TrimEnd(Constants.CharArrays.ForwardSlash));
                    }

                    var request = new HttpRequestMessage(HttpMethod.Get, keepAlivePingUrl);
                    var result  = await _httpClient.SendAsync(request, token);
                }
                catch (Exception ex)
                {
                    _logger.Error <KeepAlive>(ex, "Keep alive failed (at '{keepAlivePingUrl}').", keepAlivePingUrl);
                }
            }

            return(true); // repeat
        }
Esempio n. 16
0
        public override bool PerformRun()
        {
            // ensure we do not run if not main domain
            if (_runtime.IsMainDom == false)
            {
                _logger.Debug <TempFileCleanup>("Does not run if not MainDom.");
                return(false); // do NOT repeat, going down
            }

            foreach (var dir in _tempFolders)
            {
                CleanupFolder(dir);
            }

            return(true); //repeat
        }
Esempio n. 17
0
        /// <summary>
        /// Tries to find the document matching the request, by running the IPublishedContentFinder instances.
        /// </summary>
        /// <exception cref="InvalidOperationException">There is no finder collection.</exception>
        internal void FindPublishedContent(PublishedRequest request)
        {
            const string tracePrefix = "FindPublishedContent: ";

            // look for the document
            // the first successful finder, if any, will set this.PublishedContent, and may also set this.Template
            // some finders may implement caching

            using (_profilingLogger.DebugDuration <PublishedRouter>(
                       $"{tracePrefix}Executing finders...",
                       $"{tracePrefix}Completed executing finders"))
            {
                //iterate but return on first one that finds it
                var found = _contentFinders.Any(finder =>
                {
                    _logger.Debug <PublishedRouter>("Finder {ContentFinderType}", finder.GetType().FullName);
                    return(finder.TryFindContent(request));
                });

                _profilingLogger.Debug <PublishedRouter>(
                    "Found? {Found} Content: {PublishedContentId}, Template: {TemplateAlias}, Domain: {Domain}, Culture: {Culture}, Is404: {Is404}, StatusCode: {StatusCode}",
                    found,
                    request.HasPublishedContent ? request.PublishedContent.Id : "NULL",
                    request.HasTemplate ? request.TemplateAlias : "NULL",
                    request.HasDomain ? request.Domain.ToString() : "NULL",
                    request.Culture?.Name ?? "NULL",
                    request.Is404,
                    request.ResponseStatusCode);
            }

            // indicate that the published content (if any) we have at the moment is the
            // one that was found by the standard finders before anything else took place.
            request.SetIsInitialPublishedContent();
        }
Esempio n. 18
0
        /// <summary>
        ///  Get the media rewrite folder
        /// </summary>
        /// <remarks>
        ///     looks in appSettings for uSync:mediaFolder
        ///
        ///  <add key="uSync.mediaFolder" value="/something" />
        ///
        ///  or in uSync8.config for media setting
        ///
        ///  <backoffice>
        ///     <media>
        ///         <folder>/somefolder</folder>
        ///     </media>
        ///  </backoffice>
        /// </remarks>
        private string GetMediaFolderSetting(uSyncConfig config)
        {
            // look in the web.config
            var folder = ConfigurationManager.AppSettings["uSync.mediaFolder"];

            if (!string.IsNullOrWhiteSpace(folder))
            {
                return(folder);
            }

            // azure guessing - so for most people seeing this issue,
            // they won't have to do any config, as we will detect it ???
            var useDefault = ConfigurationManager.AppSettings["AzureBlobFileSystem.UseDefaultRoute:media"];

            if (useDefault != null && bool.TryParse(useDefault, out bool usingDefaultRoute) && !usingDefaultRoute)
            {
                // means azure is configured to not use the default root, so the media
                // will be prepended with /container-name.
                var containerName = ConfigurationManager.AppSettings["AzureBlobFileSystem.ContainerName:media"];
                if (!string.IsNullOrWhiteSpace(containerName))
                {
                    logger.Debug <ImagePathMapper>("Calculating media folder path from AzureBlobFileSystem settings");
                    return($"/{containerName}");
                }
            }


            // look in the uSync8.config
            return(config.GetExtensionSetting("media", "folder", string.Empty));
        }
Esempio n. 19
0
        private void RegisterBackgroundTasks()
        {
            LazyInitializer.EnsureInitialized(ref _tasks, ref _started, ref _locker, () =>
            {
                _logger.Debug <SchedulerComponent>("Initializing the scheduler");
                var settings = Current.Configs.Settings();

                var tasks = new List <IBackgroundTask>();

                if (settings.KeepAlive.DisableKeepAliveTask == false)
                {
                    tasks.Add(RegisterKeepAlive(settings.KeepAlive));
                }

                tasks.Add(RegisterScheduledPublishing());
                tasks.Add(RegisterLogScrubber(settings));
                tasks.Add(RegisterTempFileCleanup());

                var healthCheckConfig = Current.Configs.HealthChecks();
                if (healthCheckConfig.NotificationSettings.Enabled)
                {
                    tasks.Add(RegisterHealthCheckNotifier(healthCheckConfig, _healthChecks, _notifications, _logger));
                }

                return(tasks.ToArray());
            });
        }
Esempio n. 20
0
        /// <summary>
        /// The RegEx matches any HTML attribute values that start with a tilde (~), those that match are passed to ResolveUrl to replace the tilde with the application path.
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        /// <remarks>
        /// When used with a Virtual-Directory set-up, this would resolve all URLs correctly.
        /// The recommendation is that the "ResolveUrlsFromTextString" option (in umbracoSettings.config) is set to false for non-Virtual-Directory installs.
        /// </remarks>
        public string EnsureUrls(string text)
        {
            if (_contentSection.ResolveUrlsFromTextString == false)
            {
                return(text);
            }

            using (var timer = _logger.DebugDuration(typeof(IOHelper), "ResolveUrlsFromTextString starting", "ResolveUrlsFromTextString complete"))
            {
                // find all relative URLs (ie. URLs that contain ~)
                var tags = ResolveUrlPattern.Matches(text);
                _logger.Debug <long, int>(typeof(IOHelper), "After regex: {Duration} matched: {TagsCount}", timer.Stopwatch.ElapsedMilliseconds, tags.Count);
                foreach (Match tag in tags)
                {
                    var url = "";
                    if (tag.Groups[1].Success)
                    {
                        url = tag.Groups[1].Value;
                    }

                    // The richtext editor inserts a slash in front of the URL. That's why we need this little fix
                    //                if (url.StartsWith("/"))
                    //                    text = text.Replace(url, ResolveUrl(url.Substring(1)));
                    //                else
                    if (string.IsNullOrEmpty(url) == false)
                    {
                        var resolvedUrl = (url.Substring(0, 1) == "/") ? IOHelper.ResolveUrl(url.Substring(1)) : IOHelper.ResolveUrl(url);
                        text = text.Replace(url, resolvedUrl);
                    }
                }
            }

            return(text);
        }
Esempio n. 21
0
//ToDo: Refactor after reimplement CacheRefresher events
        public void Initialize()
        {
            foreach (var index in _indexCreator.Create())
            {
                _examineManager.AddIndex(index);
                ElasticSearchIndex luceneIndex = (ElasticSearchIndex)index;
            }

            _logger.Debug <ExamineComponent>("Examine shutdown registered with MainDom");

            var registeredIndexers = _examineManager.Indexes.OfType <IIndex>().Count();

            _logger.Info <ExamineComponent>("Adding examine event handlers for {RegisteredIndexers} index providers.",
                                            registeredIndexers);

            // don't bind event handlers if we're not suppose to listen
            if (registeredIndexers == 0)
            {
                return;
            }

            ContentCacheRefresher.CacheUpdated     += ContentCacheRefresherUpdated;
            ContentTypeCacheRefresher.CacheUpdated += ContentTypeCacheRefresherUpdated;
            MediaCacheRefresher.CacheUpdated       += MediaCacheRefresherUpdated;
            MemberCacheRefresher.CacheUpdated      += MemberCacheRefresherUpdated;
        }
Esempio n. 22
0
        private IEnumerable <Type> PrepareComposerTypes()
        {
            // create a list, remove those that cannot be enabled due to runtime level
            var composerTypeList = _composerTypes
                                   .Where(x =>
            {
                // use the min/max levels specified by the attribute if any
                // otherwise, min: user composers are Run, anything else is Unknown (always run)
                //            max: everything is Run (always run)
                var attr     = x.GetCustomAttribute <RuntimeLevelAttribute>();
                var minLevel = attr?.MinLevel ?? (x.Implements <IUserComposer>() ? RuntimeLevel.Run : RuntimeLevel.Unknown);
                var maxLevel = attr?.MaxLevel ?? RuntimeLevel.Run;
                return(_composition.RuntimeState.Level >= minLevel && _composition.RuntimeState.Level <= maxLevel);
            })
                                   .ToList();

            // enable or disable composers
            EnableDisableComposers(composerTypeList);

            // sort the composers according to their dependencies
            var requirements = new Dictionary <Type, List <Type> >();

            foreach (var type in composerTypeList)
            {
                requirements[type] = null;
            }
            foreach (var type in composerTypeList)
            {
                GatherRequirementsFromRequireAttribute(type, composerTypeList, requirements);
                GatherRequirementsFromRequiredByAttribute(type, composerTypeList, requirements);
            }

            // only for debugging, this is verbose
            //_logger.Debug<Composers>(GetComposersReport(requirements));

            // sort composers
            var graph = new TopoGraph <Type, KeyValuePair <Type, List <Type> > >(kvp => kvp.Key, kvp => kvp.Value);

            graph.AddItems(requirements);
            List <Type> sortedComposerTypes;

            try
            {
                sortedComposerTypes = graph.GetSortedItems().Select(x => x.Key).ToList();
            }
            catch (Exception e)
            {
                // in case of an error, force-dump everything to log
                _logger.Info <Composers>("Composer Report:\r\n{ComposerReport}", GetComposersReport(requirements));
                _logger.Error <Composers>(e, "Failed to sort composers.");
                throw;
            }

            // bit verbose but should help for troubleshooting
            //var text = "Ordered Composers: " + Environment.NewLine + string.Join(Environment.NewLine, sortedComposerTypes) + Environment.NewLine;
            _logger.Debug <Composers>("Ordered Composers: {SortedComposerTypes}", sortedComposerTypes);

            return(sortedComposerTypes);
        }
Esempio n. 23
0
        // gets this macro content from the cache
        // ensuring that it is appropriate to use the cache
        private MacroContent GetMacroContentFromCache(MacroModel model)
        {
            // only if cache is enabled
            if (_umbracoContextAccessor.UmbracoContext.InPreviewMode || model.CacheDuration <= 0)
            {
                return(null);
            }

            var cache        = _appCaches.RuntimeCache;
            var macroContent = cache.GetCacheItem <MacroContent>(CacheKeys.MacroContentCacheKey + model.CacheIdentifier);

            if (macroContent == null)
            {
                return(null);
            }

            _plogger.Debug <MacroRenderer>("Macro content loaded from cache '{MacroCacheId}'", model.CacheIdentifier);

            // ensure that the source has not changed
            // note: does not handle dependencies, and never has
            var macroSource = GetMacroFile(model); // null if macro is not file-based

            if (macroSource != null)
            {
                if (macroSource.Exists == false)
                {
                    _plogger.Debug <MacroRenderer>("Macro source does not exist anymore, ignore cache.");
                    return(null);
                }

                if (macroContent.Date < macroSource.LastWriteTime)
                {
                    _plogger.Debug <MacroRenderer>("Macro source has changed, ignore cache.");
                    return(null);
                }
            }

            // this is legacy and I'm not sure what exactly it is supposed to do
            if (macroContent.Control != null)
            {
                macroContent.Control.ID = macroContent.ControlId;
            }

            return(macroContent);
        }
Esempio n. 24
0
        /// <summary>
        ///  Report the changes for a folder
        /// </summary>
        /// <param name="folder">Folder to run the report for</param>
        /// <param name="handlers">List of SyncHandlers to use for the report</param>
        /// <param name="callbacks">Callback functions to keep UI uptodate</param>
        /// <returns>List of actions detailing what would and wouldn't change</returns>
        public IEnumerable <uSyncAction> Report(string folder, IEnumerable <ExtendedHandlerConfigPair> handlers, uSyncCallbacks callbacks)
        {
            var sw = Stopwatch.StartNew();

            fireBulkStarting(ReportStarting);

            logger.Debug <uSyncService>("Reporting For [{0}]", string.Join(",", handlers.Select(x => x.Handler.Name)));

            var actions = new List <uSyncAction>();

            var summary = new SyncProgressSummary(handlers.Select(x => x.Handler), "Reporting", handlers.Count());

            if (GlobalSettings.DebugMode && settings.ReportDebug)
            {
                // debug - full export into a dated folder.
                summary.UpdateMessage("Debug: Creating Extract in Tracker folder");
                logger.Warn <uSyncService>("Running Report Debug - this can be a slow process, don't enable unless you need it");

                callbacks?.Callback?.Invoke(summary);
                this.Export($"~/uSync/Tracker/{DateTime.Now.ToString("yyyyMMdd_HHmmss")}/", handlers, callbacks);
            }

            foreach (var configuredHandler in handlers)
            {
                var handler         = configuredHandler.Handler;
                var handlerSettings = configuredHandler.Settings;

                summary.Increment();

                summary.UpdateHandler(handler.Name, HandlerStatus.Processing, $"Reporting {handler.Name}", 0);

                callbacks?.Callback?.Invoke(summary);

                var handlerActions = handler.Report($"{folder}/{handler.DefaultFolder}", handlerSettings, callbacks?.Update);
                actions.AddRange(handlerActions);

                summary.UpdateHandler(handler.Name, HandlerStatus.Complete,
                                      handlerActions.CountChanges(),
                                      handlerActions.ContainsErrors());
            }

            summary.UpdateMessage("Report Complete");
            callbacks?.Callback?.Invoke(summary);

            fireBulkComplete(ReportComplete, actions);
            sw.Stop();

            logger.Info <uSyncService>("uSync Report: {handlerCount} handlers, processed {itemCount} items, {changeCount} changes in {ElapsedMilliseconds}ms",
                                       handlers.Count(), actions.Count,
                                       actions.CountChanges(),
                                       sw.ElapsedMilliseconds);

            callbacks?.Update?.Invoke($"Processed {actions.Count} items in {sw.ElapsedMilliseconds}ms", 1, 1);

            return(actions);
        }
        private bool RunOnThisServer()
        {
            switch (runtimeState.ServerRole)
            {
            case ServerRole.Replica:
                logger.Debug <CustomBackgroundTask>("Not running on a replica");
                return(false);

            case ServerRole.Unknown:
                logger.Debug <CustomBackgroundTask>("Not running on a unknown server role");
                return(false);

            // case ServerRole.Master:
            // case ServerRole.Single:
            default:
                logger.Debug <CustomBackgroundTask>("Running on master or single");
                return(true);
            }
        }
Esempio n. 26
0
        /// <summary>
        ///  Load the handler sets from the 'Handlers' section of the file
        /// </summary>
        private IList <HandlerSet> LoadHandlerSets(XElement node, uSyncSettings defaultSettings, out string defaultSet)
        {
            var sets = new List <HandlerSet>();

            defaultSet = ValueFromWebConfigOrDefault("DefaultHandlerSet", node.Attribute("Default").ValueOrDefault("default"));

            logger.Debug <uSyncConfig>("Handlers : Default Set {defaultSet}", defaultSet);

            foreach (var setNode in node.Elements("Handlers"))
            {
                var handlerSet = LoadSingleHandlerSet(setNode, defaultSettings);
                if (handlerSet.Handlers.Count > 0)
                {
                    sets.Add(handlerSet);
                }
            }

            return(sets);
        }
Esempio n. 27
0
        public IEnumerable <ExtendedHandlerConfigPair> GetValidHandlers(SyncHandlerOptions options = null)
        {
            if (options == null)
            {
                options = new SyncHandlerOptions(this.DefaultSet);
            }
            EnsureHandlerSet(options);

            var set = config.Settings.HandlerSets
                      .Where(x => x.Name.InvariantEquals(options.Set))
                      .FirstOrDefault();

            if (set == null || set.Handlers.Count == 0)
            {
                logger.Warn <SyncHandlerFactory>("No Handlers configured for requested set {setName}", options.Set);
                return(Enumerable.Empty <ExtendedHandlerConfigPair>());
            }

            var configs = new List <ExtendedHandlerConfigPair>();

            foreach (var settings in set.Handlers.Where(x => x.Enabled))
            {
                // Get the handler
                var handler = syncHandlers.ExtendedHandlers
                              .Where(x => x.Alias.InvariantEquals(settings.Alias))
                              .FirstOrDefault();

                // check its valid for the passed group and action.
                if (handler != null &&
                    IsValidGroup(options.Group, handler) &&
                    IsValidAction(options.Action, settings.Actions))
                {
                    logger.Debug <SyncHandlerFactory>("Adding {handler} to ValidHandler List", handler.Alias);

                    configs.Add(new ExtendedHandlerConfigPair()
                    {
                        Handler  = handler,
                        Settings = settings
                    });
                }
                else
                {
                    // only log if we are doing the default 'everything' group
                    // because weh nfoing groups we choose not to load things.
                    if (string.IsNullOrWhiteSpace(options.Group))
                    {
                        logger.Warn <SyncHandlerFactory>("No Handler with {alias} has been loaded", settings.Alias);
                    }
                }
            }

            return(configs.OrderBy(x => x.Handler.Priority));
        }
Esempio n. 28
0
        public ImagePathMapper(IEntityService entityService,
                               IProfilingLogger logger, uSyncConfig config) : base(entityService)
        {
            this.logger = logger;

            siteRoot    = SystemDirectories.Root;
            mediaFolder = GetMediaFolderSetting(config);

            if (!string.IsNullOrWhiteSpace(mediaFolder))
            {
                logger.Debug <ImagePathMapper>("Media Folders: [{media}]", mediaFolder);
            }
        }
Esempio n. 29
0
        private void InitBackOffice()
        {
            if (globalSettings.ExportAtStartup || (globalSettings.ExportOnSave && !syncFileService.RootExists(globalSettings.RootFolder)))
            {
                uSyncService.Export(globalSettings.RootFolder);
            }

            if (globalSettings.ImportAtStartup)
            {
                uSyncService.Import(globalSettings.RootFolder, false);
            }

            if (globalSettings.ExportOnSave)
            {
                var handlers = syncHandlers.GetValidHandlers("Save", globalSettings);
                foreach (var syncHandler in handlers)
                {
                    logger.Debug <uSyncBackofficeComponent>($"Starting up Handler {syncHandler.Handler.Name}");
                    syncHandler.Handler.Initialize(syncHandler.Settings);
                }
            }
        }
Esempio n. 30
0
        internal IEnumerable <Type> PrepareComposerTypes()
        {
            var requirements = GetRequirements();

            // only for debugging, this is verbose
            //_logger.Debug<Composers>(GetComposersReport(requirements));

            var sortedComposerTypes = SortComposers(requirements);

            // bit verbose but should help for troubleshooting
            //var text = "Ordered Composers: " + Environment.NewLine + string.Join(Environment.NewLine, sortedComposerTypes) + Environment.NewLine;
            _logger.Debug <Composers>("Ordered Composers: {SortedComposerTypes}", sortedComposerTypes);

            return(sortedComposerTypes);
        }