I am using Kentico 13 Core.
I have a custom task that creates or updates Kentico pages based on json data that I want to improve performance on. There are several thousand records in the json object that need to be either created or updated.
My initial thought was to make the scheduled task async to improve performance, but the below code throws an error because ITask does not seem to support async methods.
public async Task<string> Execute(TaskInfo ti)
{
var ItemsToUpsert = fileName.LoadJson<List<CustomItem>>();
List<Task> listOfTasks = new List<Task>();
foreach (var item in ItemsToUpsert)
{
listOfTasks.Add(UpsertItemAsync(item));
}
await Task.WhenAll(listOfTasks);
return returnMessage;
}
I then tried versions of this code that look like the two below, but both throw errors about having multiple datareaders open and then connectionstring changes respectively.
public string Execute(TaskInfo ti)
{
var ItemsToUpsert = fileName.LoadJson<List<CustomItem>>();
Task.Run(Parallel.ForEach(ItemsToUpsert, item =>
{
UpsertColorwayAsync(item);
}));
return returnMessage;
}
Second version
public string Execute(TaskInfo ti)
{
var ItemsToUpsert = fileName.LoadJson<List<CustomItem>>();
Task.Factory.StartNew(CMSThread.Wrap(() => (Parallel.ForEach(ItemsToUpsert, items=>
{
UpsertColorwayAsync(items);
}));
return returnMessage;
}
I have tried to look up the CMS.DataEngine.AsyncWorker class to see if I could use that, but there is limited documentation about it.