private bool WriteAttempt(TekReleaseWorkflowStateEntity item)
        {
            if (++_AttemptCount > AttemptCountMax)
            {
                _Logger.WriteMaximumCreateAttemptsReached();
                throw new InvalidOperationException("Maximum create attempts reached.");
            }

            if (_AttemptCount > 1)
            {
                _Logger.WriteDuplicatesFound(_AttemptCount);
            }


            item.LabConfirmationId = _LabConfirmationIdService.Next();
            item.BucketId          = _NumberGenerator.NextByteArray(_WorkflowConfig.BucketIdLength);
            item.ConfirmationKey   = _NumberGenerator.NextByteArray(_WorkflowConfig.ConfirmationKeyLength);

            try
            {
                _WorkflowDbContext.SaveAndCommit();
                _Logger.WriteCommitted();
                return(true);
            }
            catch (DbUpdateException ex)
            {
                _WorkflowDbContext.Remove(item);
                if (CanRetry(ex))
                {
                    return(false);
                }

                throw;
            }
        }
Esempio n. 2
0
        public async Task AddExampleContent()
        {
            var r2 = new StandardRandomNumberGenerator();

            await using var tx = await _Provider.Database.BeginTransactionAsync();

            var wfs1 = new TekReleaseWorkflowStateEntity
            {
                LabConfirmationId = _LabConfirmationIdService.Next(),
                BucketId          = r2.NextByteArray(_WorkflowConfig.BucketIdLength),
                ConfirmationKey   = r2.NextByteArray(_WorkflowConfig.ConfirmationKeyLength),
                Created           = new DateTime(2020, 5, 1, 0, 0, 0, DateTimeKind.Utc),
            };

            var key1 = new TekEntity
            {
                Owner              = wfs1,
                PublishingState    = PublishingState.Unpublished,
                RollingPeriod      = 2,
                RollingStartNumber = DateTime.UtcNow.Date.ToRollingStartNumber(),
                KeyData            = r2.NextByteArray(_TekValidatorConfig.KeyDataLength),
                Region             = "NL"
            };


            var key2 = new TekEntity
            {
                Owner              = wfs1,
                PublishingState    = PublishingState.Unpublished,
                RollingPeriod      = 144,
                RollingStartNumber = DateTime.UtcNow.Date.ToRollingStartNumber(),
                KeyData            = r2.NextByteArray(_TekValidatorConfig.KeyDataLength),
                Region             = "NL"
            };

            await _Provider.KeyReleaseWorkflowStates.AddAsync(wfs1);

            await _Provider.TemporaryExposureKeys.AddRangeAsync(key1, key2);

            _Provider.SaveAndCommit();
        }
        private bool TryGenerateRemainingFieldsAndWriteToDb(TekReleaseWorkflowStateEntity item)
        {
            if (++_attemptCount > AttemptCountMax)
            {
                _logger.WriteMaximumCreateAttemptsReached();
                throw new InvalidOperationException("Maximum create attempts reached.");
            }

            if (_attemptCount > 1)
            {
                _logger.WriteDuplicatesFound(_attemptCount);
            }

            item.LabConfirmationId = _labConfirmationIdService.Next();
            item.BucketId          = _numberGenerator.NextByteArray(UniversalConstants.BucketIdByteCount);
            item.ConfirmationKey   = _numberGenerator.NextByteArray(UniversalConstants.ConfirmationKeyByteCount);

            try
            {
                _workflowDbContext.SaveAndCommit();
                _logger.WriteCommitted();
                return(true);
            }
            catch (DbUpdateException ex)
            {
                _workflowDbContext.Database.CurrentTransaction.RollbackAsync();
                _workflowDbContext.Remove(item);

                if (CanRetry(ex))
                {
                    return(false);
                }

                throw;
            }
        }