Rock.Model.ServiceJobService.GetActiveJobs C# (CSharp) Method

GetActiveJobs() public method

Returns a queryable collection of active Jobs
public GetActiveJobs ( ) : IQueryable
return IQueryable
        public IQueryable<ServiceJob> GetActiveJobs()
        {
            return Queryable().Where( t => t.IsActive == true );
        }

Usage Example

Ejemplo n.º 1
0
        /// <summary>
        /// Updates the scheduled jobs.
        /// </summary>
        /// <param name="context">The context.</param>
        private void UpdateScheduledJobs( IJobExecutionContext context )
        {
            var scheduler = context.Scheduler;

            var rockContext = new Rock.Data.RockContext();
            ServiceJobService jobService = new ServiceJobService( rockContext );
            List<ServiceJob> activeJobList = jobService.GetActiveJobs().ToList();
            List<Quartz.JobKey> scheduledQuartzJobs = scheduler.GetJobKeys( GroupMatcher<JobKey>.GroupStartsWith( string.Empty ) ).ToList();

            // delete any jobs that are no longer exist (are are not set to active) in the database
            var quartsJobsToDelete = scheduledQuartzJobs.Where( a => !activeJobList.Any( j => j.Guid.Equals( a.Name.AsGuid() ) ) );
            foreach ( JobKey jobKey in quartsJobsToDelete )
            {
                scheduler.DeleteJob( jobKey );
            }

            // add any jobs that are not yet scheduled
            var newActiveJobs = activeJobList.Where( a => !scheduledQuartzJobs.Any( q => q.Name.AsGuid().Equals( a.Guid ) ) );
            foreach ( Rock.Model.ServiceJob job in newActiveJobs )
            {
                try
                {
                    IJobDetail jobDetail = jobService.BuildQuartzJob( job );
                    ITrigger jobTrigger = jobService.BuildQuartzTrigger( job );

                    scheduler.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.Assembly, ex.Message );
                    job.LastStatusMessage = message;
                    job.LastStatus = "Error Loading Job";
                }
            }

            rockContext.SaveChanges();

            // reload the jobs in case any where added/removed
            scheduledQuartzJobs = scheduler.GetJobKeys( GroupMatcher<JobKey>.GroupStartsWith( string.Empty ) ).ToList();

            // update schedule if the schedule has changed
            foreach ( var jobKey in scheduledQuartzJobs )
            {
                var jobCronTrigger = scheduler.GetTriggersOfJob( jobKey ).OfType<ICronTrigger>().FirstOrDefault();
                if ( jobCronTrigger != null )
                {
                    var activeJob = activeJobList.FirstOrDefault( a => a.Guid.Equals( jobKey.Name.AsGuid() ) );
                    if ( activeJob != null )
                    {
                        if ( activeJob.CronExpression != jobCronTrigger.CronExpressionString )
                        {
                            ITrigger newJobTrigger = jobService.BuildQuartzTrigger( activeJob );
                            scheduler.RescheduleJob( jobCronTrigger.Key, newJobTrigger );
                        }
                    }
                }
            }
        }
All Usage Examples Of Rock.Model.ServiceJobService::GetActiveJobs