Skip to content

Commit

Permalink
Updated to MT v8
Browse files Browse the repository at this point in the history
  • Loading branch information
phatboyg committed Apr 19, 2022
1 parent d01be29 commit 07127a4
Show file tree
Hide file tree
Showing 22 changed files with 64 additions and 186 deletions.
8 changes: 3 additions & 5 deletions src/SampleBatch.Api/SampleBatch.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MassTransit.Analyzers" Version="7.0.0" />
<PackageReference Include="MassTransit.AspNetCore" Version="7.0.0" />
<PackageReference Include="MassTransit.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="MassTransit.RabbitMQ" Version="7.0.0" />
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" Version="7.0.0" />
<PackageReference Include="MassTransit.Analyzers" Version="8.0.1" />
<PackageReference Include="MassTransit.RabbitMQ" Version="8.0.1" />
<PackageReference Include="MassTransit.Azure.ServiceBus.Core" Version="8.0.1" />
<PackageReference Include="Microsoft.Extensions.Caching.StackExchangeRedis" Version="3.1.5" />
<PackageReference Include="NSwag.AspNetCore" Version="13.6.2" />
</ItemGroup>
Expand Down
17 changes: 0 additions & 17 deletions src/SampleBatch.Common/JsonHelper.cs

This file was deleted.

64 changes: 0 additions & 64 deletions src/SampleBatch.Common/JsonValueComparer.cs

This file was deleted.

16 changes: 0 additions & 16 deletions src/SampleBatch.Common/JsonValueConverter.cs

This file was deleted.

4 changes: 2 additions & 2 deletions src/SampleBatch.Common/SampleBatch.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MassTransit" Version="7.0.0" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.5" />
<PackageReference Include="MassTransit" Version="8.0.1" />
<PackageReference Include="Microsoft.EntityFrameworkCore" Version="3.1.18" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,14 @@
{
using System;
using System.Threading.Tasks;
using MassTransit.Courier;
using MassTransit.Courier.Exceptions;
using MassTransit;
using Microsoft.Extensions.Logging;


public class CancelOrderActivity :
IExecuteActivity<CancelOrderArguments>
{
private readonly SampleBatchDbContext _dbContext;
readonly SampleBatchDbContext _dbContext;
readonly ILogger _logger;

public CancelOrderActivity(SampleBatchDbContext dbContext, ILoggerFactory loggerFactory)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@
{
using System;
using System.Threading.Tasks;
using MassTransit.Courier;
using MassTransit.Courier.Exceptions;
using MassTransit;
using Microsoft.Extensions.Logging;


Expand Down
9 changes: 0 additions & 9 deletions src/SampleBatch.Components/Consumers/SubmitBatchConsumer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using System.Threading.Tasks;
using Contracts;
using MassTransit;
using MassTransit.Definition;
using Microsoft.Extensions.Logging;


Expand Down Expand Up @@ -66,14 +65,6 @@ public class SubmitBatchConsumerDefinition :
public SubmitBatchConsumerDefinition()
{
ConcurrentMessageLimit = 10;

Request<SubmitBatch>(x =>
{
x.Responds<BatchRejected>();
x.Responds<BatchSubmitted>();

x.Publishes<BatchReceived>();
});
}
}
}
4 changes: 2 additions & 2 deletions src/SampleBatch.Components/SampleBatch.Components.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MassTransit.Analyzers" Version="7.0.0" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="7.0.0" />
<PackageReference Include="MassTransit.Analyzers" Version="8.0.1" />
<PackageReference Include="MassTransit.EntityFrameworkCore" Version="8.0.1" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/SampleBatch.Components/SampleBatchDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)
}

public DbSet<BatchState> BatchStates { get; set; }
public DbSet<JobState> JobStates { get; set; }
public DbSet<BatchJobState> JobStates { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
namespace SampleBatch.Components.StateMachines
{
using System;
using Automatonymous;
using Contracts.Enums;
using MassTransit;


public class JobState :
public class BatchJobState :
SagaStateMachineInstance
{
public Guid BatchId { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
namespace SampleBatch.Components.StateMachines
{
using System;
using System.Threading.Tasks;
using Automatonymous;
using Contracts;
using GreenPipes;
using MassTransit;
using MassTransit.Definition;


public class JobStateMachine :
MassTransitStateMachine<JobState>
public class BatchJobStateMachine :
MassTransitStateMachine<BatchJobState>
{
public JobStateMachine()
public BatchJobStateMachine()
{
InstanceState(x => x.CurrentState);

Expand All @@ -25,7 +21,14 @@ public JobStateMachine()
.Then(context => Touch(context.Instance, context.Data.Timestamp))
.Then(context => SetReceiveTimestamp(context.Instance, context.Data.Timestamp))
.Then(Initialize)
.ThenAsync(InitiateProcessing)
.Send(context => context.Init<ProcessBatchJob>(new
{
BatchJobId = context.Instance.CorrelationId,
Timestamp = DateTime.UtcNow,
context.Instance.BatchId,
context.Instance.OrderId,
context.Instance.Action
}))
.TransitionTo(Received));

During(Received,
Expand Down Expand Up @@ -58,55 +61,43 @@ public JobStateMachine()
public Event<BatchJobFailed> BatchJobFailed { get; private set; }
public Event<BatchJobCompleted> BatchJobCompleted { get; private set; }

static void Touch(JobState state, DateTime timestamp)
static void Touch(BatchJobState state, DateTime timestamp)
{
state.CreateTimestamp ??= timestamp;

if (!state.UpdateTimestamp.HasValue || state.UpdateTimestamp.Value < timestamp)
state.UpdateTimestamp = timestamp;
}

static void SetReceiveTimestamp(JobState state, DateTime timestamp)
static void SetReceiveTimestamp(BatchJobState state, DateTime timestamp)
{
if (!state.ReceiveTimestamp.HasValue || state.ReceiveTimestamp.Value > timestamp)
state.ReceiveTimestamp = timestamp;
}

static void Initialize(BehaviorContext<JobState, BatchJobReceived> context)
static void Initialize(BehaviorContext<BatchJobState, BatchJobReceived> context)
{
InitializeInstance(context.Instance, context.Data);
}

static void InitializeInstance(JobState instance, BatchJobReceived data)
static void InitializeInstance(BatchJobState instance, BatchJobReceived data)
{
instance.Action = data.Action;
instance.OrderId = data.OrderId;
instance.BatchId = data.BatchId;
}

static async Task InitiateProcessing(BehaviorContext<JobState, BatchJobReceived> context)
{
await context.Send<JobState, BatchJobReceived, ProcessBatchJob>(new
{
BatchJobId = context.Instance.CorrelationId,
Timestamp = DateTime.UtcNow,
context.Instance.BatchId,
context.Instance.OrderId,
context.Instance.Action
});
}
}


public class JobStateMachineDefinition :
SagaDefinition<JobState>
SagaDefinition<BatchJobState>
{
public JobStateMachineDefinition()
{
ConcurrentMessageLimit = 8;
}

protected override void ConfigureSaga(IReceiveEndpointConfigurator endpointConfigurator, ISagaConfigurator<JobState> sagaConfigurator)
protected override void ConfigureSaga(IReceiveEndpointConfigurator endpointConfigurator, ISagaConfigurator<BatchJobState> sagaConfigurator)
{
sagaConfigurator.UseMessageRetry(r => r.Immediate(5));
sagaConfigurator.UseInMemoryOutbox();
Expand Down
7 changes: 3 additions & 4 deletions src/SampleBatch.Components/StateMachines/BatchState.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@
{
using System;
using System.Collections.Generic;
using Automatonymous;
using Contracts.Enums;
using MassTransit;


public class BatchState :
SagaStateMachineInstance
{
public Guid CorrelationId { get; set; }

public string CurrentState { get; set; }

public DateTime? ReceiveTimestamp { get; set; }
Expand All @@ -35,6 +33,7 @@ public class BatchState :
public Dictionary<Guid, Guid> ProcessingOrderIds { get; set; } = new Dictionary<Guid, Guid>(); // CorrelationId, OrderId

// Navigation Properties
public List<JobState> Jobs { get; set; } = new List<JobState>();
public List<BatchJobState> Jobs { get; set; } = new List<BatchJobState>();
public Guid CorrelationId { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
{
using System;
using System.Collections.Generic;
using Common;
using Contracts.Enums;
using MassTransit.EntityFrameworkCoreIntegration;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Automatonymous;
using Contracts;
using GreenPipes;
using MassTransit;
using MassTransit.Definition;


public class BatchStateMachine :
Expand Down Expand Up @@ -93,7 +90,7 @@ public BatchStateMachine()
InVar.Timestamp,
ProcessingJobCount = x.Instance.ProcessingOrderIds.Count,
UnprocessedJobCount = x.Instance.UnprocessedOrderIds.Count,
State = (await this.GetState(x.Instance)).Name
State = (await this.GetState(x)).Name
})),
When(BatchReceived)
.Then(context => Touch(context.Instance, context.Data.Timestamp))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@


class JobStateEntityConfiguration :
IEntityTypeConfiguration<JobState>
IEntityTypeConfiguration<BatchJobState>
{
public void Configure(EntityTypeBuilder<JobState> builder)
public void Configure(EntityTypeBuilder<BatchJobState> builder)
{
builder.HasKey(c => c.CorrelationId);

Expand Down
2 changes: 1 addition & 1 deletion src/SampleBatch.Contracts/SampleBatch.Contracts.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MassTransit" Version="7.0.0" />
<PackageReference Include="MassTransit" Version="8.0.1" />
</ItemGroup>

</Project>
Loading

0 comments on commit 07127a4

Please sign in to comment.