Beispiel #1
0
        public async Task <IResult> ExecuteAll(ICriteria criteria, CancellationToken token)
        {
            logger.LogDebug("Starting job runner...");
            try
            {
                if (!criteria.IsProper())
                {
                    throw new Exception("Invalid Criteria! Aborting...");
                }
                var scriptEngine     = scriptEngineProvider.ScriptEngine;
                var activeParentJobs = await jobLoader.LoadAllParentJobsAsync(criteria);

                var eligibleJobs = activeParentJobs.OrderBy(job => job.Priority);
                logger.LogInformation($"The following jobs will run : {string.Join(",", eligibleJobs.Select(job => job.Name))}");
                Parallel.ForEach(eligibleJobs,
                                 new ParallelOptions {
                    MaxDegreeOfParallelism = options.ConcurrentNumberOfJobs
                },
                                 job =>
                {
                    ConnectAndExecute(job, scriptEngine, abortOnFailure: true);
                });
                logger.LogInformation($"COMPLETED {eligibleJobs.Count()} jobs.");
                logger.LogInformation($"COMPLETED lambda execution.");
            }
            catch (Exception ex)
            {
                var exceptionBuilder = new System.Text.StringBuilder();
                exceptionBuilder.AppendLine($"Event: {criteria}");
                exceptionBuilder.AppendLine($"Time Remaining: {context.RemainingTime}");
                if (ex is AggregateException ae)
                {
                    foreach (var exception in ae.InnerExceptions)
                    {
                        exceptionBuilder.AppendLine(exception.GetType().Name + " : " + exception.Message);
                    }
                }
                else
                {
                    exceptionBuilder.AppendLine(ex.Message);
                }

                string detailedExceptionMessage = exceptionBuilder.ToString();
                logger.LogError(detailedExceptionMessage);
                await notificationService.NotifyFailureAsync(detailedExceptionMessage);

                return(new Result
                {
                    Success = false,
                    Message = detailedExceptionMessage
                });
            }
            return(new Result
            {
                Success = true
            });
        }