Creating custom web farm synchronization tasks

Web farm tasks are code executed by web farm servers. Create custom web farm tasks if you extended Kentico with functions that work directly with the application’s memory or file storages. For example, if your custom function writes files to the file system and you want these files to be synchronized across all servers in your web farm, you need to write a custom web farm task that handles the synchronization.

Add the code of your tasks into a class placed in the App_Code folder of your web project (or CMSApp_AppCode -> Old_App_Code if you installed Kentico as a web application).

Example

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 (or CMSApp_AppCode -> Old_App_Code) and add a new class named WebFarmTaskLoader.cs.

  2. Add the following references:

    
    
    
     using CMS.Base;
     using CMS.EventLog;
     using CMS.Core;
     using CMS.Helpers;
    
    
     
  3. Delete the default class declaration and its content.

  4. Extend the CMSModuleLoader partial class and define a new attribute as shown below:

    
    
    
     [WebFarmTaskLoader]
     public partial class CMSModuleLoader
     {
         /// <summary>
         /// Custom attribute class.
         /// </summary>
         private class WebFarmTaskLoaderAttribute : CMSLoaderAttribute
         {
    
         }
     }
    
    
     
  5. Create a new method inside the WebFarmTaskLoaderAttribute class, for example a RegisterCustomTask method:

    
    
    
     /// <summary>
     /// Registers a custom web farm synchronization task in the system.
     /// </summary>
     public void RegisterCustomTask()
     {
         // Creates a custom web farm task which logs information to the event log
         WebFarmTask task = new WebFarmTask
         {
             Type = "CustomTask",
    
             // Logs a record into the system's event log, with 'Execute' as the event code
             Execute = (target, textData, binaryData) => 
             { 
                 string message = String.Format("Slave {0} is going to process task from master {1}", SystemContext.ServerName, textData);
                 EventLogProvider.LogInformation(target, "Execute", message); 
    
                 // TODO: Slave operation
             }
         };
    
         // Registers the given web farm task
         WebFarmHelper.RegisterTask(task);
     }
    
    
     
  6. Override the Init method inside the WebFarmTaskLoaderAttributeclass and call the RegisterCustomTask method.

    
    
    
     /// <summary>
     /// Called automatically when the application starts.
     /// </summary>
     public override void Init()
     {
         RegisterCustomTask();
     }
    
    
     
  7. Deploy these code changes to all your web farm servers.

Each server in the web farm can now process custom tasks. You can create the tasks through the API anywhere in your custom code. For example,

  1. Create a CreateCustomTask method in the same class:

    
    
    
     /// <summary>
     /// Creates a custom web farm synchronization task.
     /// </summary>
     public void CreateCustomTask()
     { 
         // TODO: Master operation
    
         // Prepares the data parameter of the custom task
         string data = SystemContext.ServerName;
    
         // Creates the custom web farm synchronization task
         if (WebFarmHelper.CreateTask("CustomTask", "MyTask", data, null))
         {
             // Runs if the custom task was created successfully
             // Logs a record in the event log on the server that created the web farm task
             string message = String.Format("Master {0} finished processing operation and created task for slaves", SystemContext.ServerName);
             EventLogProvider.LogInformation("CustomTask", "Create", message);
         }
     }
    
    
     
  2. And call the created method in the Init method:

    
    
    
     /// <summary>
     /// Called automatically when the application starts.
     /// </summary>
     public override void Init()
     {
         RegisterCustomTask();
    
         CreateCustomTask();
     }
    
    
     
  3. Deploy these code changes to all your web farm servers.

The web farm servers create the custom task when the application starts. If successful, the servers also log a record into the event log, with Create as the event code. You can see the results by checking the event log in the Event log application.

You need to have web farms properly configured and working (have more than one server in the web farm) to try out this example.