Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RunJobAndPollAsync performance improvement #257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions src/ForceToolkitForNET/ForceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,16 @@ public async Task<T> UserInfo<T>(string url)

// BULK METHODS

public async Task<List<BatchInfoResult>> RunJobAsync<T>(string objectName, BulkConstants.OperationType operationType,
public async Task<Queue<BatchInfoResult>> RunJobAsync<T>(string objectName, BulkConstants.OperationType operationType,
IEnumerable<ISObjectList<T>> recordsLists)
{
if (recordsLists == null) throw new ArgumentNullException("recordsLists");

var jobInfoResult = await CreateJobAsync(objectName, operationType);
var batchResults = new List<BatchInfoResult>();
var batchResults = new Queue<BatchInfoResult>();
foreach (var recordList in recordsLists)
{
batchResults.Add(await CreateJobBatchAsync(jobInfoResult, recordList));
batchResults.Enqueue(await CreateJobBatchAsync(jobInfoResult, recordList));
}
await CloseJobAsync(jobInfoResult);
return batchResults;
Expand All @@ -226,45 +226,44 @@ public async Task<List<BatchInfoResult>> RunJobAsync<T>(string objectName, BulkC
public async Task<List<BatchResultList>> RunJobAndPollAsync<T>(string objectName, BulkConstants.OperationType operationType,
IEnumerable<ISObjectList<T>> recordsLists)
{
const float pollingStart = 1000;
const float pollingIncrease = 2.0f;
Int32 pollingStart = 1000;

var batchInfoResults = await RunJobAsync(objectName, operationType, recordsLists);

var currentPoll = pollingStart;
var finishedBatchInfoResults = new List<BatchInfoResult>();


int processCount = batchInfoResults.Count;
while (batchInfoResults.Count > 0)
{
var removeList = new List<BatchInfoResult>();
foreach (var batchInfoResult in batchInfoResults)
var batchInfoResult = batchInfoResults.Dequeue();
var batchInfoResultNew = await PollBatchAsync(batchInfoResult);
if (batchInfoResultNew.State.Equals(BulkConstants.BatchState.Completed.Value()) ||
batchInfoResultNew.State.Equals(BulkConstants.BatchState.Failed.Value()) ||
batchInfoResultNew.State.Equals(BulkConstants.BatchState.NotProcessed.Value()))
{
var batchInfoResultNew = await PollBatchAsync(batchInfoResult);
if (batchInfoResultNew.State.Equals(BulkConstants.BatchState.Completed.Value()) ||
batchInfoResultNew.State.Equals(BulkConstants.BatchState.Failed.Value()) ||
batchInfoResultNew.State.Equals(BulkConstants.BatchState.NotProcessed.Value()))
{
finishedBatchInfoResults.Add(batchInfoResultNew);
removeList.Add(batchInfoResult);
}
}
foreach (var removeItem in removeList)
finishedBatchInfoResults.Add(batchInfoResultNew);
} else
{
batchInfoResults.Remove(removeItem);
batchInfoResults.Enqueue(batchInfoResult);
}
processCount--;

await Task.Delay((int)currentPoll);
currentPoll *= pollingIncrease;
if (batchInfoResults.Count > 0 && processCount == 0)
{
processCount = batchInfoResults.Count;
await Task.Delay(pollingStart =+ pollingStart);
}
}


var batchResults = new List<BatchResultList>();
foreach (var batchInfoResultComplete in finishedBatchInfoResults)
{
batchResults.Add(await GetBatchResultAsync(batchInfoResultComplete));
}
return batchResults;
}

public async Task<JobInfoResult> CreateJobAsync(string objectName, BulkConstants.OperationType operationType)
{
if (string.IsNullOrEmpty(objectName)) throw new ArgumentNullException("objectName");
Expand Down
2 changes: 1 addition & 1 deletion src/ForceToolkitForNET/IForceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public interface IForceClient: IDisposable
Task<T> UserInfo<T>(string url);

// BULK
Task<List<BatchInfoResult>> RunJobAsync<T>(string objectName, BulkConstants.OperationType operationType, IEnumerable<ISObjectList<T>> recordsLists);
Task<Queue<BatchInfoResult>> RunJobAsync<T>(string objectName, BulkConstants.OperationType operationType, IEnumerable<ISObjectList<T>> recordsLists);
Task<List<BatchResultList>> RunJobAndPollAsync<T>(string objectName, BulkConstants.OperationType operationType, IEnumerable<ISObjectList<T>> recordsLists);
Task<JobInfoResult> CreateJobAsync(string objectName, BulkConstants.OperationType operationType);
Task<BatchInfoResult> CreateJobBatchAsync<T>(JobInfoResult jobInfo, ISObjectList<T> recordsObject);
Expand Down