分散ワーカーの作成

Version:
日本語翻訳に関する免責事項

このページの翻訳はAIによって自動的に行われました。可能な限り正確な翻訳を心掛けていますが、原文と異なる表現や解釈が含まれる場合があります。正確で公式な情報については、必ず英語の原文をご参照ください。

次のサンプル 分散ワーカー は、xConnectクライアントAPIを使用して、コンタクトのバッチから特定のソースの識別子を削除します。ワーカーは、以下を定義するカスタムワーカーオプションディクショナリ(SampleDistributedWorkerOptionsDictionary)を受け入れます。

  • 削除する識別子の識別子ソース

  • ワーカーの完全修飾名

分散ワーカー オプション ディクショナリの作成

DistributedWorkerOptionsDictionaryクラスを使用して、カスタム分散ワーカーのタスクを登録できます。ただし、ワーカーの完全修飾名と、各ディクショナリエントリの正しい名前と形式を知っている必要があります。エラーのリスクを減らすために、すべての分散ワーカーのカスタム ワーカー オプション ディクショナリを作成することをお勧めします。

カスタムワーカーオプションディクショナリを作成するには:

  • 次に示すように、DistributedWorkerOptionsDictionaryを継承するSampleDistributedWorkerOptionsDictionaryという名前のクラスを作成します。

using System.Collections.Generic;
using Newtonsoft.Json;
using Sitecore.Processing.Engine.Abstractions;

namespace Sitecore.Documentation.Examples
{
    public class SampleDistributedWorkerOptionsDictionary : DistributedWorkerOptionsDictionary
    {

        public SampleDistributedWorkerOptionsDictionary(string identifierSource) : base("Sitecore.Documentation.Examples.SampleDistributedWorker, Sitecore.Documentation.Examples", CreateDictionary(identifierSource))
        {

        }

        [JsonConstructor]
        protected SampleDistributedWorkerOptionsDictionary(IDictionary<string, string> dictionary) : base(dictionary)
        {
        }

        public static SampleDistributedWorkerOptionsDictionary Parse(IReadOnlyDictionary<string, string> options)
        {
            return new SampleDistributedWorkerOptionsDictionary(options["IdentifierSource"]);
        }

        private static IDictionary<string, string> CreateDictionary(string identifierSource)
        {
            var dictionary = new Dictionary<string, string>
            {
                {
                    "IdentifierSource", identifierSource
                }
            };

            return dictionary;
        }
    }
}

分散ワーカーオプションディクショナリを作成する場合:

  • IDictionary<string, string>パラメーターを受け入れるコンストラクタを含める必要があります。コンストラクタはJsonConstructor属性で修飾する必要があります。このコンストラクタがないと、処理エンジンはワーカーを実行しようとすると、"System.FormatException: JSONテキストを逆シリアル化できませんでした" というエラーをスローします。

  • 次のことができるヘルパー メソッドを作成することをお勧めします。

    • IReadOnlyDictionary<string, string>をオプション辞書に変換する(CreateDictionary())

    • オプション ディクショナリをIReadOnlyDictionary<string, string>に変換する (Parse())

分散ワーカー クラスの作成

分散ワーカーを作成するには:

  • 次に示すように、IDistributedWorkerを実装するSampleDistributedWorkerという名前のクラスを作成します。

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Logging;
    using Sitecore.Processing.Engine.Abstractions;
    using Sitecore.XConnect;
    
    namespace Sitecore.Documentation.Examples
    {
        public class SampleDistributedWorker : IDistributedWorker<Contact>
        {
            private readonly ILogger _logger;
            private readonly IServiceProvider _serviceProvider;
            private readonly SampleDistributedWorkerOptionsDictionary _options;
    
            public SampleDistributedWorker(ILogger logger, SampleDistributedWorkerOptionsDictionary options, IServiceProvider serviceProvider)
    
            {
                _logger = logger;
                _serviceProvider = serviceProvider;
                _options = options;
            }
    
            public SampleDistributedWorker(ILogger logger, IReadOnlyDictionary<string, string> options, IServiceProvider serviceProvider) : this (logger, SampleDistributedWorkerOptionsDictionary.Parse(options), serviceProvider)
            {
            }
    
            public async Task ProcessBatchAsync(IReadOnlyList<Contact> batch, CancellationToken token)
            {
                _logger.LogInformation($"Run Worker: {GetType().Name}");
    
                if (batch == null)
                {
                    throw new Exception("Batch is null.");
                }
    
                var contactsToUpdate = batch.Where(c => c.Identifiers.Any(x => x.Source == _options["IdentifierSource"]));
    
                using (IServiceScope scope = _serviceProvider.CreateScope())
                {
                    using (var xdbContext = scope.ServiceProvider.GetRequiredService<IXdbContext>())
                    {
                        foreach (var contact in contactsToUpdate)
                        {
                            xdbContext.RemoveContactIdentifier(new ContactReference(contact.Id.Value), contact.Identifiers.FirstOrDefault(x => x.Source == _options["IdentifierSource"]));
                        }
    
                        await xdbContext.SubmitAsync(token);
                    }
                }
    
                return;
            }
    
            public void Dispose()
            {
            }
        }
    }

分散ワーカーを作成する場合:

  • IReadOnlyDictionary<string, string> パラメーターを受け入れるコンストラクタが必要です。このコンストラクタを含めない場合、処理エンジンは起動せず、"System.InvalidOperationException: 型に適したコンストラクタが見つかりませんでした" というエラーが表示されます。

構成にワーカーを登録する

ワーカーをCortex Processing Engineに登録するには、次のようにします。

  1. C:\<PathToxConnect>\App_data\jobs\continuous\ProcessingEngine\App_Data\Config\Global\Processing\sc.Processing.Documentation.Examples.xmlという名前のファイルを作成し、次の設定を追加します。

    <Settings>
        <Sitecore>
            <Processing>
            <Services>
            <TaskServicesFactory>
                <Options>
                    <SampleDistributedWorker>
                    <Type>Sitecore.Documentation.Examples.SampleDistributedWorker, Sitecore.Documentation.Examples</Type>
                    </SampleDistributedWorker>
                </Options>
                </TaskServicesFactory>
            </Services>
            </Processing>
        </Sitecore>
    </Settings>
  2. 処理エンジンを再起動します。これは、構成の変更を登録するために必要です。

カスタム分散ワーカーを使用する

カスタムworkerをコア ロール環境 (Content Managementなど) で使用するには:

  • 次に示すように、SampleDistributedWorkerOptionsDictionaryクラスを使用して分散タスクを登録します。

    using System;
    using Microsoft.Extensions.DependencyInjection;
    using Sitecore.DependencyInjection;
    using Sitecore.Diagnostics;
    using Sitecore.Processing.Engine.Abstractions;
    
    namespace Sitecore.Documentation.Examples
    {
        public class Example
        {
            public async void Examples()
            {
                var _taskManager = ServiceLocator.ServiceProvider.GetRequiredService<ITaskManager>();
    
                Guid taskId = await _taskManager.RegisterDistributedTaskAsync(
                    new ContactDataSourceOptionsDictionary(new ContactExpandOptions(), 20, 20),
                    new SampleDistributedWorkerOptionsDictionary("RemoveMe"), null, TimeSpan.FromDays(1))
                    .ConfigureAwait(false);
            }
        }
    }
この記事を改善するための提案がある場合は、 お知らせください!