/// <summary> /// Calculating winner according this algorythm /// https://en.wikipedia.org/wiki/Alias_method and /// http://code.activestate.com/recipes/576564-walkers-alias-method-for-random-objects-with-diffe/ /// </summary> /// <returns></returns> public Runner CalculateWinner() { double sumOfAllChances = Runners.Sum(x => x.Chance(Margin)); double sumU = Math.Ceiling(sumOfAllChances); var pick = rnd.Next((int)sumU); double sum = 0; Runner res = new Runner(); foreach (var item in Runners) { sum += item.Chance(Margin); if (sum >= pick) { res = item; Runners[Runners.IndexOf(res)].winCount++; break; } } return(res); }
public async Task RunRunset(bool doContinueRun = false) { try { mRunSetConfig.IsRunning = true; //reset run if (doContinueRun == false) { if (WorkSpace.Instance.RunningInExecutionMode == false || RunSetConfig.ExecutionID == null) { RunSetConfig.ExecutionID = Guid.NewGuid(); } else { if (mSelectedExecutionLoggerConfiguration.PublishLogToCentralDB == ExecutionLoggerConfiguration.ePublishToCentralDB.Yes && !string.IsNullOrEmpty(WorkSpace.Instance.Solution.LoggerConfigurations.CentralLoggerEndPointUrl)) { AccountReportApiHandler accountReportApiHandler = new AccountReportApiHandler(WorkSpace.Instance.Solution.LoggerConfigurations.CentralLoggerEndPointUrl); bool isValidated = accountReportApiHandler.ExecutionIdValidation((Guid)RunSetConfig.ExecutionID); if (!isValidated) { RunSetConfig.ExecutionID = Guid.NewGuid(); Reporter.ToLog(eLogLevel.WARN, string.Format("Duplicate execution id used, creating new execution id : {0}", RunSetConfig.ExecutionID)); } } } RunSetConfig.LastRunsetLoggerFolder = "-1"; // !!!!!!!!!!!!!!!!!! Reporter.ToLog(eLogLevel.INFO, string.Format("Reseting {0} elements", GingerDicser.GetTermResValue(eTermResKey.RunSet))); mStopwatch.Reset(); ResetRunnersExecutionDetails(); } else { RunSetConfig.LastRunsetLoggerFolder = null; } mStopRun = false; //configure Runners for run Reporter.ToLog(eLogLevel.INFO, string.Format("Configuring {0} elements for execution", GingerDicser.GetTermResValue(eTermResKey.RunSet))); ConfigureAllRunnersForExecution(); //Process all pre execution Run Set Operations if (doContinueRun == false) { RunSetConfig.StartTimeStamp = DateTime.UtcNow; Reporter.ToLog(eLogLevel.INFO, string.Format("Running Pre-Execution {0} Operations", GingerDicser.GetTermResValue(eTermResKey.RunSet))); WorkSpace.Instance.RunsetExecutor.ProcessRunSetActions(new List <RunSetActionBase.eRunAt> { RunSetActionBase.eRunAt.ExecutionStart, RunSetActionBase.eRunAt.DuringExecution }); } if (mSelectedExecutionLoggerConfiguration != null && mSelectedExecutionLoggerConfiguration.PublishLogToCentralDB == ePublishToCentralDB.Yes && mSelectedExecutionLoggerConfiguration.DataPublishingPhase == ExecutionLoggerConfiguration.eDataPublishingPhase.DuringExecution && Runners.Count > 0) { await((GingerExecutionEngine)Runners[0].Executor).Centeralized_Logger.RunSetStart(RunSetConfig); } //Start Run if (doContinueRun == false) { Reporter.ToLog(eLogLevel.INFO, string.Format("########################## {0} Execution Started: '{1}'", GingerDicser.GetTermResValue(eTermResKey.RunSet), RunSetConfig.Name)); SetRunnersExecutionLoggerConfigs();//contains ExecutionLogger.RunSetStart() } else { Reporter.ToLog(eLogLevel.INFO, string.Format("########################## {0} Execution Continuation: '{1}'", GingerDicser.GetTermResValue(eTermResKey.RunSet), RunSetConfig.Name)); } mStopwatch.Start(); Reporter.ToLog(eLogLevel.INFO, string.Format("######## {0} Runners Execution Started", GingerDicser.GetTermResValue(eTermResKey.RunSet))); List <Task> runnersTasks = new List <Task>(); if (RunSetConfig.RunModeParallel) { foreach (GingerRunner GR in Runners) { if (mStopRun) { return; } Task t = new Task(() => { if (doContinueRun == false) { GR.Executor.RunRunner(); } else if (GR.Status == Amdocs.Ginger.CoreNET.Execution.eRunStatus.Stopped) //we continue only Stopped Runners { GR.Executor.ResetRunnerExecutionDetails(doNotResetBusFlows: true); //reset stopped runners only and not their BF's GR.Executor.ContinueRun(eContinueLevel.Runner, eContinueFrom.LastStoppedAction); } }, TaskCreationOptions.LongRunning); runnersTasks.Add(t); t.Start(); // Wait one second before starting another runner Thread.Sleep(1000); } } else { //running sequentially Task t = new Task(() => { foreach (GingerRunner GR in Runners) { if (mStopRun) { return; } if (doContinueRun == false) { GR.Executor.RunRunner(); } else { //continue if (GR.Status == Amdocs.Ginger.CoreNET.Execution.eRunStatus.Stopped) //we continue only Stopped Runners { GR.Executor.ResetRunnerExecutionDetails(doNotResetBusFlows: true); //reset stopped runners only and not their BF's GR.Executor.ContinueRun(eContinueLevel.Runner, eContinueFrom.LastStoppedAction); } else if (GR.Status == Amdocs.Ginger.CoreNET.Execution.eRunStatus.Pending)//continue the runners flow { GR.Executor.RunRunner(); } } if (GR.Status == eRunStatus.Failed && mRunSetConfig.StopRunnersOnFailure) { //marking next Runners as blocked and stopping execution for (int indx = Runners.IndexOf(GR) + 1; indx < Runners.Count; indx++) { Runners[indx].Executor.SetNextBusinessFlowsBlockedStatus(); Runners[indx].Status = eRunStatus.Blocked; Runners[indx].Executor.GiveUserFeedback();//for getting update for runner stats on runset page } break; } // Wait one second before starting another runner Thread.Sleep(1000); } }, TaskCreationOptions.LongRunning); runnersTasks.Add(t); t.Start(); } Task.WaitAll(runnersTasks.ToArray()); mStopwatch.Stop(); RunSetConfig.Elapsed = mStopwatch.ElapsedMilliseconds; RunSetConfig.EndTimeStamp = DateTime.UtcNow; //Do post execution items Reporter.ToLog(eLogLevel.INFO, string.Format("######## {0} Runners Execution Ended", GingerDicser.GetTermResValue(eTermResKey.RunSet))); //ExecutionLoggerManager.RunSetEnd(); Runners[0].Executor.ExecutionLoggerManager.RunSetEnd(); if (mSelectedExecutionLoggerConfiguration != null && mSelectedExecutionLoggerConfiguration.PublishLogToCentralDB == ePublishToCentralDB.Yes && mSelectedExecutionLoggerConfiguration.DataPublishingPhase == ExecutionLoggerConfiguration.eDataPublishingPhase.DuringExecution && Runners.Count > 0) { await((GingerExecutionEngine)Runners[0].Executor).Centeralized_Logger.RunSetEnd(RunSetConfig); } if (mStopRun == false) { // Process all post execution RunSet Operations Reporter.ToLog(eLogLevel.INFO, string.Format("######## Running Post-Execution {0} Operations", GingerDicser.GetTermResValue(eTermResKey.RunSet))); WorkSpace.Instance.RunsetExecutor.ProcessRunSetActions(new List <RunSetActionBase.eRunAt> { RunSetActionBase.eRunAt.ExecutionEnd }); } Reporter.ToLog(eLogLevel.INFO, string.Format("######## Creating {0} Execution Report", GingerDicser.GetTermResValue(eTermResKey.RunSet))); CreateGingerExecutionReportAutomaticly(); Reporter.ToLog(eLogLevel.INFO, string.Format("######## Doing {0} Execution Cleanup", GingerDicser.GetTermResValue(eTermResKey.RunSet))); CloseAllEnvironments(); Reporter.ToLog(eLogLevel.INFO, string.Format("########################## {0} Execution Ended", GingerDicser.GetTermResValue(eTermResKey.RunSet))); if (mSelectedExecutionLoggerConfiguration.DataPublishingPhase == ExecutionLoggerConfiguration.eDataPublishingPhase.PostExecution) { await Runners[0].Executor.ExecutionLoggerManager.PublishToCentralDBAsync(RunSetConfig.LiteDbId, RunSetConfig.ExecutionID ?? Guid.Empty); } } finally { mRunSetConfig.IsRunning = false; } }