diff --git a/.github/config/gitversion.yml b/.github/config/gitversion.yml new file mode 100644 index 0000000..27761ed --- /dev/null +++ b/.github/config/gitversion.yml @@ -0,0 +1,9 @@ +next-version: 1.1.0 +mode: ContinuousDeployment +assembly-versioning-scheme: MajorMinorPatch +assembly-file-versioning-scheme: MajorMinorPatch + +branches: + main: + regex: ^main$ + increment: Patch diff --git a/.github/workflows/pull_request_dev.yml b/.github/workflows/pull_request_dev.yml new file mode 100644 index 0000000..df6f213 --- /dev/null +++ b/.github/workflows/pull_request_dev.yml @@ -0,0 +1,22 @@ +name: Build NuGet package on dev branch pull request + +on: + pull_request: + branches: + - "dev" + paths: + - "TimetableDesigner.Backend.Events.OutboxPattern**" + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 10.0.x + - name: Build + run: dotnet build \ No newline at end of file diff --git a/.github/workflows/pull_request_main.yml b/.github/workflows/pull_request_main.yml new file mode 100644 index 0000000..0b9e5bf --- /dev/null +++ b/.github/workflows/pull_request_main.yml @@ -0,0 +1,22 @@ +name: Build NuGet package on main branch pull request + +on: + pull_request: + branches: + - "main" + paths: + - "TimetableDesigner.Backend.Events.OutboxPattern**" + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 10.0.x + - name: Build + run: dotnet build diff --git a/.github/workflows/push_dev.yml b/.github/workflows/push_dev.yml new file mode 100644 index 0000000..9571242 --- /dev/null +++ b/.github/workflows/push_dev.yml @@ -0,0 +1,22 @@ +name: Build NuGet package on dev branch push + +on: + push: + branches: + - "dev" + paths: + - "TimetableDesigner.Backend.Events.OutboxPattern**" + +jobs: + build: + name: Build + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 10.0.x + - name: Build + run: dotnet build \ No newline at end of file diff --git a/.github/workflows/push_main.yml b/.github/workflows/push_main.yml new file mode 100644 index 0000000..a672a79 --- /dev/null +++ b/.github/workflows/push_main.yml @@ -0,0 +1,72 @@ +name: Build NuGet package on main branch push + +on: + push: + branches: + - "main" + paths: + - "TimetableDesigner.Backend.Events.OutboxPattern**" + +jobs: + build: + name: Build and pack + runs-on: ubuntu-latest + steps: + - name: Checkout + uses: actions/checkout@v6.0.2 + with: + fetch-depth: 0 + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 10.0.x + - name: Build + run: dotnet build + - name: Setup GitVersion + uses: gittools/actions/gitversion/setup@v4.2.0 + with: + versionSpec: 6.4.x + - name: Determine version + uses: gittools/actions/gitversion/execute@v4.2.0 + id: gitversion + with: + configFilePath: ./.github/config/gitversion.yml + - name: Create NuGet package + run: dotnet pack -c Release -p:Version=${{steps.gitversion.outputs.SemVer}} -p:PackageVersion=${{steps.gitversion.outputs.SemVer}} --output ${{ github.workspace}}/nuget + - name: Upload artifact + uses: actions/upload-artifact@v6 + with: + name: nuget + path: ${{ github.workspace}}/nuget/*.nupkg + outputs: + version: ${{ steps.gitversion.outputs.SemVer }} + publish: + name: Publish + permissions: write-all + runs-on: ubuntu-latest + needs: build + steps: + - name: Download artifact + uses: actions/download-artifact@v6 + with: + name: nuget + path: ${{ github.workspace}}/nuget + - name: Setup .NET + uses: actions/setup-dotnet@v3 + with: + dotnet-version: 10.0.x + - name: Add source + run: dotnet nuget add source --username TimetableDesigner --password ${{secrets.GITHUB_TOKEN }} --store-password-in-clear-text --name github "https://nuget.pkg.github.com/TimetableDesigner/index.json" + - name: Publish + run: dotnet nuget push ${{ github.workspace}}/nuget/*.nupkg --source "github" --api-key ${{ secrets.NUGET_REGISTRY_API_KEY }} --skip-duplicate + - name: Create Release + uses: actions/create-release@v1 + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + with: + tag_name: ${{needs.build.outputs.version}} + release_name: ${{needs.build.outputs.version}} + draft: false + prerelease: false + + diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b5d7fe6 --- /dev/null +++ b/.gitignore @@ -0,0 +1,289 @@ +## Ignore Visual Studio temporary files, build results, and +## files generated by popular Visual Studio add-ons. +## +## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore + +# User-specific files +*.suo +*.user +*.userosscache +*.sln.docstates + +# User-specific files (MonoDevelop/Xamarin Studio) +*.userprefs + +# Build results +[Dd]ebug/ +[Dd]ebugPublic/ +[Rr]elease/ +x64/ +x86/ +bld/ +[Bb]in/ +[Oo]bj/ +[Ll]og/ + +# Visual Studio 2015 cache/options directory +.vs/ +# Uncomment if you have tasks that create the project's static files in wwwroot +#wwwroot/ + +# MSTest test Results +[Tt]est[Rr]esult*/ +[Bb]uild[Ll]og.* + +# NUNIT +*.VisualState.xml +TestResult.xml + +# Build Results of an ATL Project +[Dd]ebugPS/ +[Rr]eleasePS/ +dlldata.c + +# .NET Core +project.lock.json +project.fragment.lock.json +artifacts/ +**/Properties/launchSettings.json + +# VS Code +.vscode/ + +*_i.c +*_p.c +*_i.h +*.ilk +*.meta +*.obj +*.pch +*.pdb +*.pgc +*.pgd +*.rsp +*.sbr +*.tlb +*.tli +*.tlh +*.tmp +*.tmp_proj +*.log +*.vspscc +*.vssscc +.builds +*.pidb +*.svclog +*.scc + +# Chutzpah Test files +_Chutzpah* + +# Visual C++ cache files +ipch/ +*.aps +*.ncb +*.opendb +*.opensdf +*.sdf +*.cachefile +*.VC.db +*.VC.VC.opendb + +# Visual Studio profiler +*.psess +*.vsp +*.vspx +*.sap + +# TFS 2012 Local Workspace +$tf/ + +# Guidance Automation Toolkit +*.gpState + +# ReSharper is a .NET coding add-in +_ReSharper*/ +*.[Rr]e[Ss]harper +*.DotSettings.user + +# JustCode is a .NET coding add-in +.JustCode + +# TeamCity is a build add-in +_TeamCity* + +# DotCover is a Code Coverage Tool +*.dotCover + +# Visual Studio code coverage results +*.coverage +*.coveragexml + +# NCrunch +_NCrunch_* +.*crunch*.local.xml +nCrunchTemp_* + +# MightyMoose +*.mm.* +AutoTest.Net/ + +# Web workbench (sass) +.sass-cache/ + +# Installshield output folder +[Ee]xpress/ + +# DocProject is a documentation generator add-in +DocProject/buildhelp/ +DocProject/Help/*.HxT +DocProject/Help/*.HxC +DocProject/Help/*.hhc +DocProject/Help/*.hhk +DocProject/Help/*.hhp +DocProject/Help/Html2 +DocProject/Help/html + +# Click-Once directory +publish/ + +# Publish Web Output +*.[Pp]ublish.xml +*.azurePubxml +# TODO: Comment the next line if you want to checkin your web deploy settings +# but database connection strings (with potential passwords) will be unencrypted +*.pubxml +*.publishproj + +# Microsoft Azure Web App publish settings. Comment the next line if you want to +# checkin your Azure Web App publish settings, but sensitive information contained +# in these scripts will be unencrypted +PublishScripts/ + +# NuGet Packages +*.nupkg +# The packages folder can be ignored because of Package Restore +**/packages/* +# except build/, which is used as an MSBuild target. +!**/packages/build/ +# Uncomment if necessary however generally it will be regenerated when needed +#!**/packages/repositories.config +# NuGet v3's project.json files produces more ignorable files +*.nuget.props +*.nuget.targets + +# Microsoft Azure Build Output +csx/ +*.build.csdef + +# Microsoft Azure Emulator +ecf/ +rcf/ + +# Windows Store app package directories and files +AppPackages/ +BundleArtifacts/ +Package.StoreAssociation.xml +_pkginfo.txt + +# Visual Studio cache files +# files ending in .cache can be ignored +*.[Cc]ache +# but keep track of directories ending in .cache +!*.[Cc]ache/ + +# Others +ClientBin/ +~$* +*~ +*.dbmdl +*.dbproj.schemaview +*.jfm +*.pfx +*.publishsettings +orleans.codegen.cs + +# Since there are multiple workflows, uncomment next line to ignore bower_components +# (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) +#bower_components/ + +# RIA/Silverlight projects +Generated_Code/ + +# Backup & report files from converting an old project file +# to a newer Visual Studio version. Backup files are not needed, +# because we have git ;-) +_UpgradeReport_Files/ +Backup*/ +UpgradeLog*.XML +UpgradeLog*.htm + +# SQL Server files +*.mdf +*.ldf + +# Business Intelligence projects +*.rdl.data +*.bim.layout +*.bim_*.settings + +# Microsoft Fakes +FakesAssemblies/ + +# GhostDoc plugin setting file +*.GhostDoc.xml + +# Node.js Tools for Visual Studio +.ntvs_analysis.dat +node_modules/ + +# Typescript v1 declaration files +typings/ + +# Visual Studio 6 build log +*.plg + +# Visual Studio 6 workspace options file +*.opt + +# Visual Studio 6 auto-generated workspace file (contains which files were open etc.) +*.vbw + +# Visual Studio LightSwitch build output +**/*.HTMLClient/GeneratedArtifacts +**/*.DesktopClient/GeneratedArtifacts +**/*.DesktopClient/ModelManifest.xml +**/*.Server/GeneratedArtifacts +**/*.Server/ModelManifest.xml +_Pvt_Extensions + +# Paket dependency manager +.paket/paket.exe +paket-files/ + +# FAKE - F# Make +.fake/ + +# JetBrains Rider +.idea/ +*.sln.iml + +# CodeRush +.cr/ + +# Python Tools for Visual Studio (PTVS) +__pycache__/ +*.pyc + +# Cake - Uncomment if you are using it +# tools/** +# !tools/packages.config + +# Telerik's JustMock configuration file +*.jmconfig + +# BizTalk build output +*.btp.cs +*.btm.cs +*.odx.cs +*.xsd.cs diff --git a/TimetableDesigner.Backend.Events.OutboxPattern.slnx b/TimetableDesigner.Backend.Events.OutboxPattern.slnx new file mode 100644 index 0000000..6ea9c22 --- /dev/null +++ b/TimetableDesigner.Backend.Events.OutboxPattern.slnx @@ -0,0 +1,3 @@ + + + diff --git a/TimetableDesigner.Backend.Events.OutboxPattern/Event.cs b/TimetableDesigner.Backend.Events.OutboxPattern/Event.cs new file mode 100644 index 0000000..f4ea843 --- /dev/null +++ b/TimetableDesigner.Backend.Events.OutboxPattern/Event.cs @@ -0,0 +1,19 @@ +using System.Text.Json; + +namespace TimetableDesigner.Backend.Events.OutboxPattern; + +public class Event +{ + public Guid Id { get; set; } + public required string Payload { get; set; } + public required string PayloadType { get; set; } + public DateTimeOffset OccuredOn { get; set; } = DateTimeOffset.UtcNow; + public DateTimeOffset? LastRetryOn { get; set; } + public uint RetryCount { get; set; } + + public Event(object payload) + { + Payload = JsonSerializer.Serialize(payload); + PayloadType = payload.GetType().FullName!; + } +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Events.OutboxPattern/EventConfiguration.cs b/TimetableDesigner.Backend.Events.OutboxPattern/EventConfiguration.cs new file mode 100644 index 0000000..0bcd183 --- /dev/null +++ b/TimetableDesigner.Backend.Events.OutboxPattern/EventConfiguration.cs @@ -0,0 +1,30 @@ +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Metadata.Builders; + +namespace TimetableDesigner.Backend.Events.OutboxPattern; + +public class EventConfiguration : IEntityTypeConfiguration +{ + public void Configure(EntityTypeBuilder builder) + { + builder.HasKey(x => x.Id); + builder.HasIndex(x => x.Id) + .IsUnique(); + builder.Property(x => x.Id) + .IsRequired(); + + builder.Property(x => x.Payload) + .IsRequired(); + + builder.Property(x => x.PayloadType) + .IsRequired(); + + builder.Property(x => x.OccuredOn) + .IsRequired(); + + builder.Property(x => x.LastRetryOn); + + builder.Property(x => x.RetryCount) + .IsRequired(); + } +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Events.OutboxPattern/EventOutboxSender.cs b/TimetableDesigner.Backend.Events.OutboxPattern/EventOutboxSender.cs new file mode 100644 index 0000000..2cdb9cf --- /dev/null +++ b/TimetableDesigner.Backend.Events.OutboxPattern/EventOutboxSender.cs @@ -0,0 +1,50 @@ +using System.Text.Json; +using Microsoft.EntityFrameworkCore; +using Microsoft.Extensions.Hosting; + +namespace TimetableDesigner.Backend.Events.OutboxPattern; + +public class EventOutboxSender : BackgroundService where TDbContext : DbContext, IEventOutboxDbContext +{ + private readonly TDbContext _databaseContext; + private readonly IEventQueuePublisher _eventQueuePublisher; + + public EventOutboxSender(TDbContext databaseContext, IEventQueuePublisher eventQueuePublisher) + { + _databaseContext = databaseContext; + _eventQueuePublisher = eventQueuePublisher; + } + + protected override async Task ExecuteAsync(CancellationToken stoppingToken) + { + while (!stoppingToken.IsCancellationRequested) + { + Event? eventData = await _databaseContext.Events.FirstOrDefaultAsync(x => x.LastRetryOn == null, stoppingToken) + ?? await _databaseContext.Events.OrderBy(x => x.LastRetryOn).FirstOrDefaultAsync(stoppingToken); + + if (eventData is null) + { + continue; + } + + Type payloadType = Type.GetType(eventData.PayloadType)!; + JsonSerializer.Deserialize(eventData.Payload, payloadType); + + try + { + await _eventQueuePublisher.PublishAsync(eventData.Payload, payloadType); + + _databaseContext.Events.Remove(eventData); + } + catch + { + eventData.LastRetryOn = DateTimeOffset.UtcNow; + eventData.RetryCount++; + } + finally + { + await _databaseContext.SaveChangesAsync(stoppingToken); + } + } + } +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Events.OutboxPattern/IEventOutboxDbContext.cs b/TimetableDesigner.Backend.Events.OutboxPattern/IEventOutboxDbContext.cs new file mode 100644 index 0000000..47b7fe6 --- /dev/null +++ b/TimetableDesigner.Backend.Events.OutboxPattern/IEventOutboxDbContext.cs @@ -0,0 +1,8 @@ +using Microsoft.EntityFrameworkCore; + +namespace TimetableDesigner.Backend.Events.OutboxPattern; + +public interface IEventOutboxDbContext +{ + DbSet Events { get; } +} \ No newline at end of file diff --git a/TimetableDesigner.Backend.Events.OutboxPattern/TimetableDesigner.Backend.Events.OutboxPattern.csproj b/TimetableDesigner.Backend.Events.OutboxPattern/TimetableDesigner.Backend.Events.OutboxPattern.csproj new file mode 100644 index 0000000..c54ee55 --- /dev/null +++ b/TimetableDesigner.Backend.Events.OutboxPattern/TimetableDesigner.Backend.Events.OutboxPattern.csproj @@ -0,0 +1,21 @@ + + + + net10.0 + enable + enable + + TimetableDesigner.Backend.Events.OutboxPattern + Mateusz Skoczek + https://github.com/TimetableDesigner + icon.png + https://github.com/TimetableDesigner/TimetableDesigner.Backend.Events.OutboxPattern + + + + + + + + + diff --git a/TimetableDesigner.Backend.Events.OutboxPattern/icon.png b/TimetableDesigner.Backend.Events.OutboxPattern/icon.png new file mode 100644 index 0000000..98f0c54 Binary files /dev/null and b/TimetableDesigner.Backend.Events.OutboxPattern/icon.png differ