Exemplo n.º 1
0
        public async STT.Task <IEnumerable <SAVM.Result> > ExecuteAsync(Guid id, CancellationToken ct)
        {
            var taskToExecute = await PrepareTaskToExecute(id, ct);

            _taskExecutionQueue.Add(taskToExecute);
            return(new List <SAVM.Result>());
        }
        /// <summary>
        /// Bootstraps (loads data) Tasks that were in process when this api encounters a stop/start cycle
        /// </summary>
        private async STT.Task Bootstrap()
        {
            _logger.LogInformation($"TaskExecutionService is starting Bootstrap.");
            var bootstrapComplete = false;

            while (!bootstrapComplete)
            {
                try
                {
                    using (var scope = _scopeFactory.CreateScope())
                    {
                        var scoringService = scope.ServiceProvider.GetRequiredService <IScoringService>();
                        await scoringService.UpdateAllScores();

                        using (var steamfitterContext = scope.ServiceProvider.GetRequiredService <SteamfitterContext>())
                        {
                            // get tasks that are currently "in process"
                            var queuedTaskEntities = steamfitterContext.Tasks
                                                     .Where(task => task.Results.Any(result => result.Status == TaskStatus.queued)).Include(task => task.Results);

                            if (queuedTaskEntities.Any())
                            {
                                _logger.LogDebug($"TaskExecutionService is queueing {queuedTaskEntities.Count()} Tasks.");
                                foreach (var taskEntity in queuedTaskEntities)
                                {
                                    var userId = taskEntity.Results.Where(result => result.Status == TaskStatus.queued).First().CreatedBy;
                                    taskEntity.UserId = userId;
                                    _taskExecutionQueue.Add(taskEntity);
                                    _logger.LogDebug($"TaskExecutionService is queueing Task {taskEntity.Id}.");
                                }
                            }
                        }
                    }
                    bootstrapComplete = true;
                    _startupHealthCheck.StartupTaskCompleted = true;
                }
                catch (System.Exception ex)
                {
                    _logger.LogError("Exception encountered in TaskExecutionService Bootstrap.", ex);
                    await STT.Task.Delay(new TimeSpan(0, 0, _vmTaskProcessingOptions.CurrentValue.HealthCheckSeconds));
                }
            }
            _logger.LogInformation("TaskExecutionService Bootstrap complete.");
        }
        public async STT.Task <IEnumerable <SAVM.Result> > ExecuteAsync(Guid id, CancellationToken ct)
        {
            if (!(await _authorizationService.AuthorizeAsync(_user, null, new ContentDeveloperRequirement())).Succeeded)
            {
                throw new ForbiddenException();
            }

            var taskToExecute = await _context.Tasks.SingleOrDefaultAsync(v => v.Id == id, ct);

            taskToExecute.CurrentIteration = 0;
            await _context.SaveChangesAsync();

            taskToExecute.UserId = _user.GetId();
            _taskExecutionQueue.Add(taskToExecute);

            return(new List <SAVM.Result>());
        }