Example #1
0
        /// <summary>
        /// Tries to create a new instance of IJob for the given JobDescriptor.
        /// </summary>
        /// <param name="descriptor">
        /// The JobDescriptor to create an instance of IJob for.
        /// </param>
        /// <returns>
        /// An instance of IJob that was created for the given JobDescriptor.
        /// </returns>
        public IJob CreateJob(JobDescriptor descriptor)
        {
            if (null == descriptor)
            {
                throw new ArgumentNullException("descriptor");
            }

            JobSpec spec;

            if (!nameToSpecMap.TryGetValue(descriptor.Job, out spec))
            {
                throw new UnknownJobException(descriptor.QueueMessageId, descriptor.Job);
            }

            try
            {
                IJob job = spec.CreateAndBind(descriptor.Properties);

                return(job);
            }
            catch (Exception ex)
            {
                throw new MessageFormatException(descriptor.QueueMessageId, ex);
            }
        }
Example #2
0
        /// <summary>
        /// Applies the FailedJobHandling behavior from the current ConsumerSettings.
        /// </summary>
        /// <param name="job">
        /// The IJob for which execution failed.
        /// </param>
        /// <param name="jobDesc">
        /// The JobDescriptor that describes the failed job.
        /// </param>
        /// <param name="exception">
        /// The Exception which was raised, or null if no exception was raised.
        /// </param>
        private void ApplyFailedJobHandling(IJob job, JobDescriptor jobDesc, Exception exception)
        {
            Debug.Assert(null != job, "The job must not be null.");
            Debug.Assert(null != jobDesc, "The job descriptor must not be null.");

            switch (consumerSettings.FailedJobHandling)
            {
            case FailedJobHandling.Requeue:
            case FailedJobHandling.RequeueThenDeleteAfterThreshold:
            case FailedJobHandling.Delete:
                ApplyFailedJobHandling(consumerSettings.FailedJobHandling);
                break;

            case FailedJobHandling.DecidePerJob:
                if (null == consumerSettings.FailedJobHandlingProvider)
                {
                    throw new InvalidOperationException("The FailedJobHandlingProvider must not be null when 'DecidePerJob' is used.");
                }

                ApplyFailedJobHandling(consumerSettings.FailedJobHandlingProvider(job, jobDesc, exception));
                break;

            default:
                throw new NotSupportedException("Unsupported FailedJobHandling: " + consumerSettings.FailedJobHandling);
            }
        }
Example #3
0
            /// <summary>
            /// Creates a JobDescriptor object for the given job.
            /// </summary>
            /// <param name="job">
            /// The IJob to create the JobDescriptor for.
            /// </param>
            /// <returns>
            /// An instance of JobDescriptor that describes the job.
            /// </returns>
            public JobDescriptor Describe(IJob job)
            {
                Debug.Assert(null != job, "The job must not be null.");
                JobDescriptor descriptor = new JobDescriptor();

                descriptor.Job = Name;
                Debug.Assert(null != Properties, "The properties dictionary must not be null.");

                if (0 < Properties.Count)
                {
                    descriptor.Properties = new Dictionary <string, JToken>(Properties.Count);

                    foreach (KeyValuePair <string, PropertyInfo> prop in Properties)
                    {
                        object val = prop.Value.GetValue(job);
                        JToken token;

                        if (null == val)
                        {
                            token = JValue.CreateNull();
                        }
                        else
                        {
                            token = JToken.FromObject(val);
                        }

                        descriptor.Properties.Add(prop.Key, token);
                    }
                }

                return(descriptor);
            }
Example #4
0
        /// <summary>
        /// Produces one message for the given job descriptor and adds it to the queue.
        /// </summary>
        /// <param name="descriptor">
        /// The JobDescriptor which describes the job to produce.
        /// </param>
        /// <param name="initialVisibilityDelay">
        /// A TimeSpan value specifying the interval of time from now during which the message will be invisible. If
        /// null then the message will be visible immediately.
        /// </param>
        public void One(JobDescriptor descriptor, TimeSpan?initialVisibilityDelay)
        {
            if (null == descriptor)
            {
                throw new ArgumentNullException("descriptor");
            }
            else if (String.IsNullOrWhiteSpace(descriptor.Job))
            {
                throw new ArgumentException("The JobDescriptor must have a non-null and non-blank Job property.");
            }

            string            body = JsonConvert.SerializeObject(descriptor);
            CloudQueueMessage msg  = new CloudQueueMessage(body);

            queue.AddMessageAsync(msg, /* timeToLive */ null, initialVisibilityDelay, null, null).GetAwaiter().GetResult();
        }
Example #5
0
        /// <summary>
        /// Tries to create a JobDescriptor for the given job.
        /// </summary>
        /// <param name="job">
        /// The IJob object to create a descriptor for.
        /// </param>
        /// <returns>
        /// An instance of JobDescriptor that describes the job.
        /// </returns>
        public JobDescriptor CreateDescriptor(IJob job)
        {
            if (null == job)
            {
                throw new ArgumentNullException("job");
            }

            JobSpec spec;

            if (!typeToSpecMap.TryGetValue(job.GetType(), out spec))
            {
                throw new UnknownJobException(null, job.GetType().Name);
            }

            JobDescriptor descriptor = spec.Describe(job);

            return(descriptor);
        }
Example #6
0
        /// <summary>
        /// Tries to create a new instance of IJob for the given JobDescriptor.
        /// </summary>
        /// <param name="descriptor">
        /// The JobDescriptor to create an instance of IJob for.
        /// </param>
        /// <returns>
        /// An instance of IJob that was created for the given JobDescriptor.
        /// </returns>
        public IJob CreateJob(JobDescriptor descriptor)
        {
            if (null == descriptor)
            {
                throw new ArgumentNullException("descriptor");
            }

            JobSpec spec;

            if (!nameToSpecMap.TryGetValue(descriptor.Job, out spec))
            {
                throw new UnknownJobException(descriptor.QueueMessageId, descriptor.Job);
            }

            IJob job = spec.CreateAndBind(descriptor.Properties);

            return job;
        }
Example #7
0
 /// <summary>
 /// Produces one message for the given job descriptor and adds it to the queue.
 /// </summary>
 /// <param name="descriptor">
 /// The JobDescriptor which describes the job to produce.
 /// </param>
 public void One(JobDescriptor descriptor)
 {
     One(descriptor, null);
 }
Example #8
0
        /// <summary>
        /// Produces one message for the given job descriptor and adds it to the queue.
        /// </summary>
        /// <param name="descriptor">
        /// The JobDescriptor which describes the job to produce.
        /// </param>
        /// <param name="initialVisibilityDelay">
        /// A TimeSpan value specifying the interval of time from now during which the message will be invisible. If
        /// null then the message will be visible immediately.
        /// </param>
        public void One(JobDescriptor descriptor, TimeSpan? initialVisibilityDelay)
        {
            if (null == descriptor)
            {
                throw new ArgumentNullException("descriptor");
            }
            else if (String.IsNullOrWhiteSpace(descriptor.Job))
            {
                throw new ArgumentException("The JobDescriptor must have a non-null and non-blank Job property.");
            }

            string body = JsonConvert.SerializeObject(descriptor);
            CloudQueueMessage msg = new CloudQueueMessage(body);

            queue.AddMessage(msg, /* timeToLive */ null, initialVisibilityDelay);
        }
Example #9
0
 /// <summary>
 /// Produces one message for the given job descriptor and adds it to the queue.
 /// </summary>
 /// <param name="descriptor">
 /// The JobDescriptor which describes the job to produce.
 /// </param>
 public void One(JobDescriptor descriptor)
 {
     One(descriptor, null);
 }
Example #10
0
            /// <summary>
            /// Creates a JobDescriptor object for the given job.
            /// </summary>
            /// <param name="job">
            /// The IJob to create the JobDescriptor for.
            /// </param>
            /// <returns>
            /// An instance of JobDescriptor that describes the job.
            /// </returns>
            public JobDescriptor Describe(IJob job)
            {
                Debug.Assert(null != job, "The job must not be null.");
                JobDescriptor descriptor = new JobDescriptor();

                descriptor.Job = Name;
                Debug.Assert(null != Properties, "The properties dictionary must not be null.");

                if (0 < Properties.Count)
                {
                    descriptor.Properties = new Dictionary<string, JToken>(Properties.Count);

                    foreach (KeyValuePair<string, PropertyInfo> prop in Properties)
                    {
                        object val = prop.Value.GetValue(job);
                        JToken token;

                        if (null == val)
                        {
                            token = JValue.CreateNull();
                        }
                        else
                        {
                            token = JToken.FromObject(val);
                        }

                        descriptor.Properties.Add(prop.Key, token);
                    }
                }

                return descriptor;
            }
Example #11
0
        /// <summary>
        /// Applies the UnknownJobHandling behavior from the current ConsumerSettings.
        /// </summary>
        /// <param name="jobDesc">
        /// The JobDescriptor that describes the unknown job.
        /// </param>
        private void ApplyUnknownJobHandling(JobDescriptor jobDesc)
        {
            Debug.Assert(null != jobDesc, "The job descriptor must not be null.");

            switch (consumerSettings.UnknownJobHandling)
            {
                case UnknownJobHandling.Requeue:
                case UnknownJobHandling.Delete:
                    ApplyUnknownJobHandling(consumerSettings.UnknownJobHandling);
                    break;

                case UnknownJobHandling.DedicePerJob:
                    if (null == consumerSettings.UnknownJobHandlingProvider)
                    {
                        throw new InvalidOperationException("The UnknownJobHandlingProvider must not be null when 'DedicePerJob' is used.");
                    }

                    ApplyUnknownJobHandling(consumerSettings.UnknownJobHandlingProvider(jobDesc));
                    break;

                default:
                    throw new NotSupportedException("Unsupported UnknownJobHandling: " + consumerSettings.UnknownJobHandling);
            }
        }