private void InitDbRecord() { var list = JobManager.AllSchedules.ToList().Select(a => new { a.Name, a.NextRun }).Distinct().ToList(); using (var uof = new UnitOfWork()) { foreach (var o in list) { var item = uof.SchedulerJobRepository.Find(a => a.Nome == o.Name).FirstOrDefault(); if (item == null) { item = new Library.Entity.Altro.SchedulerJob(); item.Nome = o.Name; } item.ProssimoAvvio = list.Where(a => a.Name == o.Name).Min(a => a.NextRun); if (item.ID == 0) { item.Errore = ""; item.Enabled = false; uof.SchedulerJobRepository.Add(item); } else { uof.SchedulerJobRepository.Update(item); } uof.Commit(); } } ReadUpdateDb(); }
private static void AttachEvents() { JobManager.JobStart += (o) => { ManagerLog.Logger.Info("Avviato job " + o.Name); using (var uof = new UnitOfWork()) { var item = uof.SchedulerJobRepository.Find(a => a.Nome == o.Name).FirstOrDefault(); if (item == null) { item = new Library.Entity.Altro.SchedulerJob(); item.Nome = o.Name; } item.UltimaEsecuzione = o.StartTime; item.Errore = ""; if (item.ID == 0) { uof.SchedulerJobRepository.Add(item); } else { uof.SchedulerJobRepository.Update(item); } uof.Commit(); } }; JobManager.JobException += (o) => { ManagerLog.Logger.Error(o.Exception, "Job " + o.Name); using (var uof = new UnitOfWork()) { var item = uof.SchedulerJobRepository.Find(a => a.Nome == o.Name).FirstOrDefault(); if (item == null) { item = new Library.Entity.Altro.SchedulerJob(); item.Nome = o.Name; } item.Errore = o.Exception.Message; if (item.ID == 0) { uof.SchedulerJobRepository.Add(item); } else { uof.SchedulerJobRepository.Update(item); } uof.Commit(); } }; JobManager.JobEnd += (o) => { ManagerLog.Logger.Info("Job " + o.Name + " terminato"); using (var uof = new UnitOfWork()) { var item = uof.SchedulerJobRepository.Find(a => a.Nome == o.Name).FirstOrDefault(); if (item == null) { item = new Library.Entity.Altro.SchedulerJob(); item.Nome = o.Name; } item.ProssimoAvvio = o.NextRun.Value; item.UltimaEsecuzione = o.StartTime; item.Duration = o.Duration; if (item.ID == 0) { uof.SchedulerJobRepository.Add(item); } else { uof.SchedulerJobRepository.Update(item); } uof.Commit(); } }; }