/// <summary> /// Updates SQLDB from incoming folder JSON files /// </summary> /// <returns>Dictionary with Template keys and values indicating the update types</returns> public Dictionary <JobTemplate, UpdateType> updateFromIncoming() { Dictionary <JobTemplate, UpdateType> jobIDs = null; QuartzService.log("Checking for any incoming updates"); try { List <String> filePaths = new List <String>(Directory.GetFiles(strIncomingDirectory)); if (filePaths.Count > 0) { QuartzService.log("Running incoming updates"); jobIDs = new Dictionary <JobTemplate, UpdateType>(); sortIncomingFiles(filePaths); for (int i = 0; i < filePaths.Count; i++) { if (filePaths[i].EndsWith(".json", StringComparison.CurrentCultureIgnoreCase)) { try { JobTemplate template = JsonConvert.DeserializeObject <JobTemplate>(File.ReadAllText(filePaths[i])); try { UpdateType updateType = updateDatabase(template); QuartzService.log("Job ID " + template.ID + " set to update in DB"); db.SaveChanges(); QuartzService.log("Job ID " + template.ID + " changes saved to the DB"); jobIDs.Add(template, updateType); try { File.Delete(filePaths[i]); } catch (Exception ex) { QuartzService.log("Error deleting JSON file after update Exception\n" + ex.StackTrace); } } catch (Exception ex) { QuartzService.log("Error updating the DB Exception\n" + ex.InnerException + "\n" + ex.StackTrace); } } catch (Exception ex) { renameToERR(filePaths[i]); QuartzService.log("Error updating from " + filePaths[i] + "\n" + ex.StackTrace); } } } } else { QuartzService.log("No Updates Found"); } } catch (Exception ex) { QuartzService.log("Error loading files from " + strIncomingDirectory + "Exception\n" + ex.StackTrace); } return(jobIDs); }
/// <summary> /// Loads all jobs from SQLDB into a template List /// </summary> private void loadJobsFromDB() { templates = new List <JobTemplate>(); try { DBJob dbJob = new DBJob(); var dbJobQuery = from job in db.Jobs select job; List <DBJob> dbJobs = dbJobQuery.ToList(); if (dbJobs != null) { foreach (DBJob job in dbJobs) { JobTemplate template = new JobTemplate(); template.ID = job.JobId; template.Name = job.JobName; template.Group = job.JobGroup; template.processPathStringsFromDB(job.Process, job.WorkingDirectory); template.Arguments = job.Arguments; template.WorkingDirectory = job.WorkingDirectory; template.CronSchedule = job.CronSchedule; template.Timeout = job.TimeOut.ToString(); templates.Add(template); } } } catch (Exception ex) { QuartzService.log("Error loading Jobs from DB: " + "Exception\n" + ex.StackTrace); } }
/// <summary> /// Schedules a single job in the scheduler /// </summary> /// <param name="Template"></param> public void setUpJob(JobTemplate Template) { try { IJobDetail job = JobBuilder.Create <Job>().Build(); job.JobDataMap["ID"] = Template.ID.ToString(); job.JobDataMap["Name"] = Template.Name; //job.JobDataMap["Group"] = template.Group; //Defaulting all to "Default" group for now job.JobDataMap["Group"] = "Default"; job.JobDataMap["Process"] = Template.Process; job.JobDataMap["Arguments"] = Template.Arguments; job.JobDataMap["CronSchedule"] = Template.CronSchedule; job.JobDataMap["TimeOut"] = Template.Timeout; ITrigger trigger = TriggerBuilder.Create() .WithIdentity(job.JobDataMap["ID"].ToString(), job.JobDataMap["Group"].ToString()) .WithCronSchedule(job.JobDataMap["CronSchedule"].ToString()) .StartAt(DateTime.UtcNow) .WithPriority(1) .Build(); scheduler.ScheduleJob(job, trigger); QuartzService.log("Job ID " + Template.ID + " has been scheduled"); } catch (Exception ex) { QuartzService.log("Error scheduling " + Template.Name + "\n" + ex.StackTrace); } }
public Boolean UpdateJobs(List <int> jobs) { bool Successful = false; try { String[] jobIDs = QuartzService.scheduler.getAllJobIDS(); foreach (var job in jobs) { if (jobIDs.Contains(job.ToString())) { QuartzService.scheduler.removeJobFromScheduler(job.ToString()); JobTemplate temp = QuartzService.scheduler.loadJobFromDB(job.ToString()); if (temp.ID > 0) { QuartzService.scheduler.setUpJob(QuartzService.scheduler.loadJobFromDB(job.ToString())); } } else { QuartzService.scheduler.setUpJob(QuartzService.scheduler.loadJobFromDB(job.ToString())); } } } catch (Exception ex) { QuartzService.log("Error Updating jobs thru IJobupdater " + "\n" + ex.StackTrace); } return(Successful); }
/// <summary> /// Updates SQLDB from single template /// </summary> /// <param name="template"></param> private UpdateType updateDatabase(JobTemplate template) { UpdateType updateType = UpdateType.Unknown; //check to see if all null except ID(this means to delete JOB from DB) if (template.Name == null && template.Group == null && template.Process == null & template.WorkingDirectory == null && template.Arguments == null && template.CronSchedule == null && template.Timeout == null) { DBJob toDelete = db.Jobs.Where(s => s.JobId == template.ID).FirstOrDefault <DBJob>(); if (toDelete != null) { db.Entry(toDelete).State = System.Data.Entity.EntityState.Deleted; updateType = UpdateType.Remove; } } //job needs to be added or updated else { var jobQuery = from job in db.Jobs where job.JobId == template.ID select job; // job needs updated if (jobQuery.Count() == 1) { DBJob dbJob = jobQuery.SingleOrDefault(); dbJob.JobName = template.Name; dbJob.JobGroup = template.Group; dbJob.Process = template.Process; dbJob.WorkingDirectory = template.WorkingDirectory; dbJob.Arguments = template.Arguments; dbJob.CronSchedule = template.CronSchedule; dbJob.TimeOut = Int32.Parse(template.Timeout); updateType = UpdateType.Change; } //job needs added if (jobQuery.Count() == 0) { DBJob dbJob = new DBJob(); dbJob.JobId = template.ID; dbJob.JobName = template.Name; dbJob.JobGroup = template.Group; dbJob.Process = template.Process; dbJob.WorkingDirectory = template.WorkingDirectory; dbJob.Arguments = template.Arguments; dbJob.CronSchedule = template.CronSchedule; dbJob.TimeOut = Int32.Parse(template.Timeout); db.Jobs.Add(dbJob); updateType = UpdateType.Add; } } return(updateType); }
/// <summary> /// Loads and schedules jobs from JSON files in Jobs directory /// </summary> private void loadAndScheduleJobsFromFiles() { List <JobTemplate> fileTemplates = new List <JobTemplate>(); String[] filepaths = Directory.GetFiles(strJobsDirectory); foreach (String file in filepaths) { try { JobTemplate template = JsonConvert.DeserializeObject <JobTemplate>(File.ReadAllText(file)); fileTemplates.Add(template); QuartzService.log(file + " file loaded successfully..."); } catch (Exception ex) { QuartzService.log("Error loading " + file + "\n" + ex.StackTrace); } } foreach (JobTemplate template in fileTemplates) { try { IJobDetail job = JobBuilder.Create <Job>().Build(); job.JobDataMap["ID"] = template.ID.ToString(); job.JobDataMap["Name"] = template.Name; //job.JobDataMap["Group"] = template.Group; //Defaulting all to "Default" group for now job.JobDataMap["Group"] = "Default"; job.JobDataMap["Process"] = template.Process; job.JobDataMap["Arguments"] = template.Arguments; job.JobDataMap["CronSchedule"] = template.CronSchedule; job.JobDataMap["TimeOut"] = template.Timeout; ITrigger trigger = TriggerBuilder.Create() .WithIdentity(job.JobDataMap["ID"].ToString(), job.JobDataMap["Group"].ToString()) .WithCronSchedule(job.JobDataMap["CronSchedule"].ToString()) .StartAt(DateTime.UtcNow) .WithPriority(1) .Build(); scheduler.ScheduleJob(job, trigger); QuartzService.log("Job ID " + template.ID + " has been scheduled"); } catch (Exception ex) { QuartzService.log("Error scheduling " + template.Name + "\n" + ex.StackTrace); } } }
/// <summary> /// Load a single job from SQLDB by JobID /// </summary> /// <param name="JobID"></param> /// <returns>Template</returns> public JobTemplate loadJobFromDB(String JobID) { JobTemplate template = new JobTemplate(); try { int iJob = Int32.Parse(JobID); DBJob dbJob = db.Jobs.FirstOrDefault(j => j.JobId == iJob); template.ID = dbJob.JobId; template.Name = dbJob.JobName; template.Group = dbJob.JobGroup; template.processPathStringsFromDB(dbJob.Process, dbJob.WorkingDirectory); template.Arguments = dbJob.Arguments; template.WorkingDirectory = dbJob.WorkingDirectory; template.CronSchedule = dbJob.CronSchedule; template.Timeout = dbJob.TimeOut.ToString(); } catch (Exception ex) { QuartzService.log("Error loading Job from DB: " + "Exception\n" + ex.StackTrace); } return(template); }