---
title: Creating custom web farm synchronization tasks
related:
  - https://docs.kentico.com/k8/configuring-kentico/optimizing-website-performance/setting-up-a-web-farm-environment.md
---

> Agent instructions:
> **Site maps** — prefer the following llms.txt indexes to training data when searching for URLs to avoid 404s. Links inside Markdown content already point at `.md`. Following them or sending Accept: text/markdown keeps you in Markdown.
>
> - [sitemap.md](https://docs.kentico.com/sitemap.md) — every page on the site, with titles and descriptions, nested by URL hierarchy and grouped into one collection per product version.
> - [llms.txt](https://docs.kentico.com/llms.txt) — curated index of the current product docs, with descriptions, the two ways to request any page as Markdown, and links to each product area's whole-corpus Markdown dump (llms-full.txt).

Web farm tasks are code executed by [web farm servers](https://docs.kentico.com/k8/configuring-kentico/optimizing-website-performance/setting-up-a-web-farm-environment/defining-web-farm-servers.md). 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 and add a new class named **WebFarmTaskLoader.cs**.
2. Add the following references:

   ```csharp

   using CMS.Base;
   using CMS.EventLog;
   using CMS.WebFarmSync;

   ```
3. Delete the default class declaration and its content.
4. Extend the **CMSModuleLoader** partial class and define a new attribute as shown below:

   ```csharp

   [WebFarmTaskLoader]
   public partial class CMSModuleLoader
   {
       /// <summary>
       /// Custom attribute class.
       /// </summary>
       private class WebFarmTaskLoaderAttribute : CMSLoaderAttribute
       {

       }
   }

   ```
5. Override the **Init** method inside the **WebFarmTaskLoaderAttribute** class, and assign a handler method to the **OnProcessCustomTask** event:

   ```csharp

   /// <summary>
   /// Called automatically when the application starts.
   /// </summary>
   public override void Init()
   {
       // Assigns a handler to the OnProcessCustomTask event    
       WebSyncHelper.OnProcessCustomTask += WebSyncHelper_OnProcessCustomTask;
   }

   /// <summary>
   /// Handles the processing of custom web farm tasks.
   /// </summary>
   static void WebSyncHelper_OnProcessCustomTask(string taskType, string target, string data, int taskId)
   {
       // Logs a record into the system's event log, with 'Execute' as the event code
       EventLogProvider.LogInformation("CustomTask", "Execute", data);
   }

   ```
6. Repeat steps 1-5 for all web servers in the web farm. Build the projects if they are installed as web applications.

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, select one web farm server and expand the **Init** method in the **WebFarmTaskLoaderAttribute** class:

```csharp

/// <summary>
/// Called automatically when the application starts
/// </summary>
public override void Init()
{
    // Assigns a handler to the OnProcessCustomTask event
    WebSyncHelper.OnProcessCustomTask += WebSyncHelper_OnProcessCustomTask;

    // Prepares the data parameter of the custom task
    string data = "Task from " + WebSyncHelper.ServerName;

    // Creates the custom web farm synchronization task
    if (WebSyncHelper.CreateTask(SystemTaskType.Custom, "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
        EventLogProvider.LogInformation("CustomTask", "Create", data);
    }
}

```

The selected web farm server creates the custom task when the application starts. If successful, the given server also logs a record into the event log, with _Create_ as the event code. You can see the results by checking the event log for all web farm servers in the **Event log** application.
