---
title: "タスクチェイン"
slug: "task-chaining"
abstract: ""
---
# タスクチェイン

タスクの実行順序は、各タスクを登録する際に前提条件タスクIDのリストを渡すことで決定できます。次の例では、タスク#2とタスク#3は前提条件タスクIDの配列で登録されています。

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 Sample()
 {
 TaskManager taskManager = null;

 // Task #1
 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);

 // Task #2
 Guid deferredTaskId = await taskManager.RegisterDeferredTaskAsync(
 new DeferredWorkerOptionsDictionary("Sitecore.Documentation.SampleDeferredWorker",
 new Dictionary<string, string>() { { "testkey", "testvalue" } }),
 new Guid { distributedTaskId }, // Will not run before task #1 is complete
 TimeSpan.MaxValue);

 // Task #3
 Guid deferredTaskId = await taskManager.RegisterDeferredTaskAsync(
 new DeferredWorkerOptionsDictionary("Sitecore.Documentation.SecondSampleDeferredWorker",
 new Dictionary<string, string>() { { "testkey", "testvalue" } }),
 new Guid { deferredTaskId }, // Will not run before task #2 is complete
 TimeSpan.MaxValue);
 }
 }
}

## チェイン投影とマージ

データ投影タスクを登録する際は、データ投影が完了すると実行される依存するマージタスクも登録しなければなりません。

データ投影は分散ワーカー(ProjectionWorker)によって実行されます。各ワーカーはCortex Processing Engine Storageデータベース内の自分の一時テーブルにデータを投影します。つまり、10人のワーカーが10個の別々のテーブルを生成します。マージワーカー(MergeWorker)はデータを1つのテーブルにまとめ、一時テーブルを削除します。

> !ヒントSitecore.Processing.Engine.Abstractions.TaskManagerExtensions拡張クラスのRegisterModelTrainingTaskChainAsync()メソッドは、データ投影とマージを含む3つのタスクの連鎖を作成します。