Skip to content

Commit

Permalink
Adjusted PassageOfTime example extending by the granular time events
Browse files Browse the repository at this point in the history
  • Loading branch information
oskardudycz committed Jun 21, 2024
1 parent d926ede commit 4ca54f5
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 17 deletions.
45 changes: 40 additions & 5 deletions Core.Scheduling/PassageOfTimeJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,46 @@

namespace Core.Scheduling;

using static TimeHasPassed;

public class PassageOfTimeJob(IEventBus eventBus, TimeProvider timeProvider): IJob
{
public Task Execute(IJobExecutionContext context) =>
eventBus.Publish(
EventEnvelope.From(new TimeHasPassed(timeProvider.GetUtcNow(), context.PreviousFireTimeUtc)),
CancellationToken.None
);
public Task Execute(IJobExecutionContext context)
{
var timeUnit = context.MergedJobDataMap.GetString("timeUnit")!.ToTimeUnit();
var @event = timeUnit.ToEvent(timeProvider.GetUtcNow(), context.PreviousFireTimeUtc);

return eventBus.Publish(EventEnvelope.From(@event), CancellationToken.None);
}
}

public enum TimeUnit
{
Minute,
Hour,
Day
}

public static class TimeUnitExtensions
{
public static TimeUnit ToTimeUnit(this string timeUnitString) =>
Enum.Parse<TimeUnit>(timeUnitString);

public static TimeSpan ToTimeSpan(this TimeUnit timeUnit) =>
timeUnit switch
{
TimeUnit.Minute => TimeSpan.FromMinutes(1),
TimeUnit.Hour => TimeSpan.FromHours(1),
TimeUnit.Day => TimeSpan.FromDays(1),
_ => throw new ArgumentOutOfRangeException(nameof(timeUnit), $"Not expected time unit value: {timeUnit}")
};

public static TimeHasPassed ToEvent(this TimeUnit timeUnit, DateTimeOffset now, DateTimeOffset? previous) =>
timeUnit switch
{
TimeUnit.Minute => new MinuteHasPassed(now, previous),
TimeUnit.Hour => new HourHasPassed(now, previous),
TimeUnit.Day => new DayHasPassed(now, previous),
_ => throw new ArgumentOutOfRangeException(nameof(timeUnit), $"Not expected time unit value: {timeUnit}")
};
}
16 changes: 8 additions & 8 deletions Core.Scheduling/QuartzExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,12 @@ namespace Core.Scheduling;

public static class QuartzExtensions
{
public static IServiceCollection AddQuartzDefaults(
this IServiceCollection services,
TimeSpan? passageOfTimeInterval = null
) =>
public static IServiceCollection AddQuartzDefaults(this IServiceCollection services) =>
services
.AddQuartz(q => q
.AddPassageOfTime(passageOfTimeInterval ?? TimeSpan.FromMinutes(1))
.AddPassageOfTime(TimeUnit.Minute)
.AddPassageOfTime(TimeUnit.Hour)
.AddPassageOfTime(TimeUnit.Day)
// .UsePersistentStore(x =>
// {
// x.UseProperties = false;
Expand All @@ -23,16 +22,17 @@ public static IServiceCollection AddQuartzDefaults(

public static IServiceCollectionQuartzConfigurator AddPassageOfTime(
this IServiceCollectionQuartzConfigurator q,
TimeSpan interval
TimeUnit timeUnit
)
{
var jobKey = new JobKey($"PassageOfTimeJob_{interval}");
var jobKey = new JobKey($"PassageOfTimeJob_{timeUnit}");
q.AddJob<PassageOfTimeJob>(opts => opts.WithIdentity(jobKey));

q.AddTrigger(opts => opts
.ForJob(jobKey)
.WithIdentity($"{jobKey}-trigger")
.WithSimpleSchedule(x => x.WithInterval(interval))
.UsingJobData("timeUnit", timeUnit.ToString())
.WithSimpleSchedule(x => x.WithInterval(timeUnit.ToTimeSpan()))
);

return q;
Expand Down
7 changes: 6 additions & 1 deletion Core/Events/TimeHasPassed.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
namespace Core.Events;


public record TimeHasPassed(DateTimeOffset Now, DateTimeOffset? PreviousTime);
public abstract record TimeHasPassed(DateTimeOffset Now, DateTimeOffset? PreviousTime)
{
public record MinuteHasPassed(DateTimeOffset Now, DateTimeOffset? PreviousTime): TimeHasPassed(Now, PreviousTime);
public record HourHasPassed(DateTimeOffset Now, DateTimeOffset? PreviousTime): TimeHasPassed(Now, PreviousTime);
public record DayHasPassed(DateTimeOffset Now, DateTimeOffset? PreviousTime): TimeHasPassed(Now, PreviousTime);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Orders.Orders.GettingPending;

namespace Orders.Orders.CancellingOrder;
using static TimeHasPassed;

public record CancelOrder(
Guid OrderId,
Expand All @@ -29,7 +30,7 @@ public class HandleCancelOrder(
TimeProvider timeProvider
):
ICommandHandler<CancelOrder>,
IEventHandler<TimeHasPassed>
IEventHandler<MinuteHasPassed>
{
public Task Handle(CancelOrder command, CancellationToken ct) =>
orderRepository.GetAndUpdate(
Expand All @@ -38,7 +39,7 @@ public Task Handle(CancelOrder command, CancellationToken ct) =>
ct: ct
);

public async Task Handle(TimeHasPassed @event, CancellationToken ct)
public async Task Handle(MinuteHasPassed @event, CancellationToken ct)
{
var orderIds = await querySession.Query<PendingOrder>()
.Where(o => o.TimeoutAfter <= @event.Now)
Expand Down
3 changes: 2 additions & 1 deletion Sample/ECommerce/Orders/Orders/Orders/Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

namespace Orders.Orders;
using static OrderEvent;
using static TimeHasPassed;

internal static class OrdersConfig
{
Expand All @@ -40,7 +41,7 @@ private static IServiceCollection AddEventHandlers(this IServiceCollection servi
.AddEventHandler<ProductWasOutOfStock, OrderSaga>()
.AddEventHandler<OrderCancelled, OrderSaga>()
.AddEventHandler<OrderPaymentRecorded, OrderSaga>()
.AddEventHandler<TimeHasPassed, HandleCancelOrder>();
.AddEventHandler<MinuteHasPassed, HandleCancelOrder>();

internal static void ConfigureOrders(this StoreOptions options)
{
Expand Down

0 comments on commit 4ca54f5

Please sign in to comment.