/// <summary> /// Bootstraps (loads data) Implementations that were in process when this api encounters a stop/start cycle /// </summary> private void Bootstrap() { _logger.LogInformation($"AlloyBackgroundService is starting bootstrap."); using (var scope = _scopeFactory.CreateScope()) { using (var alloyContext = scope.ServiceProvider.GetRequiredService <AlloyContext>()) { // get implementation entities that are currently "in process" var implementationEntities = alloyContext.Implementations .Where(o => o.Status != ImplementationStatus.Active && o.Status != ImplementationStatus.Failed && o.Status != ImplementationStatus.Ended && o.Status != ImplementationStatus.Expired); if (implementationEntities.Any()) { _logger.LogInformation($"AlloyBackgroundService is queueing {implementationEntities.Count()} Implementations."); foreach (var implementationEntity in implementationEntities) { _implementationQueue.Add(implementationEntity); _logger.LogInformation($"AlloyBackgroundService is queueing Implementation {implementationEntity.Id}."); } } } } _logger.LogInformation($"AlloyBackgroundService bootstrap complete."); }
public async Task <Implementation> LaunchImplementationFromDefinitionAsync(Guid definitionId, CancellationToken ct) { var user = await _claimsService.GetClaimsPrincipal(_user.GetId(), true); if (!(await _authorizationService.AuthorizeAsync(user, null, new BasicRightsRequirement())).Succeeded) { throw new ForbiddenException(); } // check for resource limitations if (!(await ResourcesAreAvailableAsync(definitionId, ct))) { throw new Exception($"The appropriate resources are not available to create an implementation from the Definition {definitionId}."); } // create the implementation from the definition var implementationEntity = await CreateImplementationEntityAsync(definitionId, ct); // add the implementation to the implementation queue for AlloyBackgrounsService to process. _alloyImplementationQueue.Add(implementationEntity); return(Mapper.Map <Implementation>(implementationEntity)); }
private async void Run(object state) { _logger.LogInformation("AlloyQueryService is working."); try { using (var scope = _scopeFactory.CreateScope()) { using (var alloyContext = scope.ServiceProvider.GetRequiredService <AlloyContext>()) { var currentDateTime = DateTime.UtcNow; var expiredImplementationEntities = alloyContext.Implementations.Where(o => o.EndDate == null && o.ExpirationDate < currentDateTime).ToList(); if (expiredImplementationEntities.Any()) { _logger.LogInformation($"AlloyQueryService is processing {expiredImplementationEntities.Count()} expired Implementations."); foreach (var implementationEntity in expiredImplementationEntities) { implementationEntity.EndDate = DateTime.UtcNow; implementationEntity.Status = ImplementationStatus.Ending; implementationEntity.InternalStatus = InternalImplementationStatus.EndQueued; implementationEntity.RunId = null; await alloyContext.SaveChangesAsync(); _logger.LogInformation($"AlloyQueryService is queueing {implementationEntity.Id}."); _implementationQueue.Add(implementationEntity); } } } } } catch (Exception e) { _logger.LogError(e, "Exception encountered in AlloyQueryService loop"); } }