protected override void OnStart( string[] args ) { ISchedulerFactory sf; // create scheduler sf = new StdSchedulerFactory(); sched = sf.GetScheduler(); // get list of active jobs JobService jobService = new JobService(); foreach ( Job job in jobService.GetActiveJobs().ToList() ) { try { IJobDetail jobDetail = jobService.BuildQuartzJob( job ); ITrigger jobTrigger = jobService.BuildQuartzTrigger( job ); sched.ScheduleJob( jobDetail, jobTrigger ); } catch ( Exception ex ) { // get path to the services directory String path = System.Reflection.Assembly.GetExecutingAssembly().Location; path = System.IO.Path.GetDirectoryName(path); // create a friendly error message string message = string.Format("Error loading the job: {0}. Ensure that the correct version of the job's assembly ({1}.dll) in the services directory ({2}) of your server.", job.Name, job.Assemby, path); message = message + "\n\n\n\n" + ex.Message; //throw new JobLoadFailedException( message ); job.LastStatusMessage = message; job.LastStatus = "Error Loading Job"; jobService.Save( job, null ); } } // set up the listener to report back from jobs as they complete sched.ListenerManager.AddJobListener( new RockJobListener(), EverythingMatcher<JobKey>.AllJobs() ); // start the scheduler sched.Start(); }
/// <summary> /// Called by the <see cref="IScheduler"/> after a <see cref="IJobDetail"/> /// has been executed, and before the associated <see cref="Quartz.Spi.IOperableTrigger"/>'s /// <see cref="Quartz.Spi.IOperableTrigger.Triggered"/> method has been called. /// </summary> /// <param name="context"></param> /// <param name="jobException"></param> public void JobWasExecuted( IJobExecutionContext context, JobExecutionException jobException ) { StringBuilder message = new StringBuilder(); bool sendMessage = false; // get job type id int jobId = Convert.ToInt16(context.JobDetail.Description); // load job JobService jobService = new JobService(); Job job = jobService.Get(jobId); // format the message message.Append( String.Format( "The job {0} ran for {1} seconds on {2}. Below is the results:<p>" , job.Name, context.JobRunTime.TotalSeconds, context.FireTimeUtc.Value.DateTime.ToLocalTime()) ); // if noticiation staus is all set flag to send message if ( job.NotificationStatus == JobNotificationStatus.All ) sendMessage = true; // set last run date job.LastRunDate = context.FireTimeUtc.Value.DateTime.ToLocalTime(); // set run time job.LastRunDuration = Convert.ToInt32(context.JobRunTime.TotalSeconds); // set the scheduler name job.LastRunSchedulerName = context.Scheduler.SchedulerName; // determine if an error occured if ( jobException == null ) { job.LastSuccessfulRun = job.LastRunDate; job.LastStatus = "Success"; job.LastStatusMessage = ""; message.Append( "Result: Success" ); // determine if message should be sent if ( job.NotificationStatus == JobNotificationStatus.Success ) sendMessage = true; } else { // put the exception into the status job.LastStatus = "Exception"; job.LastStatusMessage = jobException.Message; message.Append( "Result: Exception<p>Message:<br>" + jobException.Message ); if ( jobException.InnerException != null ) { job.LastStatusMessage += " Inner Exception: " + jobException.InnerException.Message; message.Append( "<p>Inner Exception:<br>" + jobException.InnerException.Message ); } if ( job.NotificationStatus == JobNotificationStatus.Error ) sendMessage = true; } jobService.Save( job, null ); // send notification if ( sendMessage ) { // TODO: implement email send once it's available } }
protected void Application_Start( object sender, EventArgs e ) { // setup and launch the jobs infrastructure if running under IIS bool runJobsInContext = Convert.ToBoolean( ConfigurationManager.AppSettings["RunJobsInIISContext"] ); if ( runJobsInContext ) { ISchedulerFactory sf; // create scheduler sf = new StdSchedulerFactory(); sched = sf.GetScheduler(); // get list of active jobs JobService jobService = new JobService(); foreach ( Job job in jobService.GetActiveJobs().ToList() ) { try { IJobDetail jobDetail = jobService.BuildQuartzJob( job ); ITrigger jobTrigger = jobService.BuildQuartzTrigger( job ); sched.ScheduleJob( jobDetail, jobTrigger ); } catch ( Exception ex ) { // create a friendly error message string message = string.Format( "Error loading the job: {0}. Ensure that the correct version of the job's assembly ({1}.dll) in the websites App_Code directory. \n\n\n\n{2}", job.Name, job.Assemby, ex.Message ); job.LastStatusMessage = message; job.LastStatus = "Error Loading Job"; jobService.Save( job, null ); } } // set up the listener to report back from jobs as they complete sched.ListenerManager.AddJobListener( new RockJobListener(), EverythingMatcher<JobKey>.AllJobs() ); // start the scheduler sched.Start(); } // add call back to keep IIS process awake at night and to provide a timer for the queued transactions AddCallBack(); RegisterRoutes( RouteTable.Routes ); Rock.Security.Authorization.Load(); AddEventHandlers(); }