Problem

You want to switch between both the embedded job scheduler and the cluster job scheduler.

Solution

Use the factory pattern to switch between job schedulers. Create a method that returns a IJobScheduler interface. In the rest of the application, use the common IJobScheduler interface as much as you can.

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

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

                // If you need to... you can also try to upcast to ClusterJobScheduler
                ClusterJobScheduler asClusterScheduler = scheduler as ClusterJobScheduler;
                if (asClusterScheduler != null)
                {
                    Console.WriteLine("We are using the cluster scheduler");
                }
            }
        }

        // In your factory method, return the IJobScheduler.
        private static IJobScheduler GetJobScheduler(string[] args)
        {
            if (args.Length > 0 && args[0] == "local")
            {
                return GetEmbeddedJobScheduler();
            }
            else
            {
                return GetClusterJobScheduler();
            }
        }

        private static IJobScheduler GetEmbeddedJobScheduler()
        {
            return new EmbeddedJobScheduler();
        }

        private static IJobScheduler GetClusterJobScheduler()
        {
            return new ClusterJobScheduler("localhost");
        }
    }
}

Discussion

Switching between ClusterJobScheduler and EmbeddedJobScheduler could be useful during development. For example, the EmbeddedJobScheduler might be faster to test and debug, but the ClusterJobScheduler allows the use of a cluster. Easily switching between the two options can yield the best of both worlds.

To maximize the portability of an application, use the IJobScheduler interface as much as possible. When features only available on the ClusterJobScheduler are needed, check the type of the scheduler object and upcast it to the ClusterJobScheduler class.

See Also