---
title: Creating custom web farm synchronization tasks
related:
  - https://docs.kentico.com/k11/configuring-kentico/optimizing-website-performance/setting-up-web-farms.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/k11/configuring-kentico/optimizing-website-performance/setting-up-web-farms/configuring-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.

## 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 the Kentico web project in Visual Studio (using the **WebSite.sln** or **WebApp.sln** file).
2. Create a [custom module class](https://docs.kentico.com/k11/custom-development/creating-custom-modules/initializing-modules-to-run-custom-code.md).
   - Either add the class into a custom project within the Kentico solution (recommended) or directly into the Kentico web project (into a custom folder under the **CMSApp** project for _web application_ installations, into the **App\_Code** folder for _web site_ installations).

> **Info:** For basic execution of initialization code, you only need to register a "code-only" module through the API. You do NOT need to create a new module within the **Modules** application in the Kentico administration interface.

3. Add a method inside the moduleclass, for example _RegisterCustomTask_.
4. Override the module's **OnInit** method and call the _RegisterCustomTask_ method.

   ```csharp

   using System;

   using CMS;
   using CMS.Base;
   using CMS.DataEngine;
   using CMS.EventLog;
   using CMS.Core;
   using CMS.Helpers;

   // Registers the custom module into the system
   [assembly: RegisterModule(typeof(CustomWebFarmTaskModule))]

   public class CustomWebFarmTaskModule : Module
   {
       // Module class constructor, the system registers the module under the name "CustomWebFarmTasks"
       public CustomWebFarmTaskModule()
           : base("CustomWebFarmTasks")
       {
       }

       // Contains initialization code that is executed when the application starts
       protected override void OnInit()
       {
           base.OnInit();

           RegisterCustomTask();
       }

       // Registers a custom web farm synchronization task in the system
       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);
       }
   }

   ```
5. Deploy these code changes to all of your web farm servers.

Each server in the web farm can now process the custom tasks.

To create the custom tasks, call the **WebFarmHelper.CreateTask** method anywhere in your custom code. For example:

1. Add a _CreateCustomTask_ method into the module class that handles the task registration:

   ```csharp

   // Creates a custom web farm synchronization task
   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. Call _CreateCustomTask()_ within the module's **OnInit** method:

   ```csharp

   // Contains initialization code that is executed when the application starts
   protected override void OnInit()
   {
       base.OnInit();

       RegisterCustomTask();

       CreateCustomTask();
   }

   ```

The instance where you added the code now always creates the custom task during initialization (application start). If the task is successfully created, the system also logs 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.
