Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions src/Testing/CoreTests/Runtime/MessageContextTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ public async Task reschedule_without_native_scheduling()

theEnvelope.ScheduledTime.ShouldBe(scheduledTime);

await theContext.Storage.Inbox.Received().ScheduleJobAsync(theEnvelope);
await theContext.Storage.Inbox.Received().ScheduleExecutionAsync(theEnvelope);
}

[Fact]
Expand All @@ -130,11 +130,42 @@ public async Task reschedule_with_native_scheduling()

theEnvelope.ScheduledTime.ShouldBe(scheduledTime);

await theContext.Storage.Inbox.DidNotReceive().ScheduleJobAsync(theEnvelope);
await theContext.Storage.Inbox.DidNotReceive().ScheduleExecutionAsync(theEnvelope);
await callback.As<ISupportNativeScheduling>().Received()
.MoveToScheduledUntilAsync(theEnvelope, scheduledTime);
}

[Fact]
public async Task reschedule_convenience_method_with_DateTimeOffset()
{
var callback = Substitute.For<IChannelCallback>();
var scheduledTime = DateTime.Today.AddHours(8);

theContext.ReadEnvelope(theEnvelope, callback);

// Test the new convenience extension method
await theContext.RescheduleAsync(scheduledTime);

theEnvelope.ScheduledTime.ShouldBe(scheduledTime);
await theContext.Storage.Inbox.Received().ScheduleExecutionAsync(theEnvelope);
}

[Fact]
public async Task reschedule_convenience_method_with_TimeSpan()
{
var callback = Substitute.For<IChannelCallback>();
var delay = TimeSpan.FromHours(2);
var expectedTime = DateTimeOffset.UtcNow.Add(delay);

theContext.ReadEnvelope(theEnvelope, callback);

// Test the new convenience extension method with TimeSpan
await theContext.RescheduleAsync(delay);

theEnvelope.ScheduledTime.Value.ShouldBeInRange(expectedTime.AddSeconds(-1), expectedTime.AddSeconds(1));
await theContext.Storage.Inbox.Received().ScheduleExecutionAsync(theEnvelope);
}

[Fact]
public async Task move_to_dead_letter_queue_without_native_dead_letter()
{
Expand Down
26 changes: 26 additions & 0 deletions src/Wolverine/IMessageBus.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,32 @@ public static ValueTask ScheduleAsync<T>(this IMessageBus bus, T message, TimeSp
options.ScheduleDelay = delay;
return bus.PublishAsync(message, options);
}

/// <summary>
/// Reschedule the current message being handled to be executed at a later time.
/// This can only be used from within a message handler and will update the existing
/// envelope rather than creating a new one (avoiding duplicate key issues).
/// </summary>
/// <param name="context">The message context from within a handler</param>
/// <param name="scheduledTime">When the message should be executed</param>
/// <returns></returns>
public static Task RescheduleAsync(this IMessageContext context, DateTimeOffset scheduledTime)
{
return ((IEnvelopeLifecycle)context).ReScheduleAsync(scheduledTime);
}

/// <summary>
/// Reschedule the current message being handled to be executed after a delay.
/// This can only be used from within a message handler and will update the existing
/// envelope rather than creating a new one (avoiding duplicate key issues).
/// </summary>
/// <param name="context">The message context from within a handler</param>
/// <param name="delay">How long to delay before executing the message</param>
/// <returns></returns>
public static Task RescheduleAsync(this IMessageContext context, TimeSpan delay)
{
return ((IEnvelopeLifecycle)context).ReScheduleAsync(DateTimeOffset.UtcNow.Add(delay));
}
}

public interface ICommandBus
Expand Down
2 changes: 1 addition & 1 deletion src/Wolverine/Runtime/MessageContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public async Task ReScheduleAsync(DateTimeOffset scheduledTime)
}
else
{
await Storage.Inbox.ScheduleJobAsync(Envelope);
await Storage.Inbox.ScheduleExecutionAsync(Envelope);
}
}

Expand Down