Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix event processing for synced events #1053

Merged
merged 1 commit into from
Jan 4, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public void Configure(EntityTypeBuilder<Event> builder)
builder.ToTable("events");
builder.Property(e => e.EventName).IsRequired().HasMaxLength(200);
builder.Property(e => e.Created).IsRequired();
builder.Property(e => e.Inserted).IsRequired();
builder.Property(e => e.Payload).IsRequired().HasColumnType("jsonb");
builder.Property(e => e.Published);
builder.HasKey(e => e.EventId);
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace TeachingRecordSystem.Core.DataStore.Postgres.Migrations
{
/// <inheritdoc />
public partial class EventInserted : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.AddColumn<DateTime>(
name: "inserted",
table: "events",
type: "timestamp with time zone",
nullable: true);

migrationBuilder.Sql("update events set inserted = created where inserted is null");

migrationBuilder.AlterColumn<DateTime>(
name: "inserted",
table: "events",
type: "timestamp with time zone",
nullable: false,
defaultValue: new DateTime(1, 1, 1, 0, 0, 0, 0, DateTimeKind.Unspecified),
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone",
oldNullable: true);
}

/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "inserted",
table: "events");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ protected override void BuildModel(ModelBuilder modelBuilder)
.HasColumnType("character varying(200)")
.HasColumnName("event_name");

b.Property<DateTime>("Inserted")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("inserted");

b.Property<string>("Payload")
.IsRequired()
.HasColumnType("jsonb")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ public class Event
public required Guid EventId { get; init; }
public required string EventName { get; init; }
public required DateTime Created { get; init; }
public required DateTime Inserted { get; init; }
public required string Payload { get; init; }
public bool Published { get; set; }

public static Event FromEventBase(EventBase @event)
public static Event FromEventBase(EventBase @event, DateTime? inserted)
{
var eventName = @event.GetEventName();
var payload = JsonSerializer.Serialize(@event, inputType: @event.GetType(), EventBase.JsonSerializerOptions);

return new Event()
{
EventId = @event.EventId,
Created = @event.CreatedUtc,
EventName = eventName,
Created = @event.CreatedUtc,
Inserted = inserted ?? @event.CreatedUtc,
Payload = payload
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ public static void ConfigureOptions(DbContextOptionsBuilder optionsBuilder, stri
.UseSnakeCaseNamingConvention();
}

public void AddEvent(EventBase @event)
public void AddEvent(EventBase @event, DateTime? inserted = null)
{
Events.Add(Event.FromEventBase(@event));
Events.Add(Event.FromEventBase(@event, inserted));
}

protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,11 @@ public async Task PublishEvents(CancellationToken cancellationToken)
.FromSql($"""
select * from events
where published is false
and created > {lastProcessedEventTimestamp}
and inserted > {lastProcessedEventTimestamp}
and event_id != {lastProcessedEventId}
for update skip locked limit {BatchSize}
order by inserted, created
for update skip locked
limit {BatchSize}
""")
.ToListAsync(cancellationToken: cancellationToken);

Expand All @@ -88,7 +90,7 @@ and created > {lastProcessedEventTimestamp}
}
finally
{
lastProcessedEventTimestamp = e.Created;
lastProcessedEventTimestamp = e.Inserted;
lastProcessedEventId = e.EventId;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,7 @@ private async Task<int> SyncEvents(IReadOnlyCollection<EventBase> events, Npgsql
"event_id",
"event_name",
"created",
"inserted",
"payload"
};

Expand All @@ -592,6 +593,7 @@ private async Task<int> SyncEvents(IReadOnlyCollection<EventBase> events, Npgsql
event_id UUID NOT NULL,
event_name VARCHAR(200) NOT NULL,
created TIMESTAMP WITH TIME ZONE NOT NULL,
inserted TIMESTAMP WITH TIME ZONE NOT NULL,
payload JSONB NOT NULL
)
""";
Expand Down Expand Up @@ -622,6 +624,7 @@ ON CONFLICT (event_id) DO NOTHING
writer.WriteValueOrNull(e.EventId, NpgsqlDbType.Uuid);
writer.WriteValueOrNull(e.GetEventName(), NpgsqlDbType.Varchar);
writer.WriteValueOrNull(e.CreatedUtc, NpgsqlDbType.TimestampTz);
writer.WriteValueOrNull(clock.UtcNow, NpgsqlDbType.TimestampTz);
writer.WriteValueOrNull(payload, NpgsqlDbType.Jsonb);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,9 @@
namespace TeachingRecordSystem.Core.Tests.Events.Processing;

[Collection(nameof(DisableParallelization))]
public class PublishEventsBackgroundServiceTests : IAsyncLifetime
public class PublishEventsBackgroundServiceTests(DbFixture dbFixture) : IAsyncLifetime
{
private readonly DbFixture _dbFixture;

public PublishEventsBackgroundServiceTests(DbFixture dbFixture)
{
_dbFixture = dbFixture;
}
private readonly DbFixture _dbFixture = dbFixture;

public Task InitializeAsync() =>
_dbFixture.WithDbContext(dbContext => dbContext.Database.ExecuteSqlAsync($"delete from events"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,11 @@

namespace TeachingRecordSystem.TestCommon;

public class DbFixture
public class DbFixture(DbHelper dbHelper, IServiceProvider serviceProvider)
{
public DbFixture(DbHelper dbHelper, IServiceProvider serviceProvider)
{
DbHelper = dbHelper;
Services = serviceProvider;
}

public DbHelper DbHelper { get; }
public DbHelper DbHelper { get; } = dbHelper;

public IServiceProvider Services { get; }
public IServiceProvider Services { get; } = serviceProvider;

public TrsDbContext GetDbContext() => Services.GetRequiredService<TrsDbContext>();

Expand Down
Loading