Esempio n. 1
0
        public HttpResponseMessage PrerenderPages() // /umbraco/api/carbidesupport/prerenderpages/
        {
            string result = "";

            if (HttpContext.Current.Application["RebuildCacheStatus"] == null)
            {
                var context = HttpContext.Current;

                context.Application["RebuildCacheStatus"]  = "running";
                context.Application["RebuildCacheHistory"] = "<h4 style=\"font-size: 1.1rem; margin-bottom: 1.5rem;\">Started " + Temporal.DateFormat(DateTime.Now, DateFormats.European).ToUpper() + " @ " + Temporal.TimeFormat(DateTime.Now, TimeFormats.SqlMilitary) + "</h4>";

                result = context.Application["RebuildCacheHistory"].ToString();

                Thread workerThread = new Thread(new ThreadStart(() =>
                {
                    StopWatch timer  = new StopWatch();
                    StopWatch timer2 = new StopWatch();

                    try
                    {
                        timer.Start();
                        context.Server.ScriptTimeout = 100000;

                        context.Application["RebuildCacheHistory"] += "<ol style=\"padding: 0.25rem 0 0 1rem;\">";

                        context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Pre-rendering templates... ";
                        timer2.Reset();
                        timer2.Start();

                        var umbracoHelper    = new UmbracoHelper(Carbide.ContextHelpers.EnsureUmbracoContext());
                        int pageCounter      = 0;
                        List <int> templates = new List <int>();

                        foreach (var node in umbracoHelper.TypedContentAtRoot())
                        {
                            ListChildNodes(node, ref pageCounter, ref context, ref templates);
                        }

                        if (pageCounter > 1)
                        {
                            var msg = context.Application["RebuildCacheHistory"].ToString();
                            msg     = msg.Substring(0, msg.LastIndexOf("..."));
                            context.Application["RebuildCacheHistory"] = msg + "... ";
                        }

                        timer2.Stop();
                        context.Application["RebuildCacheHistory"] += "<strong>" + pageCounter + " template" + (pageCounter != 1 ? "s" : "") + " in " + timer2.GetSeconds <int>() + " seconds</strong></li>";

                        timer.Stop();

                        context.Application.SafeRemove("RebuildCacheStatus");

                        context.Application["RebuildCacheHistory"] += "</ol>";

                        context.Application["RebuildCacheHistory"] += "<h4 style=\"font-size: 1.1rem;\">Finished in " + timer.GetSeconds <int>() + " seconds</h4>";
                    }

                    catch (Exception e)
                    {
                        timer.Stop();
                        timer2.Stop();

                        context.Application.SafeRemove("RebuildCacheStatus");

                        context.Application["RebuildCacheHistory"] = "</li></ol><p><strong>Error in " + timer.GetSeconds <int>() + " seconds on " + Temporal.DateFormat(DateTime.Now, DateFormats.European).ToUpper() + " @ " + Temporal.TimeFormat(DateTime.Now, TimeFormats.SqlMilitary) + "</strong></p>" + e.Message;

                        result = context.Application["RebuildCacheHistory"].ToString();
                    }
                }))
                {
                    IsBackground = true
                };
                workerThread.Start();

                while (HttpContext.Current.Application["RebuildCacheStatus"] == null)
                {
                    // Wait for worker thread to start up and initialize
                    System.Threading.Thread.Sleep(50);
                }
            }

            else
            {
                result = HttpContext.Current.Application["RebuildCacheHistory"].ToString();
            }

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            response.Content = new StringContent(result, Encoding.UTF8, "text/plain");
            return(response);
        }
Esempio n. 2
0
        public HttpResponseMessage RebuildCache() // /umbraco/api/carbidesupport/rebuildcache/
        {
            string result = "";

            if (HttpContext.Current.Application["RebuildCacheStatus"] == null)
            {
                var context = HttpContext.Current;

                context.Application["RebuildCacheStatus"]  = "running";
                context.Application["RebuildCacheHistory"] = "<h4 style=\"font-size: 1.1rem; margin-bottom: 1.5rem;\">Started " + Temporal.DateFormat(DateTime.Now, DateFormats.European).ToUpper() + " @ " + Temporal.TimeFormat(DateTime.Now, TimeFormats.SqlMilitary) + "</h4>";

                result = context.Application["RebuildCacheHistory"].ToString();

                Thread workerThread = new Thread(new ThreadStart(() =>
                {
                    StopWatch timer  = new StopWatch();
                    StopWatch timer2 = new StopWatch();

                    try
                    {
                        timer.Start();
                        context.Server.ScriptTimeout = 100000;

                        context.Application["RebuildCacheHistory"] += "<ol style=\"padding: 0.25rem 0 0 1rem;\">";

                        context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Republishing all content... ";
                        timer2.Reset();
                        timer2.Start();
                        Services.ContentService.RePublishAll();
                        timer2.Stop();
                        context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";

                        context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Refreshing XML cache... ";
                        timer2.Reset();
                        timer2.Start();
                        umbraco.library.RefreshContent();
                        timer2.Stop();
                        context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";

                        context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Rebuilding Examine indexes... ";
                        timer2.Reset();
                        timer2.Start();

                        foreach (var index in ExamineManager.Instance.IndexProviderCollection.ToList())
                        {
                            context.Application["RebuildCacheHistory"] += index.Name.Replace("Indexer", "") + "... ";
                            index.RebuildIndex();
                        }

                        timer2.Stop();
                        context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";

                        timer.Stop();

                        context.Application.SafeRemove("RebuildCacheStatus");

                        context.Application["RebuildCacheHistory"] += "</ol>";

                        context.Application["RebuildCacheHistory"] += "<h4 style=\"font-size: 1.1rem;\">Finished in " + timer.GetSeconds <int>() + " seconds</h4>";
                    }

                    catch (Exception e)
                    {
                        timer.Stop();
                        timer2.Stop();

                        context.Application.SafeRemove("RebuildCacheStatus");

                        context.Application["RebuildCacheHistory"] = "</li></ol><p><strong>Error in " + timer.GetSeconds <int>() + " seconds on " + Temporal.DateFormat(DateTime.Now, DateFormats.European).ToUpper() + " @ " + Temporal.TimeFormat(DateTime.Now, TimeFormats.SqlMilitary) + "</strong></p>" + e.Message;

                        result = context.Application["RebuildCacheHistory"].ToString();
                    }
                }))
                {
                    IsBackground = true
                };
                workerThread.Start();

                while (HttpContext.Current.Application["RebuildCacheStatus"] == null)
                {
                    // Wait for worker thread to start up and initialize
                    System.Threading.Thread.Sleep(50);
                }
            }

            else
            {
                result = HttpContext.Current.Application["RebuildCacheHistory"].ToString();
            }

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            response.Content = new StringContent(result, Encoding.UTF8, "text/plain");
            return(response);
        }
Esempio n. 3
0
        public HttpResponseMessage RebuildImageCache() // /umbraco/api/carbidesupport/rebuildimagecache/
        {
            string result = "";

            if (HttpContext.Current.Application["RebuildCacheStatus"] == null)
            {
                var context = HttpContext.Current;

                context.Application["RebuildCacheStatus"]  = "running";
                context.Application["RebuildCacheHistory"] = "<h4 style=\"font-size: 1.1rem; margin-bottom: 1.5rem;\">Started " + Temporal.DateFormat(DateTime.Now, DateFormats.European).ToUpper() + " @ " + Temporal.TimeFormat(DateTime.Now, TimeFormats.SqlMilitary) + "</h4>";

                result = context.Application["RebuildCacheHistory"].ToString();

                Thread workerThread = new Thread(new ThreadStart(() =>
                {
                    StopWatch timer  = new StopWatch();
                    StopWatch timer2 = new StopWatch();

                    try
                    {
                        timer.Start();
                        context.Server.ScriptTimeout = 100000;

                        context.Application["RebuildCacheHistory"] += "<ol style=\"padding: 0.25rem 0 0 1rem;\">";

                        context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Clearing cached images... ";
                        timer2.Start();

                        foreach (var folder in Storage.GetFolders("/App_Data/cache/"))
                        {
                            Storage.DeleteDirectory("/App_Data/cache/" + folder);

                            if (Storage.DirectoryExists("/App_Data/cache/" + folder))
                            {
                                // Retry up to 5 times after pausing...

                                int retry = 0;

                                string original = context.Application["RebuildCacheHistory"].ToString();

                                while (retry < 5)
                                {
                                    retry++;

                                    context.Application["RebuildCacheHistory"] = original + "cache/" + folder + " retry " + retry;

                                    Temporal.PauseExecution(1);

                                    if (Storage.DirectoryExists("/App_Data/cache/" + folder))
                                    {
                                        Storage.DeleteDirectory("/App_Data/cache/" + folder);
                                    }

                                    if (!Storage.DirectoryExists("/App_Data/cache/" + folder))
                                    {
                                        retry = 5;
                                    }
                                }

                                context.Application["RebuildCacheHistory"] = original;

                                if (Storage.DirectoryExists("/App_Data/cache/" + folder))
                                {
                                    context.Application["RebuildCacheHistory"] += "<strong style='color:#b94a48;'>cache/" + folder + " locked...</strong> ";
                                }
                            }
                        }

                        timer2.Stop();
                        context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";

                        timer.Stop();

                        context.Application.SafeRemove("RebuildCacheStatus");

                        context.Application["RebuildCacheHistory"] += "</ol>";

                        context.Application["RebuildCacheHistory"] += "<h4 style=\"font-size: 1.1rem;\">Finished in " + timer.GetSeconds <int>() + " seconds</h4>";
                    }

                    catch (Exception e)
                    {
                        timer.Stop();
                        timer2.Stop();

                        context.Application.SafeRemove("RebuildCacheStatus");

                        context.Application["RebuildCacheHistory"] = "</li></ol><p><strong>Error in " + timer.GetSeconds <int>() + " seconds on " + Temporal.DateFormat(DateTime.Now, DateFormats.European).ToUpper() + " @ " + Temporal.TimeFormat(DateTime.Now, TimeFormats.SqlMilitary) + "</strong></p>" + e.Message;

                        result = context.Application["RebuildCacheHistory"].ToString();
                    }
                }))
                {
                    IsBackground = true
                };
                workerThread.Start();

                while (HttpContext.Current.Application["RebuildCacheStatus"] == null)
                {
                    // Wait for worker thread to start up and initialize
                    System.Threading.Thread.Sleep(50);
                }
            }

            else
            {
                result = HttpContext.Current.Application["RebuildCacheHistory"].ToString();
            }

            var response = new HttpResponseMessage(HttpStatusCode.OK);

            response.Content = new StringContent(result, Encoding.UTF8, "text/plain");
            return(response);
        }
Esempio n. 4
0
        public HttpResponseMessage CompactDatabase(string days = "30")         // /umbraco/api/carbidesupport/compactdatabase/
        {
            string result   = "";
            var    response = new HttpResponseMessage(HttpStatusCode.OK);

            if (HttpContext.Current.Application["RebuildCacheStatus"] == null)
            {
                var context = HttpContext.Current;

                context.Application["RebuildCacheStatus"]  = "running";
                context.Application["RebuildCacheHistory"] = "<h4 style=\"font-size: 1.1rem; margin-bottom: 1.5rem;\">Started " + TemporalHelpers.DateFormat(DateTime.Now, DateFormats.European).ToUpper() + " @ " + TemporalHelpers.TimeFormat(DateTime.Now, TimeFormats.SqlMilitary) + "</h4>";

                result = context.Application["RebuildCacheHistory"].ToString();

                if (days.IsPureNumeric() == true)
                {
                    int _days = int.Parse(days);

                    Thread workerThread = new Thread(new ThreadStart(() =>
                    {
                        StopWatch timer  = new StopWatch();
                        StopWatch timer2 = new StopWatch();

                        try
                        {
                            timer.Start();
                            context.Server.ScriptTimeout = 100000;

                            context.Application["RebuildCacheHistory"] += "<ol style=\"padding: 0.25rem 0 0 1rem;\">";

                            var daterange = "DATEADD(d, " + (_days * -1) + ", getdate())";
                            var ageText   = "> " + _days + " days old";



                            // Truncate log

                            timer2.Start();
                            context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Truncating log (" + ageText + ")... ";

                            var commands = @"DECLARE @out int = (SELECT COUNT(*) FROM umbracoLog WHERE Datestamp < " + daterange + @");
DELETE FROM umbracolog WHERE Datestamp < " + daterange + @";
SELECT @out;";
                            context.Application["RebuildCacheHistory"] += Carbide.SqlHelpers.Lookup <int>(commands, "umbracoDbDSN").FormatNumber(NumberFormats.Proper) + " deleted... ";

                            timer2.Stop();
                            context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";



                            // Delete unused property data

                            timer2.Start();
                            context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Delete unused property data (" + ageText + ")... ";

                            commands = @"DECLARE @out int = (SELECT COUNT(*) FROM cmsPropertyData WHERE
	versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > "     + daterange + @" OR newest = 1) AND
	contentNodeId IN (SELECT DISTINCT nodeID FROM cmsDocument));
DELETE FROM cmsPropertyData WHERE
	versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > "     + daterange + @" OR newest = 1) AND
	contentNodeId IN (SELECT DISTINCT nodeID FROM cmsDocument);
SELECT @out;";
                            context.Application["RebuildCacheHistory"] += Carbide.SqlHelpers.Lookup <int>(commands, "umbracoDbDSN").FormatNumber(NumberFormats.Proper) + " deleted... ";

                            timer2.Stop();
                            context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";



                            // Delete preview XML (pass 1)

                            timer2.Start();
                            context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Delete XML previews (" + ageText + ")... ";

                            commands = @"DECLARE @out int = (SELECT COUNT(*) FROM cmsPreviewXml WHERE
	versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > "     + daterange + @" OR newest = 1) AND
	nodeId IN (SELECT DISTINCT nodeID FROM cmsDocument));
DELETE FROM cmsPreviewXml WHERE
	versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > "     + daterange + @" OR newest = 1) AND
	nodeId IN (SELECT DISTINCT nodeID FROM cmsDocument);
SELECT @out;";
                            context.Application["RebuildCacheHistory"] += Carbide.SqlHelpers.Lookup <int>(commands, "umbracoDbDSN").FormatNumber(NumberFormats.Proper) + " deleted... ";

                            timer2.Stop();
                            context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";



                            // Delete content versions

                            timer2.Start();
                            context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Delete content versions (" + ageText + ")... ";

                            commands = @"DECLARE @out int = (SELECT COUNT(*) FROM cmsContentVersion WHERE
	versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > "     + daterange + @" OR newest = 1) AND
	ContentId  IN (SELECT DISTINCT nodeID FROM cmsDocument));
DELETE FROM cmsContentVersion WHERE
	versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > "     + daterange + @" OR newest = 1) AND
	ContentId  IN (SELECT DISTINCT nodeID FROM cmsDocument);
SELECT @out;";
                            context.Application["RebuildCacheHistory"] += Carbide.SqlHelpers.Lookup <int>(commands, "umbracoDbDSN").FormatNumber(NumberFormats.Proper) + " deleted... ";

                            timer2.Stop();
                            context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";



                            // Delete unpublished content

//						timer2.Start();
//							context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Delete unpublished content (" + ageText + ")... ";

//							commands = @"DECLARE @out int = (SELECT COUNT(*) FROM cmsDocument WHERE
//	versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > " + daterange + @" OR newest = 1) AND
//	nodeId IN (SELECT DISTINCT nodeID FROM cmsDocument));
//DELETE FROM cmsDocument WHERE
//	versionId NOT IN (SELECT versionId FROM cmsDocument WHERE updateDate > " + daterange + @" OR newest = 1) AND
//	nodeId IN (SELECT DISTINCT nodeID FROM cmsDocument);
//SELECT @out;";
//							context.Application["RebuildCacheHistory"] += Carbide.SqlHelpers.Lookup<int>(commands, "umbracoDbDSN").FormatNumber(NumberFormats.Proper) + " deleted... ";

//							timer2.Stop();
//							context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds<int>() + " seconds</strong></li>";



                            // Delete preview XML (pass 2)

                            timer2.Start();
                            context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Delete XML previews (pass 2)... ";

                            commands = @"DECLARE @out int = (SELECT COUNT(*) FROM cmsPreviewXml WHERE
	versionId IN (SELECT cmsPreviewXml.versionId FROM cmsPreviewXml JOIN cmsDocument ON cmsPreviewXml.versionId=cmsDocument.versionId WHERE cmsDocument.newest <> 1));
DELETE FROM cmsPreviewXml WHERE
	versionId IN (SELECT cmsPreviewXml.versionId FROM cmsPreviewXml JOIN cmsDocument ON cmsPreviewXml.versionId=cmsDocument.versionId WHERE cmsDocument.newest <> 1);
SELECT @out;";
                            context.Application["RebuildCacheHistory"] += Carbide.SqlHelpers.Lookup <int>(commands, "umbracoDbDSN").FormatNumber(NumberFormats.Proper) + " deleted... ";

                            timer2.Stop();
                            context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";



                            // REBUILD CONTENT CACHE

                            timer2.Reset();
                            context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Republishing all content... ";
                            timer2.Start();
                            Services.ContentService.RePublishAll();
                            timer2.Stop();
                            context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";

                            timer2.Reset();
                            context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Refreshing XML cache... ";
                            timer2.Start();
                            umbraco.library.RefreshContent();
                            timer2.Stop();
                            context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";

                            timer2.Reset();
                            context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Rebuilding Examine indexes... ";
                            timer2.Start();

                            foreach (var index in ExamineManager.Instance.IndexProviderCollection.ToList())
                            {
                                context.Application["RebuildCacheHistory"] += index.Name.Replace("Indexer", "") + "... ";
                                index.RebuildIndex();
                            }

                            timer2.Stop();
                            context.Application["RebuildCacheHistory"] += "<strong>completed in " + timer2.GetSeconds <int>() + " seconds</strong></li>";


                            timer.Stop();

                            context.Application.SafeRemove("RebuildCacheStatus");

                            context.Application["RebuildCacheHistory"] += "</ol>";
                            context.Application["RebuildCacheHistory"] += "<h4 style=\"font-size: 1.1rem;\">Finished in " + timer.GetSeconds <int>() + " seconds</h4>";
                        }

                        catch (Exception e)
                        {
                            timer.Stop();
                            timer2.Stop();

                            context.Application.SafeRemove("RebuildCacheStatus");

                            context.Application["RebuildCacheHistory"] += "</li></ol><p><strong>Error in " + timer.GetSeconds <int>() + " seconds on " + TemporalHelpers.DateFormat(DateTime.Now, DateFormats.European).ToUpper() + " @ " + TemporalHelpers.TimeFormat(DateTime.Now, TimeFormats.SqlMilitary) + "</strong></p>" + e.Message;

                            result = context.Application["RebuildCacheHistory"].ToString();
                        }
                    }))
                    {
                        IsBackground = true
                    };
                    workerThread.Start();

                    while (HttpContext.Current.Application["RebuildCacheStatus"] == null)
                    {
                        // Wait for worker thread to start up and initialize
                        System.Threading.Thread.Sleep(50);
                    }
                }

                else
                {
                    context.Application.SafeRemove("RebuildCacheStatus");

                    context.Application["RebuildCacheHistory"] += "<ol style=\"padding: 0.25rem 0 0 1rem;\">";
                    context.Application["RebuildCacheHistory"] += "<li style=\"padding-bottom: 1rem;\">Days is not a numeric value.</li>";
                    context.Application["RebuildCacheHistory"] += "</ol>";
                    context.Application["RebuildCacheHistory"] += "<h4 style=\"font-size: 1.1rem;\">Aborted</h4>";

                    result = context.Application["RebuildCacheHistory"].ToString();
                }
            }

            else
            {
                result = HttpContext.Current.Application["RebuildCacheHistory"].ToString();
            }

            response.Content = new StringContent(result, Encoding.UTF8, "text/plain");

            return(response);
        }