public void Execute(Job job) { var natsService = new NatsService(); job.StartTime = DateTime.Now; job.Status = ExecutionStatus.Running; Stopwatch sw = new Stopwatch(); var scriptCommands = job.Commands.ToArray(); Logger.Info($"Execute: Begin, Nb Commands: {scriptCommands}"); for (var i = 0; i < scriptCommands.Length; i++) { var scriptCommand = scriptCommands[i]; Logger.Info($"Execute[{i+1}/{scriptCommands.Length}]: {scriptCommand}"); try { scriptCommand.TimeStamp = DateTime.Now; sw.Restart(); scriptCommand.Status = ExecutionStatus.Running; CommandUpdated?.Invoke(scriptCommand); var result = scriptCommand.Execute(natsService, this); sw.Stop(); scriptCommand.Status = ExecutionStatus.Executed; scriptCommand.Result = result; JobUpdated?.Invoke(job); Logger.Info($"Executed: {result}"); } catch (Exception e) { sw.Stop(); Logger.Error($"Command failed ! {scriptCommand}"); scriptCommand.Status = ExecutionStatus.Failed; scriptCommand.Result = e.Message; } scriptCommand.Duration = sw.Elapsed; CommandUpdated?.Invoke(scriptCommand); } job.EndTime = DateTime.Now; job.Status = ExecutionStatus.Executed; Logger.Info($"Execute: End, TotalTime: {job.Duration}"); JobUpdated?.Invoke(job); natsService.Dispose(); var nextJob = Jobs.FirstOrDefault(aJob => aJob.Status == ExecutionStatus.Waiting); if (nextJob != null) { Execute(nextJob); } }
public void Run(Job job) { Logger.Info($"{nameof(Run)}: {job.Commands.Count}"); LastJob = job; job.Status = ExecutionStatus.Waiting; foreach (var scriptCommand in job.Commands) { scriptCommand.Status = ExecutionStatus.Waiting; CommandUpdated?.Invoke(scriptCommand); } Task.Run(() => Execute(job)); }
private void OnCommandUpdated(object sender, EditorCommand editorCommand) { CommandUpdated?.Invoke(this, editorCommand); }