public virtual void ScriptFinished(ScriptFinishedEventArgs.ScriptFinishedResult result, string error = null) { engineLogger.Information("Result Code: " + result.ToString()); if (error == null) { engineLogger.Information("Error: None"); if (taskModel != null && serverSettings.ServerConnectionEnabled) { HttpServerClient.UpdateTask(taskModel.TaskID, "Completed", "Script Completed Successfully"); } } else { engineLogger.Information("Error: " + error); if (taskModel != null) { HttpServerClient.UpdateTask(taskModel.TaskID, "Error", error); } } engineLogger.Dispose(); CurrentStatus = EngineStatus.Finished; ScriptFinishedEventArgs args = new ScriptFinishedEventArgs(); args.LoggedOn = DateTime.Now; args.Result = result; args.Error = error; args.ExecutionTime = sw.Elapsed; args.FileName = FileName; Core.Sockets.SocketClient.SendExecutionLog("Result Code: " + result.ToString()); Core.Sockets.SocketClient.SendExecutionLog("Total Execution Time: " + sw.Elapsed); //convert to json var serializedArguments = Newtonsoft.Json.JsonConvert.SerializeObject(args); //write execution metrics if ((engineSettings.TrackExecutionMetrics) && (FileName != null)) { var summaryLogger = new Logging().CreateJsonLogger("Execution Summary", Serilog.RollingInterval.Infinite); summaryLogger.Information(serializedArguments); summaryLogger.Dispose(); } Core.Client.EngineBusy = false; if (serverSettings.ServerConnectionEnabled) { HttpServerClient.CheckIn(); } ScriptFinishedEvent?.Invoke(this, args); }
public void ExecuteScript(string data, bool dataIsFile) { Core.Client.EngineBusy = true; try { CurrentStatus = EngineStatus.Running; //create stopwatch for metrics tracking sw = new System.Diagnostics.Stopwatch(); sw.Start(); //log starting ReportProgress("Bot Engine Started: " + DateTime.Now.ToString()); //get automation script Core.Script.Script automationScript; if (dataIsFile) { ReportProgress("Deserializing File"); engineLogger.Information("Script Path: " + data); FileName = data; automationScript = Core.Script.Script.DeserializeFile(data); } else { ReportProgress("Deserializing XML"); automationScript = Core.Script.Script.DeserializeXML(data); } if (serverSettings.ServerConnectionEnabled && taskModel == null) { taskModel = HttpServerClient.AddTask(data); } else if (serverSettings.ServerConnectionEnabled && taskModel != null) { taskModel = HttpServerClient.UpdateTask(taskModel.TaskID, "Running", "Running Server Assignment"); } //track variables and app instances ReportProgress("Creating Variable List"); VariableList = automationScript.Variables; ReportProgress("Creating App Instance Tracking List"); //create app instances and merge in global instances this.AppInstances = new Dictionary <string, object>(); var GlobalInstances = GlobalAppInstances.GetInstances(); foreach (var instance in GlobalInstances) { this.AppInstances.Add(instance.Key, instance.Value); } //execute commands foreach (var executionCommand in automationScript.Commands) { if (IsCancellationPending) { ReportProgress("Cancelling Script"); ScriptFinished(ScriptFinishedEventArgs.ScriptFinishedResult.Cancelled); return; } ExecuteCommand(executionCommand); } if (IsCancellationPending) { //mark cancelled - handles when cancelling and user defines 1 parent command or else it will show successful ScriptFinished(ScriptFinishedEventArgs.ScriptFinishedResult.Cancelled); } else { //mark finished ScriptFinished(ScriptFinishedEventArgs.ScriptFinishedResult.Successful); } } catch (Exception ex) { ScriptFinished(ScriptFinishedEventArgs.ScriptFinishedResult.Error, ex.ToString()); } }