Exemplo n.º 1
0
 protected async Task <IEnumerable <Job> > QueueJobs(params JobCreateModel[] jobs)
 => await _jobManagement.QueueJobs(jobs);
Exemplo n.º 2
0
        private async Task <IEnumerable <Job> > CreateGenerateAllocationJobs(IEnumerable <IDictionary <string, string> > jobProperties, string specificationId)
        {
            bool calculationEngineRunning = await _calculationEngineRunningChecker.IsCalculationEngineRunning(Job.SpecificationId, new string[] { DefinitionNames.RefreshFundingJob });

            if (calculationEngineRunning)
            {
                throw new RefreshJobRunningException($"Can not create job for specification: {Job.SpecificationId} as there is an existing Refresh Funding Job running for it. Please wait for that job to finish.");
            }

            HashSet <string> calculationsToAggregate = new HashSet <string>();

            if (Job.JobDefinitionId == JobConstants.DefinitionNames.CreateInstructGenerateAggregationsAllocationJob)
            {
                string calculationAggregatesCacheKeyPrefix = $"{CacheKeys.CalculationAggregations}{Job.SpecificationId}";

                await _cacheProvider.RemoveByPatternAsync(calculationAggregatesCacheKeyPrefix);

                IEnumerable <Models.Calcs.Calculation> calculations = await _calculationsRepository.GetCalculationsBySpecificationId(Job.SpecificationId);

                foreach (Models.Calcs.Calculation calculation in calculations)
                {
                    IEnumerable <string> aggregateParameters = SourceCodeHelpers.GetCalculationAggregateFunctionParameters(calculation.Current.SourceCode);
                    if (aggregateParameters.IsNullOrEmpty())
                    {
                        continue;
                    }

                    foreach (string aggregateParameter in aggregateParameters)
                    {
                        Models.Calcs.Calculation referencedCalculation = calculations.FirstOrDefault(m => string.Equals(_typeIdentifierGenerator.GenerateIdentifier(m.Name.Trim()), aggregateParameter.Trim(), StringComparison.InvariantCultureIgnoreCase));

                        if (referencedCalculation != null)
                        {
                            calculationsToAggregate.Add(aggregateParameter);
                        }
                    }
                }
            }

            IList <JobCreateModel> jobCreateModels = new List <JobCreateModel>();

            Trigger trigger = new Trigger
            {
                EntityId   = Job.Id,
                EntityType = nameof(Job),
                Message    = $"Triggered by parent job with id: '{Job.Id}"
            };

            int    batchNumber      = 1;
            int    batchCount       = jobProperties.Count();
            string calcsToAggregate = string.Join(",", calculationsToAggregate);

            foreach (IDictionary <string, string> properties in jobProperties)
            {
                properties.Add("batch-number", batchNumber.ToString());
                properties.Add("batch-count", batchCount.ToString());
                properties.Add("calculations-to-aggregate", calcsToAggregate);
                properties.Add("session-id", $"{specificationId}-{batchNumber}");

                JobCreateModel jobCreateModel = new JobCreateModel
                {
                    InvokerUserDisplayName = Job.InvokerUserDisplayName,
                    InvokerUserId          = Job.InvokerUserId,
                    JobDefinitionId        = Job.JobDefinitionId == JobConstants.DefinitionNames.CreateInstructAllocationJob ? JobConstants.DefinitionNames.CreateAllocationJob : JobConstants.DefinitionNames.GenerateCalculationAggregationsJob,
                    SpecificationId        = Job.SpecificationId,
                    Properties             = properties,
                    ParentJobId            = Job.Id,
                    Trigger       = trigger,
                    CorrelationId = Job.CorrelationId
                };

                batchNumber++;

                jobCreateModels.Add(jobCreateModel);
            }

            return(await _jobManagement.QueueJobs(jobCreateModels));
        }