1. エージェント

エージェントの作成

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

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

BaseAgentまたはRecurringAgentを継承するエージェントを作成でき、エージェントをスケーリングするかどうかを選択できます。カスタムエージェントを作成する理由には、次のようなものがあります。

  • エンジンが依存しているデータベースから期限切れのデータをパージするなど、エンジン内で定期的にスケジュールされたメンテナンスを実行します。デフォルトでは、処理エンジンには、メンテナンスタスクを実行する2つのエージェントが含まれています。

メモ

カスタムエージェントを作成する必要が生じることはめったにありません。多くの場合、デフォルトのTaskAgentによって実行されるカスタム処理ワーカーを作成します。

RecurringAgentを継承するエージェントを作成する

次のサンプル エージェントは、偽のストレージ プロバイダーのデータをクリーンアップするメソッドを呼び出します。ストレージ プロバイダーは、ICustomProviderインターフェイスによって表されます。エージェントはRecurringAgentを継承します。つまり、エージェントは設定可能な間隔で定期的に実行されます。

エージェント クラスを作成する

エージェントクラスを作成するには:

  • クラスを作成し、次のサンプル エージェントをコピーします。

手記

サンプルのICustomProviderクラスとCustomProviderクラスは、便宜上、同じファイルに含まれています。

using System.Threading;
using System.Threading.Tasks;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Sitecore.Framework.Conditions;
using Sitecore.Processing.Engine.Abstractions;
using Sitecore.Processing.Engine.Agents;

namespace Sitecore.Documentation.Examples
{
    /// <summary>
    ///     Provides basic agent functionality and services.
    /// </summary>
    public class SampleCleanupAgent : RecurringAgent
    {
        private readonly ICustomProvider _customProvider;

        /// <summary>
        ///
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="options"></param>
        /// <param name="customProvider"></param>
        protected SampleCleanupAgent(ILogger<IAgent> logger, IConfiguration options, ICustomProvider customProvider) : base(options, logger)
        {
            Condition.Requires(customProvider, nameof(customProvider)).IsNotNull();

            _customProvider = customProvider;
        }

        /// <summary>
        ///
        /// </summary>
        /// <param name="token"></param>
        /// <returns></returns>
        protected override async Task RecurringExecuteAsync(CancellationToken token)
        {
            // Checks if cancellation has been requested in case task is long-running
            // It is your responsibility to check the status of the cancellation token
            while (!token.IsCancellationRequested)
            {
                await _customProvider.CleanData().ConfigureAwait(false);
            }
        }
    }

    /// <summary>
    ///
    /// </summary>
    public interface ICustomProvider {
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        Task GetData();
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        Task CleanData();
    }

    /// <summary>
    ///
    /// </summary>
    public class CustomProvider : ICustomProvider
    {
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public async Task CleanData()
        {
            // Clean up data in custom data provider
            await Task.Delay(1).ConfigureAwait(false);
        }

        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public async Task GetData()
        {
            // Get data from custom data provider
            await Task.Delay(1).ConfigureAwait(false);
        }
    }
}

エージェントとサービスを構成に登録する

  1. scという名前のファイルを作成します。<engine-root>\\App_Data\\Config\\GlobalでProcessing.Engine.CustomAgents.xml。

  2. 図のようにSampleCleanupAgentICustomProviderを登録します。この例では、単純な構成を使用して1つのエージェントを登録します。

    <Settings>
        <Sitecore>
            <Processing>
            <Services>
                <ICustomProvider>
                    <Type>Sitecore.Documentation.Examples.CustomProvider, Sitecore.Documentation.Examples</Type>
                    <As>Sitecore.Documentation.Examples.ICustomProvider, Sitecore.Documentation.Examples</As>
                    <LifeTime>Transient</LifeTime>
                </ICustomProvider>
                <SampleCleanupAgent>
                    <Type>Sitecore.Documentation.Examples.SampleCleanupAgent, Sitecore.Documentation.Examples</Type>
                    <As>Sitecore.Processing.Engine.Abstractions.IAgent, Sitecore.Processing.Engine.Abstractions</As>
                    <LifeTime>Transient</LifeTime>
                    <Options>
                        <!-- The period which the agent sleeps before running again. -->
                        <SleepPeriod>0.00:00:14.000</SleepPeriod>
                    </Options>
                </SampleCleanupAgent>
            </Services>
            </Processing>
        </Sitecore>
    </Settings>

または、エージェントをスケーリングする必要がある場合は、ParallelAgentsConfigurationを使用して登録します。

    <Settings>
        <Sitecore>
            <Processing>
            <Services>
                <ICustomProvider>
                    <Type>Sitecore.Documentation.Examples.CustomProvider, Sitecore.Documentation.Examples</Type>
                    <As>Sitecore.Documentation.Examples.ICustomProvider, Sitecore.Documentation.Examples</As>
                    <LifeTime>Transient</LifeTime>
                </ICustomProvider>
                <SampleCleanupAgent>
                    <Type>Sitecore.Processing.Engine.Agents.ParallelAgentsConfiguration, Sitecore.Processing.Engine</Type>
                    <As>Sitecore.XConnect.DependencyInjection.Abstractions.IXConnectServicesConfiguration, Sitecore.XConnect.DependencyInjection</As>
                    <LifeTime>Transient</LifeTime>
                    <Options>
                        <AgentFixedCount>0</AgentFixedCount>
                        <AgentCoreRatio>0.75</AgentCoreRatio>
                        <AgentConfiguration>
                            <Type>Sitecore.Documentation.Examples.SampleCleanupAgent, Sitecore.Documentation.Examples</Type>
                            <As>Sitecore.Processing.Engine.Abstractions.IAgent, Sitecore.Processing.Engine.Abstractions</As>
                            <LifeTime>Transient</LifeTime>
                            <Options>
                                <SleepPeriod>0.00:00:14.000</SleepPeriod>
                            </Options>
                        </AgentConfiguration>
                    </Options>
                </SampleCleanupAgent>
            </Services>
            </Processing>
        </Sitecore>
    </Settings>

BaseAgentを継承するエージェントを作成する

次のサンプル エージェントは、起動時にログにメッセージを書き込み、1ミリ秒待機します。エージェントはRecurringAgentを継承せず、一定の間隔で実行されません。

大事な

この特定のエージェントは、エンジンの起動時に1回実行されます。ただし、エージェントは特定のイベントが発生するまでスリープし、イベントが再び発生するまで再びスリープする場合があります。エージェントは技術的には「繰り返し」ますが、定期的にスケジュールされた間隔ではありません。

エージェント クラスを作成する

エージェントクラスを作成するには:

  • クラスを作成し、次のサンプル エージェントをコピーします。

    using System.Threading;
    using System.Threading.Tasks;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.Logging;
    using Sitecore.Processing.Engine.Abstractions;
    using Sitecore.Processing.Engine.Agents;
    
    namespace Sitecore.Documentation.Examples
    {
        /// <summary>
        ///     Provides basic agent functionality and services.
        /// </summary>
        public class SampleLogAgent : BaseAgent
        {
            /// <summary>
            ///
            /// </summary>
            /// <param name="logger"></param>
            /// <param name="options"></param>
            protected SampleLogAgent(ILogger<IAgent> logger, IConfiguration options) : base(logger, options)
            {
            }
    
            /// <summary>
            ///
            /// </summary>
            /// <param name="token"></param>
            /// <returns></returns>
            public override async Task ExecuteAsync(CancellationToken token)
            {
                    // Write a custom log message
                    //  No cancellation token check in this example as task is unlikely to be long-running
                    base.Log.LogInformation("Your custom agent has started.");
                    await Task.Delay(1).ConfigureAwait(false);
            }
        }
    }

エージェントを構成に登録する

  1. scという名前のファイルを作成します。<engine-root>\\App_Data\\Config\\GlobalでProcessing.Engine.CustomAgents.xml。

  2. 図のようにSampleCleanupAgentを登録します。この例では、単純な構成を使用して1つのエージェントを登録します。

        <Settings>
            <Sitecore>
                <Processing>
                <Services>
                    <SampleLogAgent>
                        <Type>Sitecore.Documentation.Examples.SampleLogAgent, Sitecore.Documentation.Examples</Type>
                        <As>Sitecore.Processing.Engine.Abstractions.IAgent, Sitecore.Processing.Engine.Abstractions</As>
                        <LifeTime>Transient</LifeTime>
                    </SampleLogAgent>
                </Services>
                </Processing>
            </Sitecore>
        </Settings>

タスクのキャンセル

すべて 長時間実行される可能性のあるエージェントを実装する場合は、ExecuteAsyncメソッド (BaseAgentを継承するエージェントの場合) またはRecurringExecuteAsyncメソッド (RecurringAgentを継承するエージェント) でtoken.IsCancellationRequestedプロパティを確認してください。

この記事を改善するための提案がある場合は、 お知らせください!