internal static void UpdatePaging(IOrganizationService service, Guid jobId, JobPagingInfo pagingInfo) { service.Update( new CustomJob { Id = jobId, PageNumber = pagingInfo.NextPage, PagingCookie = pagingInfo.Cookie }); }
protected override JobRunStatus ProcessSuccessPaging(JobPagingInfo pagingInfo) { log.Log($"Updating paging info of job, page {pagingInfo.NextPage} ..."); UpdatePaging(Service, Job.Id, pagingInfo); log.Log($"Updated paging info of job, page {pagingInfo.NextPage}."); log.Log($"Updating status of job to 'waiting' ..."); SetStatus(Service, CustomJob.StatusReasonEnum.Waiting, Job.Id, false); log.Log($"Updated status of job to 'waiting."); return (new JobRunStatus { IsSuccess = true, LatestRunMessage = $"Page {Job.PageNumber} was successful." }); }
protected override JobPagingInfo GetTargets() { var jobPagingInfo = new JobPagingInfo(); var targets = new List <Guid>(); if (!string.IsNullOrEmpty(Job.TargetLogicalName)) { Log.Log("Converting FetchXML to QueryExpression ..."); var query = ((FetchXmlToQueryExpressionResponse) Service.Execute( new FetchXmlToQueryExpressionRequest { FetchXml = Job.TargetXML })).Query; Log.Log("Converted."); query.PageInfo = new PagingInfo { Count = Job.RecordsPerPage ?? 5000, PageNumber = Job.PageNumber ?? 1, PagingCookie = Job.PagingCookie }; Log.Log($"Retrieving a max of {query.PageInfo.Count} per page ..."); EntityCollection result; do { Log.Log($"Retrieving page {query.PageInfo.PageNumber} ..."); result = Service.RetrieveMultiple(query); targets.AddRange(result.Entities.Select(entity => entity.Id)); Log.Log($"Found {result.Entities.Count}."); jobPagingInfo.NextPage = query.PageInfo.PageNumber += 1; jobPagingInfo.Cookie = query.PageInfo.PagingCookie = result.PagingCookie; }while (result.MoreRecords && Job.PageNumber == null); } jobPagingInfo.Targets = targets; Log.Log($"Target count: {targets.Count}."); return(jobPagingInfo); }
protected override JobRunStatus ProcessSuccessPaging(JobPagingInfo pagingInfo) { DeleteSuccessfulFailed(); if (IsSubJob()) { var parentId = Job.ParentJob.Value; if (IsWaitingOnSubJobs(Service, parentId)) { Log.Log($"Updating paging info of parent job, page {pagingInfo.NextPage} ..."); UpdatePaging(Service, parentId, pagingInfo); Log.Log($"Updated paging info of parent job, page {pagingInfo.NextPage}."); } } return(ProcessSuccess()); }
protected override JobRunStatus ProcessFailurePaging(ExecutionFailures failures, JobPagingInfo pagingInfo) { List <CustomJobFailedTarget> newFailures; var status = new JobRunStatus(); if (IsNoRetry()) { newFailures = AssociateFailedTargets(failures, Job.Id); if (Job.FailureAction != null) { log.Log("Running failure action ..."); try { Service.Execute( new ExecuteWorkflowRequest { EntityId = Job.Id, WorkflowId = Job.FailureAction.Value }); } catch (Exception exception) { log.Log(exception); } log.Log("Finished running failure action."); } if (Job.IgnoreFailure == true) { status = ProcessSuccessPaging(pagingInfo); status.RunTargetFailures = newFailures; if (IsSubJob()) { status.ParentId = Job.ParentJob.Value; } return(status); } if (IsSubJob()) { var parentId = Job.ParentJob.Value; if (IsWaitingOnSubJobs(Service, parentId)) { log.Log($"Updating status of job parent '{parentId}' to 'failed' ..."); Close(Service, CustomJob.StatusReasonEnum.Failure, parentId, true); log.Log($"Updated status of job parent '{parentId}' to 'failed'."); } status.ParentId = parentId; } status.IsClose = true; } else { log.Log($"Creating a retry sub-job ..."); var extendingJob = BuildSubJob(Job, CustomJob.StatusReasonEnum.Retry, $"Retry Page {Job.PageNumber ?? 1}"); extendingJob.CurrentRetryRun = 1; extendingJob.Id = Service.Create(extendingJob); log.Log($"Created a retry sub-job '{extendingJob.Name}'."); newFailures = AssociateFailedTargets(failures, extendingJob.Id); log.Log($"Updating status of job 'waiting on subs' ..."); SetStatus(Service, CustomJob.StatusReasonEnum.WaitingOnSubJobs, Job.Id, false); log.Log($"Updating status of job 'waiting on subs'."); } status.IsSuccess = false; status.LatestRunMessage = $"Page {Job.PageNumber} failed."; status.RunTargetFailures = newFailures; return(status); }
protected override JobRunStatus ProcessFailurePaging(ExecutionFailures failures, JobPagingInfo pagingInfo) { var remainingFailed = DeleteSuccessfulFailed(failures); var status = new JobRunStatus(); if (IsNoRetry()) { if (IsSubJob()) { var parentId = Job.ParentJob.Value; AssociateFailedToParent(remainingFailed); if (IsWaitingOnSubJobs(Service, parentId)) { if (Job.IgnoreFailure == true) { Log.Log($"Updating paging info of job, page {pagingInfo.NextPage} ..."); UpdatePaging(Service, parentId, pagingInfo); Log.Log($"Updated paging info of job, page {pagingInfo.NextPage}."); Log.Log($"Updating status of job parent '{parentId}' to 'waiting' ..."); SetStatus(Service, CustomJob.StatusReasonEnum.Waiting, parentId, IsParentRecurrentJob()); Log.Log($"Updated status of job parent '{parentId}' to 'waiting'."); } else { Log.Log($"Updating status of job parent '{parentId}' to 'failed' ..."); Close(Service, CustomJob.StatusReasonEnum.Failure, Job.ParentJob.Value, true); Log.Log($"Updated status of job parent '{parentId}' to 'failed'."); } } } status.IsClose = true; } else { if (Job.RetrySchedule != null) { UpdateRetryTargetDate(Service, Job, Log); } IncrementRetry(Job.CurrentRetryRun ?? 0); Log.Log($"Updating status of job to 'retry' ..."); SetStatus(Service, CustomJob.StatusReasonEnum.Retry, Job.Id, false); Log.Log($"Updated status of job 'retry'."); } status.IsSuccess = false; status.LatestRunMessage = $"Retry run failed."; return(status); }