Problem

You want your task to run only on a specified list of machines.

Solution

Use task preconditions to control how a task is assigned. Many options can be controlled, including agent hostname, number of available cores, and hard disk space.

Use the TaskPreconditions method to add task preconditions to a job. The CommonResources enumeration contains the most frequently used resource types.

Caution

This feature is not available on the embedded scheduler.

C#  Copy imageCopy
using System;
using System.Collections.Generic;
using AGI.Parallel.Client;
using AGI.Parallel.Infrastructure;

namespace CodeSamples
{
    class Program
    {
        static void Main(string[] args)
        {
            using (IJobScheduler scheduler = new ClusterJobScheduler("localhost"))
            {
                scheduler.Connect();

                // Check if this feature is supported by querying for the IEnforceTaskPrecondition interface.
                IEnforceTaskPrecondition preconditionCapable = scheduler as IEnforceTaskPrecondition;
                if (preconditionCapable != null)
                {
                    IList<AgentSnapshot> agents = ((IGetAgentInfo)preconditionCapable).GetAgentInfo();
                    string agentMachineName = agents[0].MachineName;

                    Job job = scheduler.CreateJob();

                    // Restrict the task to be run only on the first agent machine.
                    job.TaskPreconditions.Add(CommonResources.Hostname, Operator.MemberOf, new string[] { agentMachineName });

                    // Restrict the task to be run on machines that have at least 1 empty host slot.
                    job.TaskPreconditions.Add(CommonResources.AvailableCores, Operator.GreaterThan, 0);

                    job.AddTask(new RestrictedTask());

                    job.Submit();
                    job.WaitUntilDone();

                    Console.WriteLine(job.Tasks[0].Result);
                }
                else
                {
                    Console.WriteLine("This job scheduler does not support task preconditions.");
                }
            }

            /*
             * The output of the application should resemble:
             * Hello from MyComputerName
             */
        }

        [Serializable]
        public class RestrictedTask : Task
        {
            public override void Execute()
            {
                this.Result = "Hello from " + Environment.MachineName;
            }
        }
    }
}

Discussion

It is a common pattern to execute tasks only on a restricted set of machines. Examples include:

  • Forcing the task to be executed only on a predefined set of machines.
  • Reserving system resources on a machine; for example: only use 2 cores of a 4 core machine.
  • Executing a task only if it has a specific resource, for example: only use machines that have STK installed.
  • Executing a task only if it has enough memory or hard disk space.

A helpful method to use in combination with TaskPreconditions is the GetAgentInfo. GetAgentInfo will return a list of agents, which can be used to determine currently available resources before tasks are submitted.

See Also