public static HangfireScheduledMessageData Create(ConsumeContext <ScheduleMessage> context) { var message = new HangfireScheduledMessageData(); SetBaseProperties(message, context, context.Message.Destination, context.Message.CorrelationId); return(message); }
public async Task Consume(ConsumeContext <ScheduleMessage> context) { var correlationId = context.Message.CorrelationId.ToString("N"); var message = HangfireScheduledMessageData.Create(context); using var connection = _jobStorage.GetConnection(); using var transaction = connection.CreateWriteTransaction(); if (TryRemoveJob(connection, correlationId, out var jobId)) { LogContext.Debug?.Log("Canceled Scheduled Message: {Id}", jobId); } jobId = _backgroundJobClient.Schedule <ScheduleJob>( x => x.SendMessage(message, null), context.Message.ScheduledTime); connection.SetRangeInHash(correlationId, new[] { new KeyValuePair <string, string>(JobIdKey, jobId) }); LogContext.Debug?.Log("Scheduled: {Id}", jobId); }
protected static void SetBaseProperties(HangfireScheduledMessageData message, ConsumeContext context, Uri destination, Guid?tokenId = default) { message.DestinationAddress = destination?.ToString() ?? ""; message.Body = ExtractBody(context.ReceiveContext.ContentType?.MediaType, context.ReceiveContext.GetBody(), destination); message.ContentType = context.ReceiveContext.ContentType?.MediaType; message.FaultAddress = context.FaultAddress?.ToString() ?? ""; message.ResponseAddress = context.ResponseAddress?.ToString() ?? ""; if (context.MessageId.HasValue) { message.MessageId = context.MessageId.Value.ToString(); } if (context.CorrelationId.HasValue) { message.CorrelationId = context.CorrelationId.Value.ToString(); } if (context.ConversationId.HasValue) { message.ConversationId = context.ConversationId.Value.ToString(); } if (context.InitiatorId.HasValue) { message.InitiatorId = context.InitiatorId.Value.ToString(); } if (context.RequestId.HasValue) { message.RequestId = context.RequestId.Value.ToString(); } if (context.ExpirationTime.HasValue) { message.ExpirationTime = context.ExpirationTime.Value.ToString("O"); } IEnumerable <KeyValuePair <string, object> > headers = context.Headers.GetAll(); if (headers.Any()) { message.HeadersAsJson = JsonConvert.SerializeObject(headers); } }
public async Task SendMessage(HangfireScheduledMessageData messageData, PerformContext performContext) { try { IPipe <SendContext> sendPipe = CreateMessageContext(messageData, _bus.Address); var endpoint = await _bus.GetSendEndpoint(messageData.Destination).ConfigureAwait(false); var scheduled = new Scheduled(); await endpoint.Send(scheduled, sendPipe, performContext.CancellationToken.ShutdownToken).ConfigureAwait(false); LogContext.Debug?.Log("Schedule Executed: {JobId}, created at: {CreatedAt}", performContext.BackgroundJob.Id, performContext.BackgroundJob.CreatedAt); } catch (Exception ex) { LogContext.Error?.Log(ex, "Failed to send scheduled message: {JobId}, created at: {CreatedAt}, destination: {DestinationAddress}", performContext.BackgroundJob.Id, messageData.Destination, performContext.BackgroundJob.CreatedAt); throw new JobPerformanceException("Job Execution exception", ex); } }