public async Task StopPendingAsync(string operationId, PauseCancellationSource source, Initiator initiator) { var locker = _lock.GetOrAdd(operationId, new SemaphoreSlim(1, 1)); await locker.WaitAsync(); try { var pendingPause = (await _pauseRepository.FindAsync( operationId, SpecialLiquidationSaga.OperationName, PendingPredicate)) .SingleOrDefault(); if (pendingPause != null) { if (pendingPause.Oid == null) { throw new InvalidOperationException("Pause oid is required to update"); } var updated = await _pauseRepository.UpdateAsync( pendingPause.Oid.Value, pendingPause.EffectiveSince, PauseState.Cancelled, _dateService.Now(), _dateService.Now(), initiator, source); if (!updated) { await _log.WriteWarningAsync(nameof(RfqPauseService), nameof(StopPendingAsync), null, $"Couldn't stop pending pause for operation id [{operationId}] and name [{SpecialLiquidationSaga.OperationName}]"); } } // todo: add audit log } finally { locker.Release(); } }
public async Task <RfqResumeErrorCode> ResumeAsync(string operationId, PauseCancellationSource source, Initiator initiator) { if (string.IsNullOrEmpty(operationId)) { throw new ArgumentNullException(nameof(operationId)); } var locker = _lock.GetOrAdd(operationId, new SemaphoreSlim(1, 1)); await locker.WaitAsync(); try { var executionInfo = await _executionInfoRepository .GetAsync <SpecialLiquidationOperationData>(SpecialLiquidationSaga.OperationName, operationId); if (executionInfo == null) { return(RfqResumeErrorCode.NotFound); } var activePause = (await _pauseRepository.FindAsync( operationId, SpecialLiquidationSaga.OperationName, ActivePredicate)) .SingleOrDefault(); if (activePause == null) { await _log.WriteInfoAsync(nameof(RfqPauseService), nameof(ResumeAsync), null, $"The active pause for operation id [{operationId}] and name [{SpecialLiquidationSaga.OperationName}] was not found"); return(RfqResumeErrorCode.NotPaused); } // Manual resume is allowed for manually paused RFQ only if (source == PauseCancellationSource.Manual && activePause.Source != PauseSource.Manual) { await _log.WriteWarningAsync(nameof(RfqPauseService), nameof(ResumeAsync), null, $"Manual resume is allowed for manually paused RFQ only"); return(RfqResumeErrorCode.ManualResumeDenied); } if (activePause.Oid == null) { throw new InvalidOperationException("Pause oid is required to update"); } var updated = await _pauseRepository.UpdateAsync( activePause.Oid.Value, activePause.EffectiveSince ?? throw new InvalidOperationException("Activated pause must have an [Effective Since] value"), PauseState.PendingCancellation, _dateService.Now(), null, initiator, source); if (updated) { _cqrsSender.SendCommandToSelf(new ResumePausedSpecialLiquidationCommand { OperationId = operationId }); } else { await _log.WriteWarningAsync(nameof(RfqPauseService), nameof(ResumeAsync), null, $"Couldn't cancel active pause for operation id [{operationId}] and name [{SpecialLiquidationSaga.OperationName}] due to database issues"); return(RfqResumeErrorCode.Persistence); } // todo: add audit log } finally { locker.Release(); } return(RfqResumeErrorCode.None); }