public void Update(JobDto dto) { try { if (jobs.TryGetValue(dto.Id, out var job)) { job.State = dto.State; job.ExecutedDate = DateTime.Now; JobUpdated?.Invoke(job); logger.LogDebug($"{job}"); if (job.State != JobState.Finished && job.State != JobState.Error) { return; } JobFinished?.Invoke(job); Clean(); } else { logger.LogWarning($"Can't find job with Id: {dto.Id}"); } } catch (Exception exception) { logger.LogError($"Update error: {dto}, {exception.Message}"); } }
public void Reschedule(Guid id, TimeSpan interval) { if (!jobs.TryGetValue(id, out var job)) { return; } job.State = JobState.New; job.ScheduledDate = DateTime.Now.Add(interval); JobUpdated?.Invoke(job); }
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); } }