Exemple #1
0
        public override async Task <List <AddJobErrorResult> > BatchAddChildrenAsync(BatchAddJobDto dto,
                                                                                     ExecutionDataflowBlockOptions options = null)
        {
            options ??= new ExecutionDataflowBlockOptions()
            {
                MaxDegreeOfParallelism = Environment.ProcessorCount
            };
            var assignNewJobIdBlock = new ActionBlock <AddJobDto>(
                async child => child.JobId ??= await GetNextIdAsync(), options);

            foreach (var child in dto.Children)
            {
                assignNewJobIdBlock.Post(child);
            }

            assignNewJobIdBlock.Complete();
            await assignNewJobIdBlock.Completion;

            var resp = await SendRequestAsync <List <AddJobErrorResult>, List <AddJobDto> >(HttpMethod.Post,
                                                                                            $"/api/JobTracker/newBatch/{dto.ParentJobId}", dto.Children);

            if (resp.Result)
            {
                return(resp.Data);
            }

            throw new Exception($"{nameof(BatchAddChildrenAsync)} failed {resp.Msg}");
        }
        public async Task TestDescendantsCountInBatchAsync()
        {
            var root = await _client.CreateNewJobAsync(new AddJobDto()
            {
                TrackJobCount = true
            });

            await _client.CreateNewJobAsync(new AddJobDto($"--", root.JobId));

            var addSubDto = new BatchAddJobDto()
            {
                Children    = Enumerable.Range(0, 10).Select(s => new AddJobDto(s.ToString())).ToList(),
                ParentJobId = root.JobId
            };
            var errorRes = await _client.BatchAddChildrenAsync(addSubDto);

            addSubDto = new BatchAddJobDto()
            {
                Children    = Enumerable.Range(0, 10).Select(s => new AddJobDto(s.ToString())).ToList(),
                ParentJobId = root.JobId
            };
            await _client.BatchAddChildrenAsync(addSubDto);

            var first = addSubDto.Children.First().JobId;
            await _client.CreateNewJobAsync(new AddJobDto("--", first));

            var count = await _client.GetDescendantsCountAsync(root.JobId);

            Assert.AreEqual(false, errorRes.Any());
            Assert.AreEqual(23, count);
        }
        public async Task TestBatchFlowSubJobsAsync()
        {
            var root = await _client.CreateNewJobAsync(new AddJobDto("root"));

            var addSubDto = new BatchAddJobDto()
            {
                Children    = Enumerable.Range(0, 1000).Select(s => new AddJobDto(s.ToString())).ToList(),
                ParentJobId = root.JobId
            };
            var errorRes = await _client.BatchAddChildrenAsync(addSubDto);

            Assert.AreEqual(false, errorRes.Any());
            var updateJobBlock = new ActionBlock <long>(async jobId =>
            {
                await _client.UpdateJobStatesAsync(jobId, new UpdateJobStateDto(JobState.Running));
                await _client.UpdateJobStatesAsync(jobId, new UpdateJobStateDto(JobState.RanToCompletion));
            },
                                                        Helper.GetOutOfGrainExecutionOptions());

            foreach (var child in addSubDto.Children)
            {
                await updateJobBlock.PostToBlockUntilSuccessAsync(child.JobId.Value);
            }

            updateJobBlock.Complete();
            await updateJobBlock.Completion;
            await _client.UpdateJobStatesAsync(root.JobId, new UpdateJobStateDto(JobState.Running));

            await _client.UpdateJobStatesAsync(root.JobId, new UpdateJobStateDto(JobState.RanToCompletion));
        }
        public async Task <List <AddJobErrorResult> > BatchAddJobAsync(BatchAddJobDto dto)
        {
            var addErrorResult = new ConcurrentBag <AddJobErrorResult>();
            var parentGrain    = _client.GetGrain <IJobGrain>(dto.ParentJobId);
            var parent         = await parentGrain.GetJobAsync();

            var idGrain          = _client.GetGrain <IJobIdGrain>(Constants.JobIdGrainDefaultName);
            var createChildBlock = new ActionBlock <AddJobDto>(async child =>
            {
                child.JobId ??= await idGrain.GetNewIdAsync();
                child.ParentJobId = dto.ParentJobId;
                var childGrain    = _client.GetGrain <IJobGrain>(child.JobId.Value);
                var addChildError =
                    await childGrain.AddJobFromParentAsync(child, parent.AncestorJobId, parent.TrackCountRef);
                if (addChildError != null)
                {
                    addErrorResult.Add(addChildError);
                }
            }, Helper.GetOutOfGrainExecutionOptions());

            foreach (var child in dto.Children)
            {
                await createChildBlock.PostToBlockUntilSuccessAsync(child);
            }

            createChildBlock.Complete();
            await createChildBlock.Completion;

            // ReSharper disable once PossibleInvalidOperationException
            await parentGrain.BatchInitChildrenAsync(dto.Children.Select(s => s.JobId.Value).ToList());

            return(addErrorResult.ToList());
        }
        public async Task TestBatchAddSubJobsAsync()
        {
            var root = await _client.CreateNewJobAsync(new AddJobDto("root"));

            var addSubDto = new BatchAddJobDto()
            {
                Children    = Enumerable.Range(0, 500).Select(s => new AddJobDto(s.ToString())).ToList(),
                ParentJobId = root.JobId
            };
            var errorRes = await _client.BatchAddChildrenAsync(addSubDto);

            Assert.AreEqual(false, errorRes.Any());
        }
Exemple #6
0
 public abstract Task <List <AddJobErrorResult> > BatchAddChildrenAsync(BatchAddJobDto dto,
                                                                        ExecutionDataflowBlockOptions options = null);