Merge pull request #129 from meysamhadeli/refactor/refactor-persist-message-db-context

refactor: Refactor PersistMessageDbContext for handle optimistic conc…
This commit is contained in:
Meysam Hadeli 2023-01-26 15:06:58 +03:30 committed by GitHub
commit 4c31d2ced2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 53 additions and 73 deletions

View File

@ -45,18 +45,17 @@ jobs:
env: env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
##ref: https://docs.docker.com/language/golang/configure-ci-cd/
##ref: https://event-driven.io/en/how_to_buid_and_push_docker_image_with_github_actions
- name: Login to DockerHub - name: Login to DockerHub
uses: docker/login-action@v2 uses: docker/login-action@v2
if: ${{ github.ref == 'refs/heads/main' && success() }} if: ${{ github.ref == 'refs/heads/main' && success() && github.event_name == 'pull_request' }}
with: with:
username: ${{ secrets.DOCKERHUB_USERNAME }} username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }} password: ${{ secrets.DOCKERHUB_PASSWORD }}
##ref: https://docs.docker.com/language/golang/configure-ci-cd/
##ref: https://event-driven.io/en/how_to_buid_and_push_docker_image_with_github_actions
- name: Build Docker Image - name: Build Docker Image
if: ${{ github.ref == 'refs/heads/main' && success() }} if: ${{ github.ref == 'refs/heads/main' && success() && github.event_name == 'pull_request' }}
run: | run: |
docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/booking-microservices-flight:${{ steps.last_release.outputs.tag_name }} -f "${{ github.workspace }}/src/Services/Flight/Dockerfile" . docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/booking-microservices-flight:${{ steps.last_release.outputs.tag_name }} -f "${{ github.workspace }}/src/Services/Flight/Dockerfile" .
docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/booking-microservices-identity:${{ steps.last_release.outputs.tag_name }} -f "${{ github.workspace }}/src/Services/Identity/Dockerfile" . docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/booking-microservices-identity:${{ steps.last_release.outputs.tag_name }} -f "${{ github.workspace }}/src/Services/Identity/Dockerfile" .
@ -64,7 +63,7 @@ jobs:
docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/booking-microservices-booking:${{ steps.last_release.outputs.tag_name }} -f "${{ github.workspace }}/src/Services/Booking/Dockerfile" . docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/booking-microservices-booking:${{ steps.last_release.outputs.tag_name }} -f "${{ github.workspace }}/src/Services/Booking/Dockerfile" .
- name: Publish Docker Image - name: Publish Docker Image
if: ${{ github.ref == 'refs/heads/main' && success() }} if: ${{ github.ref == 'refs/heads/main' && success() && github.event_name == 'pull_request' }}
run: | run: |
docker push ${{ secrets.DOCKERHUB_USERNAME }}/booking-microservices-flight:${{ steps.last_release.outputs.tag_name }} docker push ${{ secrets.DOCKERHUB_USERNAME }}/booking-microservices-flight:${{ steps.last_release.outputs.tag_name }}
docker push ${{ secrets.DOCKERHUB_USERNAME }}/booking-microservices-identity:${{ steps.last_release.outputs.tag_name }} docker push ${{ secrets.DOCKERHUB_USERNAME }}/booking-microservices-identity:${{ steps.last_release.outputs.tag_name }}

View File

@ -8,6 +8,7 @@ using Configurations;
using Core.Model; using Core.Model;
using global::Polly; using global::Polly;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Exception = System.Exception; using Exception = System.Exception;
public class PersistMessageDbContext : DbContext, IPersistMessageDbContext public class PersistMessageDbContext : DbContext, IPersistMessageDbContext
@ -52,27 +53,23 @@ public class PersistMessageDbContext : DbContext, IPersistMessageDbContext
{ {
return await policy.ExecuteAsync(async () => await base.SaveChangesAsync(cancellationToken)); return await policy.ExecuteAsync(async () => await base.SaveChangesAsync(cancellationToken));
} }
//ref: https://learn.microsoft.com/en-us/ef/core/saving/concurrency?tabs=data-annotations#resolving-concurrency-conflicts
catch (DbUpdateConcurrencyException ex) catch (DbUpdateConcurrencyException ex)
{ {
foreach (var entry in ex.Entries) foreach (var entry in ex.Entries)
{ {
var currentValue = entry.Entity; // we can use it for specific merging var currentValues = entry.CurrentValues;
var databaseValue = await entry.GetDatabaseValuesAsync(cancellationToken); var databaseValues = await entry.GetDatabaseValuesAsync(cancellationToken);
_logger.LogInformation( _logger.LogInformation(
"Entry to entity with type: {Type}, database-value: {DatabaseValue} and current-value: {CurrentValue}", "Entry to entity with database-value: {DatabaseValues} and current-value: {CurrentValues}",
entry.GetType().Name, JsonConvert.SerializeObject(databaseValues,
databaseValue, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }),
currentValue); JsonConvert.SerializeObject(currentValues,
new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }));
if (databaseValue != null) // Refresh the original values with current values
{ entry.OriginalValues.SetValues(currentValues);
entry.OriginalValues.SetValues(databaseValue);
}
else
{
entry.OriginalValues.SetValues(currentValue);
}
} }
return await base.SaveChangesAsync(cancellationToken); return await base.SaveChangesAsync(cancellationToken);

View File

@ -1,15 +1,17 @@
{ {
"$schema": "https://json.schemastore.org/launchsettings.json", "$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:53934",
"sslPort": 44392
}
},
"profiles": { "profiles": {
"Booking.Api": { "http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:6010",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,

View File

@ -1,15 +1,17 @@
{ {
"$schema": "https://json.schemastore.org/launchsettings.json", "$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:59936",
"sslPort": 44319
}
},
"profiles": { "profiles": {
"Flight.Api": { "http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:5004",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,
@ -18,14 +20,8 @@
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
} }
} }
} }

View File

@ -1,28 +1,12 @@
{ {
"$schema": "https://json.schemastore.org/launchsettings.json", "$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:42478",
"sslPort": 44342
}
},
"profiles": { "profiles": {
"Identity.Api": { "https": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,
"launchUrl": "swagger", "launchUrl": "swagger",
"applicationUrl": "https://localhost:5005;http://localhost:6005", "applicationUrl": "https://localhost:5005",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": false,
"launchUrl": "swagger",
"environmentVariables": { "environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development" "ASPNETCORE_ENVIRONMENT": "Development"
} }

View File

@ -1,15 +1,17 @@
{ {
"$schema": "https://json.schemastore.org/launchsettings.json", "$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:32326",
"sslPort": 44374
}
},
"profiles": { "profiles": {
"Passenger.Api": { "http": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "http://localhost:6012",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"https": {
"commandName": "Project", "commandName": "Project",
"dotnetRunMessages": true, "dotnetRunMessages": true,
"launchBrowser": true, "launchBrowser": true,