public ServiceJob GetAddTestJob(JobNotificationStatus jobNotificationStatus = JobNotificationStatus.None) { var testJob = new ServiceJob { IsSystem = true, IsActive = true, Name = "Test Job", Description = "This job is used for testing RockJobListener", Class = "Rock.Tests.Integration.Jobs.RockJobListenerTestJob", CronExpression = "0 0 1 * * ?", NotificationStatus = jobNotificationStatus, Guid = "84AE12A7-968B-4D28-AB39-81D36D1F230E".AsGuid(), NotificationEmails = "*****@*****.**" }; using (var rockContext = new RockContext()) { var serviceJobService = new ServiceJobService(rockContext); var job = serviceJobService.Get(testJob.Guid); if (job != null) { testJobId = job.Id; return(job); } serviceJobService.Add(testJob); rockContext.SaveChanges(); } testJobId = testJob.Id; return(testJob); }
/// <summary> /// Copies the model property values to the DTO properties /// </summary> /// <param name="model">The model.</param> public override void CopyFromModel(IEntity model) { base.CopyFromModel(model); if (model is ServiceJob) { var serviceJob = (ServiceJob)model; this.IsSystem = serviceJob.IsSystem; this.IsActive = serviceJob.IsActive; this.Name = serviceJob.Name; this.Description = serviceJob.Description; this.Assembly = serviceJob.Assembly; this.Class = serviceJob.Class; this.CronExpression = serviceJob.CronExpression; this.LastSuccessfulRunDateTime = serviceJob.LastSuccessfulRunDateTime; this.LastRunDateTime = serviceJob.LastRunDateTime; this.LastRunDurationSeconds = serviceJob.LastRunDurationSeconds; this.LastStatus = serviceJob.LastStatus; this.LastStatusMessage = serviceJob.LastStatusMessage; this.LastRunSchedulerName = serviceJob.LastRunSchedulerName; this.NotificationEmails = serviceJob.NotificationEmails; this.NotificationStatus = serviceJob.NotificationStatus; } }
public void RunJob(Dictionary <string, string> jobDataMapDictionary, JobNotificationStatus jobNotificationStatus = JobNotificationStatus.None) { var job = GetAddTestJob(jobNotificationStatus); using (var rockContext = new RockContext()) { var jobService = new ServiceJobService(rockContext); if (job != null) { // create a scheduler specific for the job var scheduleConfig = new System.Collections.Specialized.NameValueCollection(); var runNowSchedulerName = ("RunNow:" + job.Guid.ToString("N")).Truncate(40); scheduleConfig.Add(StdSchedulerFactory.PropertySchedulerInstanceName, runNowSchedulerName); var schedulerFactory = new StdSchedulerFactory(scheduleConfig); var sched = new StdSchedulerFactory(scheduleConfig).GetScheduler(); if (sched.IsStarted) { // the job is currently running as a RunNow job return; } // Check if another scheduler is running this job try { var otherSchedulers = new Quartz.Impl.StdSchedulerFactory().AllSchedulers .Where(s => s.SchedulerName != runNowSchedulerName); foreach (var scheduler in otherSchedulers) { if (scheduler.GetCurrentlyExecutingJobs().Where(j => j.JobDetail.Description == job.Id.ToString() && j.JobDetail.ConcurrentExectionDisallowed).Any()) { // A job with that Id is already running and ConcurrentExectionDisallowed is true System.Diagnostics.Debug.WriteLine(RockDateTime.Now.ToString() + $" Scheduler '{scheduler.SchedulerName}' is already executing job Id '{job.Id}' (name: {job.Name})"); return; } } } catch { } // create the quartz job and trigger IJobDetail jobDetail = jobService.BuildQuartzJob(job); if (jobDataMapDictionary != null) { // Force the <string, string> dictionary so that within Jobs, it is always okay to use // JobDataMap.GetString(). This mimics Rock attributes always being stored as strings. // If we allow non-strings, like integers, then JobDataMap.GetString() throws an exception. jobDetail.JobDataMap.PutAll(jobDataMapDictionary.ToDictionary(kvp => kvp.Key, kvp => ( object )kvp.Value)); } var jobTrigger = TriggerBuilder.Create() .WithIdentity(job.Guid.ToString(), job.Name) .StartNow() .Build(); // schedule the job sched.ScheduleJob(jobDetail, jobTrigger); // set up the listener to report back from the job when it completes sched.ListenerManager.AddJobListener(new RockJobListener(), EverythingMatcher <JobKey> .AllJobs()); // start the scheduler sched.Start(); // Wait 10secs to give job chance to start System.Threading.Tasks.Task.Delay(new TimeSpan(0, 0, 10)).Wait(); // stop the scheduler when done with job sched.Shutdown(true); } } }