Kentico CMS 7.0 Developer's Guide

Creating custom web farm synchronization tasks

Creating custom web farm synchronization tasks

Previous topic Next topic Mail us feedback on this topic!  

Creating custom web farm synchronization tasks

Previous topic Next topic JavaScript is required for the print function Mail us feedback on this topic!  

You can write custom tasks to be executed by web farm servers. This is done by adding the code of your tasks into the App_Code folder of your web project (or Old_App_Code if you installed Kentico CMS as a web application).
 

The following example shows how to create a custom synchronization task that logs information events into the event log of all servers in the web farm:

 

1. Open your web project in Visual Studio, expand the App_Code folder and add a new class into it called WebFarmTaskLoader.cs.

 

2. Add the following references:

 

[C#]

 

using CMS.SettingsProvider;
using CMS.EventLog;
using CMS.WebFarmSyncHelper;

 

3. Delete the default class declaration and its content. Instead extend the CMSModuleLoader partial class and define a new attribute for it as shown below:

 
[C#]

 

[WebFarmTaskLoader]

public partial class CMSModuleLoader

{

  /// <summary>

  /// Module registration

  /// </summary>

  private class WebFarmTaskLoaderAttribute : CMSLoaderAttribute

   {

       ...      

   }

}

 

4. Enter the following code into the WebFarmTaskLoaderAttribute class:

 

[C#]

 

/// <summary>
/// Called automatically when the application starts
/// </summary>
public override void Init()
{
  // Hook the custom task event
  WebSyncHelper.OnProcessCustomTask += new WebSyncHelper.TaskHandler(WebSyncHelper_OnProcessCustomTask);
}
 

 
static void WebSyncHelper_OnProcessCustomTask(WebFarmTaskTypeEnum taskType, string target, string data, int taskId)
{
  // Log execution
  EventLogProvider ev = new EventLogProvider();
   ev.LogEvent("I", DateTime.Now, "CustomTask", "Execute", null, data);
}

 

The override of the Init() method is called automatically when the application starts. The code assigns a handler for the OnProcessCustomTask event, which is then used to log a record with Execute as its event code into the website's event log.

 

5. Repeat the steps above for all web servers in the web farm, so that the Execute event is logged for each one. Build their projects if they are installed as a web application.

 

6. Select one web farm server and expand the Init() method according to the following:

 

[C#]

 

/// <summary>
/// Called automatically when the application starts
/// </summary>
public override void Init()
{
  // Hook the custom task event
  WebSyncHelper.OnProcessCustomTask += new WebSyncHelper.TaskHandler(WebSyncHelper_OnProcessCustomTask);
 
 
  string data = "Task from " + WebSyncHelperClass.ServerName;

 
   // Add your actions for the custom synchronization task
  if (WebSyncHelperClass.CreateTask(WebFarmTaskTypeEnum.Custom, "MyTask", data, null))
   {
      // Log creation
      EventLogProvider ev = new EventLogProvider();
       ev.LogEvent("I", DateTime.Now, "CustomTask", "Create", null, data);
   }
}

 

This ensures that the custom task is created when the selected server is started and if successful, an event with Create as its event code will be logged for the given server.

 

7. Finally, save the changes (Build the project if you installed it as a web application). You can see the results by checking the event log for all web farm servers at Site Manager -> Administration -> Event log.