Implement a Warmup service plugin
In this tutorial, you implement a custom Warmup service plugin with Highpriority that enables you to specify a list within a text file with exact URLs of pages you want request during system restart.
- Create the text file, for example,
WarmupUrls.txt, with the absolute URLs that are to be requested during warmup. The URLs need to be separated by a new line, for example: - In the
SitefinityWebAppproject, create a new folder and name itWarmup. - Create a class
UrlsPlugin.cs - In the
UrlsPluginclass, paste the following code:C#using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Web.Hosting; using Telerik.Sitefinity.Warmup; namespace SitefinityWebApp.Warmup { /// <summary> /// Plugin for the <see cref="WarmupModule"/> that returns a <see cref="WarmupUrl"/> collection for absolute URLs from a text file. /// </summary> public class UrlsPlugin : IWarmupPlugin { #region Properties /// <summary> /// Gets the name of the warmup plug-in. /// </summary> /// <value> /// The name of the warmup plug-in. /// </value> public string Name { get; private set; } #endregion #region Public Methods /// <summary> /// Initializes the new instance of the warmup plug-in. /// </summary> /// <param name="name">The name of the plug-in instance.</param> /// <param name="parameters">Collection of parameters to initialize the instance with.</param> public void Initialize(string name, NameValueCollection parameters) { this.Name = name; var urlsFilePath = parameters[UrlsPlugin.PageUrlsFilePathKey]; if (!urlsFilePath.IsNullOrEmpty()) { var absolutePath = HostingEnvironment.MapPath(urlsFilePath); if (!File.Exists(absolutePath)) throw new FileNotFoundException("The specified file from the Warmup configuration was not found.", absolutePath); this.pageUrls = File.ReadAllLines(absolutePath).Where(l => !l.IsNullOrEmpty() && Uri.IsWellFormedUriString(l, UriKind.Absolute)); } } /// <summary> /// Gets the URLs. /// </summary> /// <returns> /// The URLs collection. /// </returns> public IEnumerable<WarmupUrl> GetUrls() { return this.pageUrls.Select(u => new WarmupUrl(u) { Priority = WarmupPriority.High }); } #endregion #region Fields and Constants private IEnumerable<string> pageUrls = Enumerable.Empty<string>(); private const string PageUrlsFilePathKey = "urlsFilePath"; #endregion } }
Next, you add the plugin configuration in the backend or on the file system. Refer to the following sections.
Add the plugin configuration in the backend
- Navigate to Administration » Settings » Advanced settings » Warmup.
- Expand the Warmup node and then the Plugins node.
- Click Create newand enter values the following fields:
- Name:
URLs Plugin - Type:
SitefinityWebApp.Warmup.UrlsPlugin - Make sure the Enabled checkbox is selected.
- Set the priority to High.
- Name:
- Save your changes.
As a result, you can see your newly created plugin in the Plugins node.
To add a plugin parameter:
- Navigate to Plugins » URLs Plugin » Parameters.
- Click Create new.
- Enter the following:
- Key:
urlsFilePath - Value:
~/App_Data/WarmupUrls.txt
- Key:
- Save your changes.
After full restart of your site, the Warmup service can leverage the URLs Plugin.
Add the plugin configuration on the file system
In the ~/App_Data/Sitefinity/Configuration folder of your application create a WarmupConfig.config file and paste the following code:
<?xml version="1.0" encoding="utf-8"?>
<warmupConfig xmlns:config="urn:telerik:sitefinity:configuration" xmlns:type="urn:telerik:sitefinity:configuration:type" config:version="10.0.6400.0">
<plugins>
<add enabled="True" priority="High" type="SitefinityWebApp.Warmup.UrlsPlugin" urlsFilePath="~/App_Data/WarmupUrls.txt" name="URLs Plugin"/>
</plugins>
</warmupConfig>
NOTE: The
urlsFilePathparameter specifies the application’s relative path to the text file that contains the list with absolute URLs to be requested during warmup.