public IResponse <NoValue> EditSchedule(ScheduleModel model, IEnumerable <DateTime> dates) { var result = new Response <NoValue>(); try { var schedule = _entity.Schedule.Find(model.Id); //check if at least one job has been started var jobsExecuted = schedule.JobTimeline.Any(jt => jt.JobStatusId != 2); if (jobsExecuted) { result.Status = Common.Enums.StatusEnum.Error; result.Message = "Schedule cannot be updated if at least one job has been started."; return(result); } foreach (var job in schedule.JobTimeline.ToList()) { //delete from Hangfire var deletedHangfireJob = BackgroundJob.Delete(job.HangfireId); _entity.JobHistory.RemoveRange(job.JobHistory); } _entity.JobTimeline.RemoveRange(schedule.JobTimeline); schedule.Title = model.Title; schedule.StartDate = model.StartDate.ToUniversalTime(); schedule.EndDate = model.EndDate; schedule.UserProfileId = model.UserProfileId; schedule.JobDefinitionId = model.JobDefinitionId; schedule.RecurrenceRule = model.RecurrenceRule; schedule.Description = model.Description; schedule.CreatedAt = DateTime.UtcNow; if (dates != null) { var jobexecutiondates = dates.Select(d => new JobTimeline { ScheduleId = schedule.Id, JobStatusId = (int)Common.Enums.JobStatus.Pending, StartTime = d.ToUniversalTime() }).ToList(); _entity.JobTimeline.AddRange(jobexecutiondates); foreach (var jobExecutionDate in jobexecutiondates) { jobExecutionDate.JobHistory.Add(new JobHistory { JobTimelineId = jobExecutionDate.Id, UpdatedAt = DateTime.UtcNow, JobStatusId = (int)Common.Enums.JobStatus.Pending, Message = "Your job is waiting for execution" }); } } else { var job = new JobTimeline { ScheduleId = schedule.Id, JobStatusId = (int)Common.Enums.JobStatus.Pending, StartTime = schedule.StartDate.ToUniversalTime() }; schedule.JobTimeline.Add(job); _entity.JobHistory.Add(new JobHistory { JobTimelineId = job.Id, UpdatedAt = DateTime.UtcNow, JobStatusId = (int)Common.Enums.JobStatus.Pending, Message = "Your job is waiting for execution" }); } _entity.SaveChanges(); result.Message = "Schedule has been updated successfully."; result.Status = Common.Enums.StatusEnum.Success; //sync jobs with Hangfire (needs to be refactored) var jobs = _entity.JobTimeline.Where(jt => jt.ScheduleId == schedule.Id).ToList(); foreach (var job in jobs) { var idHangfire = BackgroundJob.Schedule(() => _jobService.ExecuteJob(job.Id), job.StartTime); job.HangfireId = idHangfire; } _entity.SaveChanges(); } catch (Exception ex) { result.Status = Common.Enums.StatusEnum.Error; result.Message = Message.SomethingWentWrong; _logger.Information($"ScheduleService.EditSchedule(model: {model}, dates: {dates})"); _logger.Error(ex.Message); } return(result); }
public IResponse <List <GenericModel> > RemoveIntersection(List <GenericModel> transactions, JobTimeline jobTimeline) { var response = new Response <List <GenericModel> >(); try { if (jobTimeline.Schedule.RecurrenceRule != null) { var transactionsIDs = transactions.Select(t => t.TransactionId).ToList(); var intersectingTransactions = _entity.Transaction.Where(t => transactionsIDs.Contains(t.TransactionId) && t.JobTimeline.ScheduleId == jobTimeline.ScheduleId).ToList(); transactions.RemoveAll(t => intersectingTransactions.Select(s => s.TransactionId).Contains(t.TransactionId)); } response.Value = transactions; response.Status = Common.Enums.StatusEnum.Success; } catch (Exception ex) { response.Status = Common.Enums.StatusEnum.Error; _logger.Information($"TransactionService.RemoveIntersection(transaction: {transactions}, jobTimeline {jobTimeline.Id})"); _logger.Error(ex.Message); throw; } return(response); }