Click or drag to resize

Send email when task encounters error.

Problem

You want to send an email to a System Administrator when an error occurs.

Solution

Configure the AGI.Parallel.Host.exe.config, AGI.Parallel.Host_x86.exe.config, AGI.Parallel.Host_WkGc.exe.config, or AGI.Parallel.Host_WkGc_x86.exe.config file to set up the required NLog xml section. For the cluster scheduler, the default location is C:\Program Files\AGI\STK Parallel Computing Server 2.9\Agent\bin\AGI.Parallel.Host.exe.config. The file will need write permissions. Add the target XML element as described in the example code. Once that is done, an email will be sent every time a task logs an error message.

Tip Tip

This same pattern can be used to send emails when an error occurs on the agent and the coordinator.

C#
using System;
using AGI.Parallel.Client;
using AGI.Parallel.Infrastructure;
using AGI.Parallel.Infrastructure.Logging;

namespace CodeSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            /*
             * First, edit your AGI.Parallel.Host.exe.config to add a SMTP Mail target.
             * This is an example of the NLog xml element.
             * Documentation for the mail target can be found at https://github.com/nlog/NLog/wiki/Mail-target
             * Other Config examples can be found at http://nlog-project.org/config
             *

<nlog>
    <targets>
        <target type="Mail" name="MailTarget" 
            to="to@domain.com" from="from@domain.com"
            subject="test logging message" 
            smtpServer="SMTPServer.domain.com"
            layout="${newline}${longdate} ${pad:padding=-5:inner=${level:uppercase=true}} - ${message}/>
    </targets>
    <rules>
        <logger name="*" minlevel="ERROR" writeTo="MailTarget" />
    </rules>
</nlog>
             */

            using (IJobScheduler scheduler = new ClusterJobScheduler("localhost"))
            {
                scheduler.Connect();
                Job job = scheduler.CreateJob();
                job.AddTask(new TaskThatWritesToLoggingError());
                job.Submit();
                job.WaitUntilDone();
            }
        }

        [Serializable]
        public class TaskThatWritesToLoggingError : Task
        {
            public override void Execute()
            {
                ILog log = this.GetProperty<ILog>(TaskProperties.Logger);
                log.Error("This could be sent to your email");
            }
        }
    }
}
Discussion

Logging and email can be important for being notified when a task fails or a certain event happens in the system. Internally, NLog is used for logging. Thus, all of the features and properties that NLog provides are available for use.

Use NLog's MailTarget to email a user when a task fails. For more examples of different NLog logging examples, see this page: http://nlog-project.org/config.

STK Parallel Computing Server 2.9 API for .NET