public void TestWriteToXmlAndReadFromXmlOnJobConfiguration()
        {
            DependencyResolver.Reset();
            DependencyResolver.Initialize();

            var jobConfiguration = DummyJobConfiguration();

            var firstWrite = new XmlDocument();
            firstWrite.AppendChild(firstWrite.CreateElement("JobConfiguration"));
            jobConfiguration.WriteTo(firstWrite.DocumentElement);

            var anotherJobConfiguration = new JobConfiguration();
            anotherJobConfiguration.ReadFrom(firstWrite.DocumentElement);

            var secondWrite = new XmlDocument();
            secondWrite.AppendChild(secondWrite.CreateElement("JobConfiguration"));
            anotherJobConfiguration.WriteTo(secondWrite.DocumentElement);

            var firstString = new StringBuilder();
            var firstText = XmlWriter.Create(firstString);
            firstWrite.Save(firstText);

            var secondString = new StringBuilder();
            var secondText = XmlWriter.Create(secondString);
            secondWrite.Save(secondText);

            secondString.ToString().Should().BeEquivalentTo(firstString.ToString());
        }
 internal static JobConfiguration DummyJobConfiguration()
 {
     var jobConfiguration = new JobConfiguration
     {
         RequestMaxAttempts = 9,
         RequestTimeout = TimeSpan.FromMilliseconds(999),
         JobAndWorkerType =
         {
             JobType = RuntimeType.From(typeof (DummyJob)),
             WorkerType = RuntimeType.From(typeof (DummyWorker))
         }
     };
     return jobConfiguration;
 }
        public static DispatcherSettings Build()
        {
            var settings = new DispatcherSettings();

            var configuration = new JobConfiguration
            {
                JobAndWorkerType =
                {
                    JobType = RuntimeType.From(typeof (DummyJob)),
                    WorkerType = RuntimeType.From(typeof (DummyWorker))
                },
                RequestMaxAttempts = 3,
                RequestTimeout = TimeSpan.FromSeconds(10)
            };

            settings.JobConfigurations.Add(configuration);

            settings.IntervalForCheckingUnfinishedJobs = TimeSpan.FromMilliseconds(800);

            return settings;
        }
 private static DispatcherSettings ConfigureDispatcherFor(string dispatcherId)
 {
     var dispatcherSettings = new DispatcherSettings
     {
         DispatcherId = DispatcherId.FromString(dispatcherId),
         DispatcherLifeSpan = new DispatcherLifeSpan
         {
             Mode = DispatcherLifeSpanMode.UntilTimedOut,
             Timeout = TimeSpan.FromSeconds(30)
         },
         IntervalForCheckingUnfinishedJobs = TimeSpan.FromMilliseconds(800),
         MaximumNumberOfProcessingJobs = 1,
     };
     var jobConfiguration = new JobConfiguration
     {
         JobAndWorkerType =
         {
             JobType = RuntimeType.From(typeof (SampleJob)),
             WorkerType = RuntimeType.From(typeof (SampleWorker))
         },
         RequestMaxAttempts = 3,
         RequestTimeout = TimeSpan.FromSeconds(10)
     };
     dispatcherSettings.JobConfigurations.Add(jobConfiguration);
     return dispatcherSettings;
 }
        internal void ConfigureSubscriptionForDummyJobForMultipleDispatchers()
        {
            foreach (var settings in ListOfSettingsForMultipleDispatchers)
            {
                var configuration = new JobConfiguration
                {
                    JobAndWorkerType =
                    {
                        JobType = RuntimeType.From(typeof(DummyJob)),
                        WorkerType = RuntimeType.From(typeof(DummyWorker))
                    },
                    RequestMaxAttempts = 2,
                    RequestTimeout = TimeSpan.FromSeconds(5 * ListOfSettingsForMultipleDispatchers.Count)
                };

                settings.JobConfigurations.Add(configuration);
            }
        }
        internal void ConfigureSubscriptionForAnotherDummyJobOnAnotherWorkDispatcher()
        {
            var configuration = new JobConfiguration
            {
                JobAndWorkerType = { JobType = RuntimeType.From(typeof(AnotherDummyJob)), WorkerType = RuntimeType.From(typeof(DummyWorker)) },
                RequestMaxAttempts = 2,
                RequestTimeout = TimeSpan.FromSeconds(10)
            };

            AnotherSettings.JobConfigurations.Add(configuration);
        }
        private void SubscribeToPendingJob(JobConfiguration configuration, Action<Job> receiveJob)
        {
            if ((configuration.JobAndWorkerType.JobType.Type != typeof(Job)) &&
                (!configuration.JobAndWorkerType.JobType.Type.IsSubclassOf(typeof(Job))))
                throw new ArgumentException("MessageType must be a subtype of Job");

            // Will block when receive a work request and receive another only after the first is processed.
            // If it fails, the work request will return to the pending queue to be retried

            SubscriptionBus.SubscribeTo(
                configuration.JobAndWorkerType.JobType.Type,
                JobStatus.Pending.TopicId(),
                PendingJobsSubscriptionId,
                message => receiveJob((Job)message));
        }
        private static void ApplyConfigurationToJob(Job job, JobConfiguration configuration)
        {
            job.RequestMaxAttempts = configuration.RequestMaxAttempts;
            job.RequestTimeout = configuration.RequestTimeout;
            job.ProcessingAttempts += 1;

            job.RequestMaxTime = configuration.RequestTimeout == Timeout.InfiniteTimeSpan
                ? Timeout.InfiniteTimeSpan
                : TimeSpan.FromSeconds(configuration.RequestTimeout.TotalSeconds * configuration.RequestMaxAttempts);

            job.WorkerType = configuration.JobAndWorkerType.WorkerType.Type;
        }
 public void ConfigureDispatcherToHandleJob(string dispatcherId, JobAndWorkerType jobAndWorkerType, DispatcherLifeSpan dispatcherLifeSpan)
 {
     var dispatcherSettings = DispatcherSettings.Single(c => c.DispatcherId.Value == dispatcherId);
     dispatcherSettings.DispatcherLifeSpan = dispatcherLifeSpan;
     if (dispatcherSettings.JobConfigurations.Any(
         c => c.JobAndWorkerType.JobType == jobAndWorkerType.JobType)) return;
     var jobConfiguration = new JobConfiguration
     {
         JobAndWorkerType =
         {
             JobType = jobAndWorkerType.JobType,
             WorkerType = jobAndWorkerType.WorkerType
         }
     };
     dispatcherSettings.JobConfigurations.Add(jobConfiguration);
 }