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); }
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); }
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); }