Register a task

Current version: 9.2

Use the task manager (TaskManager) to register a distributed task, a deferred task, or a chain of tasks from a client such as the Content Management role. Tasks are added to the Message Bus and are eventually executed by workers in the Cortex Processing Engine.

The examples in this topic demonstrate how to register tasks using the base worker options dictionaries: DeferredWorkerOptionsDictionary and DistributedWorkerOptionsDictionary. Although you can register any task in this way, it is standard practice to create a specialized worker options dictionaries for each worker type. Default worker options dictionaries include:

  • MergeWorkerOptionsDictionary

  • InteractionProjectionWorkerOptionsDictionary

  • ContactProjectionWorkerOptionsDictionary

  • InteractionTrainingWorkerOptionsDictionary

  • ContactTrainingWorkerOptionsDictionary

Register a deferred task

Use the RegisterDeferredTaskAsync() method on the TaskManager class to register a deferred task. The following example:

  • Uses the DeferredWorkerOptionsDictionary base class:

  • Assumes a Sitecore.Documentation.SampleDeferredWorker exists.

  • Assumes that worker includes logic to handle a key of "testkey" with a value of "testvalue".

RequestResponse
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Sitecore.Processing.Engine;
using Sitecore.Processing.Engine.Abstractions;
namespace Sitecore.Docs.Examples
{
    public class DocTaskManager
    {
        public async Task RegisterTask()
        {
            TaskManager taskManager = ServiceLocator.ServiceProvider.GetService<ITaskManager>();

            Guid deferredTaskId = await taskManager.RegisterDeferredTaskAsync(
                new DeferredWorkerOptionsDictionary("Sitecore.Documentation.SampleDeferredWorker",
                new Dictionary<string, string>() { { "testkey", "testvalue" } }),
                null,
                TimeSpan.FromDays(1));
        }
    }
}

Register a distributed task

Use the RegisterDistributedTaskAsync() method on the TaskManager class to register a distributed task. The following example:

  • Uses the DistributedWorkerOptionsDictionary base class:

  • Assumes a Sitecore.Documentation.SampleDistributedWorker exists.

  • Assumes that worker includes logic to handle a key of "testkey" with a value of "testvalue".

    Note

    It is standard practice to create a specialized worker options dictionary for distributed workers that inherits `DistributedWorkerOptionsDictionary`.

  • Uses the ContactDataSourceOptionsDictionary class, which specifies that the worker should get data via data extraction from xConnect. There are four default data source options.

  • Assumes a contact facet with the key "TestFacetKey" exists.

RequestResponse
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Sitecore.Processing.Engine;
using Sitecore.Processing.Engine.Abstractions;
using Sitecore.Processing.Tasks.Options.DataSources.DataExtraction;
using Sitecore.XConnect;

namespace Sitecore.Docs.Examples
{
    public class DocTaskManager
    {
        public async Task RegisterDistributedTask()
        {
            TaskManager taskManager = ServiceLocator.ServiceProvider.GetService<ITaskManager>();

            // First task
            Guid distributedTaskId = await taskManager.RegisterDistributedTaskAsync(
                new ContactDataSourceOptionsDictionary(new ContactExpandOptions(new string[] { "TestFacetKey" }), 300, 300),
                new DistributedWorkerOptionsDictionary("Sitecore.Documentation.SampleDistributedWorker", new Dictionary<string, string> { { "testkey", "testvalue" } }),
                null,
                TimeSpan.MaxValue);
        }
    }
}

Registering tasks for default workers

Refer to the following documentation for information about how to register tasks for specific workers:

  • Training worker

  • Projection worker

  • Merge worker

Note

Many scenarios, such as model training, includes a chain of tasks that is processed by several different types of workers.

Do you have some feedback for us?

If you have suggestions for improving this article,