We are indexing documents into Solr using Solrnet in asp.net c# project. We are having requirement where Solr DIH can not be used, so we are indexing products in certain batches to Solr using following code:
decimal cycleCount = ProductCount / batchSize;
for (int i = 0; i <= Math.Round(cycleCount); i++)
{
var Products = Products(batchSize, languageId, storeId).ToList();
solrCustomWorker.Add(solrProducts);
solrCustomWorker.Commit();
}
With huge document size, it takes lot of time (most of times it takes few hours) to complete whole process, and sometimes we are having requirement to stop this process in-between by manual intervention.
However, I am not sure how to stop this indexing batch cycles in between before it completes. A single cycle with large batch size of documents, takes few seconds to complete and then it commit. But considering huge no. of documents, while performing full indexing it takes few hours and we're unable to stop this process in between.
Any idea - how can I stop this process in-between.. I'm unable to figure out what should be done here?
Please suggest.
In situations where we want to stop the iteration before getting to the last item or before a given condition is met, we can use the break statement. The break statement will have its own condition – this tells it when to "break" the loop.
Python provides two keywords that terminate a loop iteration prematurely: The Python break statement immediately terminates a loop entirely. Program execution proceeds to the first statement following the loop body. The Python continue statement immediately terminates the current loop iteration.
You use the break statement to terminate a loop early such as the while loop or the for loop. If there are nested loops, the break statement will terminate the innermost loop. You can also use the break statement to terminate a switch statement or a labeled statement.
Two approaches you can take here are:
1 Use a global variable (this is not a good solution though, hopefully for obvious reasons):
public static bool KeepRunning;
...
for (int i = 0; i <= Math.Round(cycleCount); i++)
{
if (KeepRunning)
{
var Products = Products(batchSize, languageId, storeId).ToList();
solrCustomWorker.Add(solrProducts);
solrCustomWorker.Commit();
}
}
2 Use a callback to check whether to keep running:
public void SomeMethod(Func<bool> keepRunning)
{
for (int i = 0; i <= Math.Round(cycleCount); i++)
{
if (keepRunning())
{
var Products = Products(batchSize, languageId, storeId).ToList();
solrCustomWorker.Add(solrProducts);
solrCustomWorker.Commit();
}
}
}
The advantage of the second approach is that you decouple the decision logic from the indexing logic and avoid global variables, eg by capturing whether to keep running or not inside a closure around an async call to the long running process and an event handler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With