| | | 1 | | using System.Data; |
| | | 2 | | using System.Security.Cryptography; |
| | | 3 | | using System.Text; |
| | | 4 | | using Microsoft.EntityFrameworkCore; |
| | | 5 | | using Microsoft.Extensions.DependencyInjection; |
| | | 6 | | using OzonRepositories.Context; |
| | | 7 | | |
| | | 8 | | namespace Services.ApiServices._1CApi.OData; |
| | | 9 | | |
| | | 10 | | /// <summary>Сериализует создание одной номенклатуры между экземплярами приложения.</summary> |
| | | 11 | | public interface IOneCNomenclatureCreationLock |
| | | 12 | | { |
| | | 13 | | Task<IAsyncDisposable> AcquireAsync(string article, string producerName, CancellationToken cancellationToken = defau |
| | | 14 | | } |
| | | 15 | | |
| | | 16 | | public sealed class OneCNomenclatureCreationLock : IOneCNomenclatureCreationLock |
| | | 17 | | { |
| | | 18 | | private const int TimeoutSeconds = 30; |
| | | 19 | | private readonly IServiceScopeFactory _scopeFactory; |
| | | 20 | | |
| | 0 | 21 | | public OneCNomenclatureCreationLock(IServiceScopeFactory scopeFactory) |
| | | 22 | | { |
| | 0 | 23 | | _scopeFactory = scopeFactory; |
| | 0 | 24 | | } |
| | | 25 | | |
| | | 26 | | public async Task<IAsyncDisposable> AcquireAsync(string article, string producerName, CancellationToken cancellation |
| | | 27 | | { |
| | 0 | 28 | | var scope = _scopeFactory.CreateAsyncScope(); |
| | | 29 | | try |
| | | 30 | | { |
| | 0 | 31 | | var db = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>(); |
| | 0 | 32 | | var connection = db.Database.GetDbConnection(); |
| | 0 | 33 | | await connection.OpenAsync(cancellationToken); |
| | | 34 | | |
| | 0 | 35 | | var lockName = BuildLockName(article, producerName); |
| | 0 | 36 | | await using var command = connection.CreateCommand(); |
| | 0 | 37 | | command.CommandText = "SELECT GET_LOCK(@lockName, @timeoutSeconds)"; |
| | 0 | 38 | | AddParameter(command, "@lockName", lockName); |
| | 0 | 39 | | AddParameter(command, "@timeoutSeconds", TimeoutSeconds); |
| | 0 | 40 | | var acquired = Convert.ToInt32(await command.ExecuteScalarAsync(cancellationToken)) == 1; |
| | 0 | 41 | | if (!acquired) |
| | 0 | 42 | | throw new TimeoutException($"Не удалось получить межпроцессную блокировку создания номенклатуры '{articl |
| | | 43 | | |
| | 0 | 44 | | return new Releaser(scope, connection, lockName); |
| | 0 | 45 | | } |
| | 0 | 46 | | catch |
| | | 47 | | { |
| | 0 | 48 | | await scope.DisposeAsync(); |
| | 0 | 49 | | throw; |
| | | 50 | | } |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | private static string BuildLockName(string article, string producerName) |
| | | 54 | | { |
| | 0 | 55 | | var source = $"{article}|{producerName}"; |
| | 0 | 56 | | var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(source))); |
| | 0 | 57 | | return $"onec-nomenclature:{hash}"; |
| | | 58 | | } |
| | | 59 | | |
| | | 60 | | private static void AddParameter(IDbCommand command, string name, object value) |
| | | 61 | | { |
| | 0 | 62 | | var parameter = command.CreateParameter(); |
| | 0 | 63 | | parameter.ParameterName = name; |
| | 0 | 64 | | parameter.Value = value; |
| | 0 | 65 | | command.Parameters.Add(parameter); |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | private sealed class Releaser : IAsyncDisposable |
| | | 69 | | { |
| | | 70 | | private readonly AsyncServiceScope _scope; |
| | | 71 | | private readonly System.Data.Common.DbConnection _connection; |
| | | 72 | | private readonly string _lockName; |
| | | 73 | | |
| | 0 | 74 | | public Releaser(AsyncServiceScope scope, System.Data.Common.DbConnection connection, string lockName) |
| | | 75 | | { |
| | 0 | 76 | | _scope = scope; |
| | 0 | 77 | | _connection = connection; |
| | 0 | 78 | | _lockName = lockName; |
| | 0 | 79 | | } |
| | | 80 | | |
| | | 81 | | public async ValueTask DisposeAsync() |
| | | 82 | | { |
| | | 83 | | try |
| | | 84 | | { |
| | 0 | 85 | | await using var command = _connection.CreateCommand(); |
| | 0 | 86 | | command.CommandText = "SELECT RELEASE_LOCK(@lockName)"; |
| | 0 | 87 | | AddParameter(command, "@lockName", _lockName); |
| | 0 | 88 | | await command.ExecuteScalarAsync(); |
| | 0 | 89 | | } |
| | | 90 | | finally |
| | | 91 | | { |
| | 0 | 92 | | await _connection.CloseAsync(); |
| | 0 | 93 | | await _scope.DisposeAsync(); |
| | | 94 | | } |
| | 0 | 95 | | } |
| | | 96 | | } |
| | | 97 | | } |