/// <summary> /// Method which takes IEnumerable with the data and calls the assigned line action on every line of the data. /// The data needs to fit inside the line action. /// This method is meant to be used when mining from command line /// </summary> /// <param name="data">Enumerable with the data lines</param> /// <param name="lineAction">Action which will be executed on each line</param> protected void UpdateDatabase <T>(IEnumerable <T> data, LineAction <T> lineAction) { // Writeout the delegate name System.Console.WriteLine(lineAction.GetMethodInfo().Name); bool successfull = false; do { var numberOfTries = 1; try { //Execute query -- retrieve collection only once System.Console.WriteLine("Querying Endpoind"); var listData = data.ToList(); System.Console.WriteLine("Updating database"); using (var db = new BookRecommenderContext()) { //Create new console progress counter using (var counter = new Counter(listData.Count)) { // Insert all books in database foreach (var line in listData) { lineAction(line, db); counter.Update(); } } System.Console.WriteLine("Saving database"); db.SaveChanges(); successfull = true; } } catch (Exception ex) { // If something went wrong, wait 10 sec and then try again numberOfTries++; System.Console.WriteLine(ex.ToString()); System.Console.WriteLine("Try again, attempt number " + numberOfTries); } // If something went wrong, wait 10 sec and then try again if (!successfull) { System.Threading.Tasks.Task.Delay(10000).Wait(); } } while (!successfull); }
/// <summary> /// Method which takes IEnumerable with the data and calls the assigned line action on every line of the data. /// The data needs to fit inside the line action. /// This method is meant to be used when mining from web interface /// </summary> /// <param name="data">Enumerable with the data lines</param> /// <param name="lineAction">Action which will be executed on each line</param> /// <param name="miningState">Mining state of the operation from the MiningProxySingleton used to monitor the data mining</param> protected void UpdateDatabase <T>(IEnumerable <T> data, LineAction <T> lineAction, MiningState miningState) { // fall back for deprecated commandline mining if (miningState == null) { UpdateDatabase <T>(data, lineAction); return; } try { // set the mining state miningState.CurrentState = MiningStateType.RunningQueryingEndpoint; var listData = data.ToList(); miningState.CurrentState = MiningStateType.Running; var currentPosition = 0; using (var db = new BookRecommenderContext()) { // proccess each line using the line action and log changes to the mining state foreach (var line in listData) { lineAction(line, db); currentPosition++; miningState.Message = String.Format("{0}/{1}", currentPosition, listData.Count); } miningState.CurrentState = MiningStateType.RunningSavingToDatabase; db.SaveChanges(); miningState.CurrentState = MiningStateType.Completed; miningState.Message = DateTime.Now.ToString(); } } catch (Exception ex) { // If something went wrong, wait 10 sec and then try again miningState.CurrentState = MiningStateType.Error; miningState.Message = ex.Message; } }