/// <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); } }
/// <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); } }
/// <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); }
/// <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(); }
/// <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); }
/// <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; }
/// <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); }
/// <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); }
/// <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; }
/// <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); } }