public async Task <CloudResource> CreateStudySpecificResourceGroupEntryAsync(int studyId, string resourceGroupName, string region, Dictionary <string, string> tags) { var currentUser = await _userService.GetCurrentUserAsync(); var sessionId = _requestIdService.GetRequestId(); var resourceGroupEntry = CloudResourceFactory.CreateStudyResourceGroupEntry(currentUser, sessionId, studyId, region, resourceGroupName, tags); await SaveToDb(resourceGroupEntry); return(resourceGroupEntry); }
async Task <CloudResourceOperation> CreateBasicOperationAsync() { var currentUser = await _userService.GetCurrentUserAsync(false); var newOperation = new CloudResourceOperation() { Status = CloudResourceOperationState.NEW, CreatedBy = currentUser.UserName, CreatedBySessionId = _requestIdService.GetRequestId(), MaxTryCount = CloudResourceConstants.RESOURCE_MAX_TRY_COUNT }; return(newOperation); }
public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next) { try { var response = await next(); return(response); } catch (Exception ex) { var name = typeof(TRequest).Name; ex.Source = _configuration["HandledExceptionSource"]; _logger.LogError("Error ({RequestId}): {Date} {Name} {@Exception}", _requestIdService.GetRequestId(), DateTime.Now, name, ex); throw; } }
public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next) { _timer.Start(); var response = await next(); _timer.Stop(); if (_timer.ElapsedMilliseconds <= PerformanceThreshold) { return(response); } var name = typeof(TRequest).Name; _logger.LogWarning( "Long Running Request ({RequestId}): {Date} {Name} ({ElapsedMilliseconds} milliseconds) {@Request}", _requestIdService.GetRequestId(), DateTime.Now, name, _timer.ElapsedMilliseconds, request); return(response); }
public async Task <TResponse> Handle(TRequest request, CancellationToken cancellationToken, RequestHandlerDelegate <TResponse> next) { var name = typeof(TRequest).Name; _logger.LogInformation("Request ({RequestId}): {Date} {Name}", _requestIdService.GetRequestId(), DateTime.Now, name); return(await next()); }
public async Task HandleWork(ProvisioningQueueParentDto queueParentItem) { _provisioningLogService.HandlingQueueParent(queueParentItem); //One per child item in queue item CloudResourceOperationDto currentOperation = null; //Get's re-used amonong child elements because the operations might share variables var currentProvisioningParameters = new ResourceProvisioningParameters(); ResourceProvisioningResult currentProvisioningResult = null; try { //If more than one child/operation, run basic checks on all operations before starting if (queueParentItem.Children.Count > 1) { _provisioningLogService.QueueParentProgressInformation(queueParentItem, "Multiple child item, running pre checks"); foreach (var queueChildItem in queueParentItem.Children) { currentOperation = await _resourceOperationReadService.GetByIdAsync(queueChildItem.ResourceOperationId); _operationCheckService.ThrowIfPossiblyInProgress(currentOperation); } } foreach (var queueChildItem in queueParentItem.Children) { try { currentOperation = await _resourceOperationReadService.GetByIdAsync(queueChildItem.ResourceOperationId); _provisioningLogService.OperationInformation(currentOperation, "Starting operation"); _operationCheckService.ThrowIfTryCountExceededOrAborted(currentOperation); _operationCheckService.ThrowIfResourceIsDeletedAndOperationIsNotADelete(currentOperation); _operationCheckService.ThrowIfPossiblyInProgress(currentOperation); await _operationCheckService.ThrowIfDependentOnUnfinishedOperationAsync(currentOperation, queueParentItem); string networkSecurityGroupName = null; //Only relevant for Sandbox Resource Creation if (currentOperation.Resource.SandboxId.HasValue) { var nsg = CloudResourceUtil.GetSibilingResource(await _resourceReadService.GetByIdNoAccessCheckAsync(currentOperation.Resource.Id), AzureResourceType.NetworkSecurityGroup); networkSecurityGroupName = nsg?.ResourceName; } ProvisioningParamaterUtil.PrepareForNewOperation(currentProvisioningParameters, currentOperation, currentProvisioningResult, networkSecurityGroupName); await _provisioningQueueService.IncreaseInvisibleBasedOnResource(currentOperation, queueParentItem); _provisioningLogService.OperationInformation(currentOperation, "Initial checks passed"); if (_createAndUpdateService.CanHandle(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is CREATE or UPDATE"); var provisioningService = AzureResourceServiceResolver.GetProvisioningServiceOrThrow(_serviceProvider, currentOperation.Resource.ResourceType); if (await _operationCompletedService.HandledAsAllreadyCompletedAsync(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is allready completed"); currentProvisioningResult = await provisioningService.GetSharedVariables(currentProvisioningParameters); continue; } else { currentOperation = await _resourceOperationUpdateService.SetInProgressAsync(currentOperation.Id, _requestIdService.GetRequestId()); currentProvisioningResult = await _createAndUpdateService.Handle(currentOperation, currentProvisioningParameters, provisioningService); } currentOperation = await _resourceOperationUpdateService.TouchAsync(currentOperation.Id); await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, CloudResourceOperationState.DONE_SUCCESSFUL, updatedProvisioningState : currentProvisioningResult.CurrentProvisioningState); } else if (_deleteOperationService.CanHandle(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is DELETE"); var provisioningService = AzureResourceServiceResolver.GetProvisioningServiceOrThrow(_serviceProvider, currentOperation.Resource.ResourceType); currentOperation = await _resourceOperationUpdateService.SetInProgressAsync(currentOperation.Id, _requestIdService.GetRequestId()); currentProvisioningResult = await _deleteOperationService.Handle(currentOperation, currentProvisioningParameters, provisioningService); await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, CloudResourceOperationState.DONE_SUCCESSFUL, updatedProvisioningState : null); } else if (_roleProvisioningService.CanHandle(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is ENSURE ROLES"); currentOperation = await _resourceOperationUpdateService.SetInProgressAsync(currentOperation.Id, _requestIdService.GetRequestId()); await _roleProvisioningService.Handle(currentOperation); await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, CloudResourceOperationState.DONE_SUCCESSFUL); } else if (_firewallService.CanHandle(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is ENSURE FIREWALL"); var firewallRuleService = AzureResourceServiceResolver.GetFirewallRuleService(_serviceProvider, currentOperation.Resource.ResourceType); currentOperation = await _resourceOperationUpdateService.SetInProgressAsync(currentOperation.Id, _requestIdService.GetRequestId()); if (firewallRuleService is IHasFirewallRules) { await _firewallService.Handle(currentOperation, firewallRuleService ); await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, CloudResourceOperationState.DONE_SUCCESSFUL); } else { throw new ProvisioningException($"Service {firewallRuleService.GetType().Name} does not support firewall operations", CloudResourceOperationState.ABORTED, deleteFromQueue: true); } } else if (_corsRuleProvisioningService.CanHandle(currentOperation)) { _provisioningLogService.OperationInformation(currentOperation, "Operation is ENSURE CORS RULES"); var corsRuleService = AzureResourceServiceResolver.GetCorsRuleServiceOrThrow(_serviceProvider, currentOperation.Resource.ResourceType); currentOperation = await _resourceOperationUpdateService.SetInProgressAsync(currentOperation.Id, _requestIdService.GetRequestId()); if (corsRuleService is IHasCorsRules) { await _corsRuleProvisioningService.Handle(currentOperation, corsRuleService); await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, CloudResourceOperationState.DONE_SUCCESSFUL); } else { throw new ProvisioningException($"Service {corsRuleService.GetType().Name} does not support CORS operations", CloudResourceOperationState.ABORTED, deleteFromQueue: true); } } else { throw new ProvisioningException("Unknown operation type", CloudResourceOperationState.ABORTED); } _provisioningLogService.OperationInformation(currentOperation, "Successfully handeled operation"); } catch (ProvisioningException ex) //Inner loop, ordinary exception is not catched { if (ex.LogAsWarning) { if (ex.IncludeExceptionInWarningLog) { _provisioningLogService.OperationWarning(currentOperation, "Operation aborted", ex); } else { _provisioningLogService.OperationWarning(currentOperation, $"Operation aborted: {ex.Message}"); } } else { _provisioningLogService.OperationError(ex, currentOperation, "Operation failed"); } currentOperation = await _resourceOperationUpdateService.SetErrorMessageAsync(currentOperation.Id, ex); if (!String.IsNullOrWhiteSpace(ex.NewOperationStatus)) { currentOperation = await _resourceOperationUpdateService.UpdateStatusAsync(currentOperation.Id, ex.NewOperationStatus); } if (!ex.ProceedWithOtherOperations) { throw; } } } //foreach _provisioningLogService.QueueParentProgressInformation(queueParentItem, "Done"); await _provisioningQueueService.DeleteMessageAsync(queueParentItem); await MoveUpAnyDependentOperations(queueParentItem); } catch (ProvisioningException ex) //Outer loop catch 1 { if (ex.DeleteFromQueue) { _provisioningLogService.QueueParentProgressWarning(queueParentItem, "Deleting due to exception"); await _provisioningQueueService.DeleteMessageAsync(queueParentItem); } else if (ex.PostponeQueueItemFor.HasValue && ex.PostponeQueueItemFor.Value > 0) { if (currentOperation.TryCount < currentOperation.MaxTryCount) { if (queueParentItem.DequeueCount == 5) { _provisioningLogService.QueueParentProgressWarning(queueParentItem, "Re-queuing after exception"); await _provisioningQueueService.ReQueueMessageAsync(queueParentItem, ex.PostponeQueueItemFor.Value); } else { _provisioningLogService.QueueParentProgressWarning(queueParentItem, "Increasing invisibility after exception"); await _provisioningQueueService.IncreaseInvisibilityAsync(queueParentItem, ex.PostponeQueueItemFor.Value); } } } if (ex.StoreQueueInfoOnOperation) { if (!queueParentItem.NextVisibleOn.HasValue) { _provisioningLogService.QueueParentProgressError(queueParentItem, "Could not store queue info on operation, no next visible time exist"); } else { currentOperation = await _resourceOperationUpdateService.SetQueueInformationAsync(currentOperation.Id, queueParentItem.MessageId, queueParentItem.PopReceipt, queueParentItem.NextVisibleOn.Value); } } } catch (Exception ex) //Outer loop catch 2 { _provisioningLogService.QueueParentProgressError(queueParentItem, "Unhandled exception occured"); await _provisioningQueueService.DeleteMessageAsync(queueParentItem); } }
public async Task Invoke(HttpContext context, ILogger <ErrorHandlingMiddleware> log /* other dependencies */) { var path = context.Request.Path; var method = context.Request.Method; var requestId = _requestIdService.GetRequestId(); try { await next(context); } catch (NotFoundException ex) { LogHelper.LogError(log, ex, path, method); var result = JsonExceptionResultFactory.CreateExceptionMessageResult( requestId, ex, HttpStatusCode.NotFound); await HandleExceptionAsync(context, result.Content, result.StatusCode); } catch (ForbiddenException ex) { LogHelper.LogError(log, ex, path, method); var result = JsonExceptionResultFactory.CreateExceptionMessageResult( requestId, ex, HttpStatusCode.Forbidden); await HandleExceptionAsync(context, result.Content, result.StatusCode); } catch (ArgumentException ex) { LogHelper.LogError(log, ex, path, method); var result = JsonExceptionResultFactory.CreateExceptionMessageResult( requestId, ex, HttpStatusCode.BadRequest); await HandleExceptionAsync(context, result.Content, result.StatusCode); } catch (BadRequestException ex) { LogHelper.LogError(log, ex, path, method); var result = JsonExceptionResultFactory.CreateExceptionMessageResult( requestId, ex, HttpStatusCode.BadRequest); await HandleExceptionAsync(context, result.Content, result.StatusCode); } catch (TaskCanceledException ex) { log.LogInformation($"Task was cancelled: {method} {path}, requestId: {requestId}"); var result = JsonExceptionResultFactory.CreateExceptionMessageResult( requestId, ex, HttpStatusCode.Continue); await HandleExceptionAsync(context, result.Content, result.StatusCode); } catch (Exception ex) { LogHelper.LogError(log, ex, path, method); var result = JsonExceptionResultFactory.CreateExceptionMessageResult( requestId, ex, HttpStatusCode.InternalServerError); await HandleExceptionAsync(context, result.Content, result.StatusCode); } }