public async Task <Event> LaunchEventFromEventTemplateAsync(Guid eventTemplateId, Guid?userId, string username, CancellationToken ct) { // Only an admin can start an Event for a different user than themselves if (userId.HasValue && !(await _authorizationService.AuthorizeAsync(_user, null, new SystemAdminRightsRequirement())).Succeeded) { throw new ForbiddenException(); } if (!userId.HasValue) { userId = _user.GetId(); } if (!(await _authorizationService.AuthorizeAsync(_user, null, new BasicRightsRequirement())).Succeeded) { throw new ForbiddenException(); } // check for resource limitations if (!(await ResourcesAreAvailableAsync(eventTemplateId, userId.Value, ct))) { throw new Exception($"The appropriate resources are not available to create an event from the EventTemplate {eventTemplateId}."); } // make sure the user can launch from the specified eventTemplate var eventTemplate = await _context.EventTemplates.SingleOrDefaultAsync(o => o.Id == eventTemplateId, ct); if (!eventTemplate.IsPublished && !((await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRightsRequirement())).Succeeded || (await _authorizationService.AuthorizeAsync(_user, null, new SystemAdminRightsRequirement())).Succeeded)) { throw new ForbiddenException(); } // create the event from the eventTemplate var eventEntity = await CreateEventEntityAsync(eventTemplateId, userId.Value, username, ct); // add the event to the event queue for AlloyBackgrounsService to process. _alloyEventQueue.Add(eventEntity); return(_mapper.Map <Event>(eventEntity)); }
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 expiredEventEntities = alloyContext.Events.Where(o => o.EndDate == null && o.ExpirationDate < currentDateTime).ToList(); if (expiredEventEntities.Any()) { _logger.LogInformation($"AlloyQueryService is processing {expiredEventEntities.Count()} expired Events."); foreach (var eventEntity in expiredEventEntities) { eventEntity.EndDate = DateTime.UtcNow; eventEntity.Status = EventStatus.Ending; eventEntity.InternalStatus = InternalEventStatus.EndQueued; eventEntity.RunId = null; await alloyContext.SaveChangesAsync(); _logger.LogInformation($"AlloyQueryService is queueing {eventEntity.Id}."); _eventQueue.Add(eventEntity); } } } } _hostedServiceHealthCheck.CompletedRun(); } catch (Exception e) { _logger.LogError(e, "Exception encountered in AlloyQueryService loop"); } }