/// <summary> /// Some additional properties from the TriggerFiredBundle /// There is a bug in RabbitMq.Client that prevents using the DateTimeOffset type in the headers /// These values are being serialized as ISO-8601 round trip string /// </summary> /// <param name="bundle"></param> static string CreatePayloadHeaderString(global::Quartz.IJobExecutionContext bundle) { var timeHeaders = new Dictionary <string, object> { { MessageHeaders.Quartz.Sent, bundle.FireTimeUtc } }; if (bundle.ScheduledFireTimeUtc.HasValue) { timeHeaders.Add(MessageHeaders.Quartz.Scheduled, bundle.ScheduledFireTimeUtc); } if (bundle.NextFireTimeUtc.HasValue) { timeHeaders.Add(MessageHeaders.Quartz.NextScheduled, bundle.NextFireTimeUtc); } if (bundle.PreviousFireTimeUtc.HasValue) { timeHeaders.Add(MessageHeaders.Quartz.PreviousSent, bundle.PreviousFireTimeUtc); } if (bundle.JobDetail.JobDataMap.TryGetValue("TokenId", out var tokenId)) { timeHeaders.Add(MessageHeaders.SchedulingTokenId, tokenId); } return(JsonConvert.SerializeObject(timeHeaders)); }
public async Task Execute(global::Quartz.IJobExecutionContext context) { var messageData = new ScheduledMessageData(); var jobData = new global::Quartz.JobDataMap(); jobData.PutAll(context.Scheduler.Context); jobData.PutAll(context.JobDetail.JobDataMap); jobData.PutAll(context.Trigger.JobDataMap); jobData.Put("PayloadMessageHeadersAsJson", CreatePayloadHeaderString(context)); SetObjectProperties(messageData, jobData); try { var bus = (IBus)context.Scheduler.Context[BUS_CONTEXT_KEY] ?? _bus; if (bus == null) { throw new Exception("Could not find MassTransit Bus instance on the Job or the Scheduler Context."); } var destinationAddress = messageData.Destination; var sourceAddress = bus.Address; IPipe <SendContext> sendPipe = CreateMessageContext(messageData, sourceAddress); var endpoint = await bus.GetSendEndpoint(destinationAddress).ConfigureAwait(false); var scheduled = new Scheduled(); await endpoint.Send(scheduled, sendPipe, context.CancellationToken).ConfigureAwait(false); LogContext.Debug?.Log("Schedule Executed: {Key} {Schedule}", context.JobDetail.Key, context.Trigger.GetNextFireTimeUtc()); } catch (Exception ex) { LogContext.Error?.Log(ex, "Failed to send scheduled message, type: {MessageType}, destination: {DestinationAddress}", messageData.MessageType, messageData.Destination); throw new global::Quartz.JobExecutionException(ex, context.RefireCount < 5); } }