public virtual IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        {
            if (bundle == null)
            {
                throw new ArgumentNullException(nameof(bundle));
            }
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }

            var jobType = bundle.JobDetail.JobType;

            var nestedScope = _lifetimeScope.BeginLifetimeScope(_scopeTag);

            IJob newJob;

            try
            {
                newJob = (IJob)nestedScope.Resolve(jobType);
                var jobTrackingInfo = new JobTrackingInfo(nestedScope);
                RunningJobs[newJob] = jobTrackingInfo;
                nestedScope         = null;
            }
            catch (Exception ex)
            {
                nestedScope?.Dispose();
                throw new SchedulerConfigException(string.Format(CultureInfo.InvariantCulture,
                                                                 "Failed to instantiate Job '{0}' of type '{1}'",
                                                                 bundle.JobDetail.Key, bundle.JobDetail.JobType), ex);
            }
            return(newJob);
        }
Exemplo n.º 2
0
        /// <summary>
        ///     Called by the scheduler at the time of the trigger firing, in order to
        ///     produce a <see cref="T:Quartz.IJob" /> instance on which to call Execute.
        /// </summary>
        /// <remarks>
        ///     It should be extremely rare for this method to throw an exception -
        ///     basically only the the case where there is no way at all to instantiate
        ///     and prepare the Job for execution.  When the exception is thrown, the
        ///     Scheduler will move all triggers associated with the Job into the
        ///     <see cref="F:Quartz.TriggerState.Error" /> state, which will require human
        ///     intervention (e.g. an application restart after fixing whatever
        ///     configuration problem led to the issue wih instantiating the Job.
        /// </remarks>
        /// <param name="bundle">
        ///     The TriggerFiredBundle from which the <see cref="T:Quartz.IJobDetail" />
        ///     and other info relating to the trigger firing can be obtained.
        /// </param>
        /// <param name="scheduler">a handle to the scheduler that is about to execute the job</param>
        /// <throws>SchedulerException if there is a problem instantiating the Job. </throws>
        /// <returns>
        ///     the newly instantiated Job
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="bundle" /> is <see langword="null" />.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="scheduler" /> is <see langword="null" />.</exception>
        /// <exception cref="SchedulerConfigException">
        ///     Error resolving exception. Original exception will be stored in
        ///     <see cref="Exception.InnerException" />.
        /// </exception>
        public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        {
            if (bundle == null)
            {
                throw new ArgumentNullException(nameof(bundle));
            }
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }

            var jobType = bundle.JobDetail.JobType;

            //var nestedScope = _lifetimeScope.BeginLifetimeScope(_scopeName);
            var nestedScope = _lifetimeScope.BeginLifetimeScope(MatchingScopeLifetimeTags.RequestLifetimeScopeTag);

            //var nestedScope =
            (ObjectContainer.Current as AutofacObjectContainer)?.Container.BeginLifetimeScope(
                MatchingScopeLifetimeTags.RequestLifetimeScopeTag);

            IJob newJob = null;

            try
            {
                newJob = (IJob)nestedScope.Resolve(jobType);

                JobDataMap jobDataMap = new JobDataMap();
                jobDataMap.PutAll(scheduler.Context);
                jobDataMap.PutAll(bundle.JobDetail.JobDataMap);
                jobDataMap.PutAll(bundle.Trigger.JobDataMap);

                SetObjectProperties(newJob, jobDataMap);

                var jobTrackingInfo = new JobTrackingInfo(nestedScope);
                RunningJobs[newJob] = jobTrackingInfo;

                if (_logger.IsDebugEnabled)
                {
                    _logger.Info($"Scope 0x{jobTrackingInfo.Scope.GetHashCode():x} associated with Job 0x{newJob.GetHashCode():x}");
                }

                nestedScope = null;
            }
            catch (Exception ex)
            {
                if (nestedScope != null)
                {
                    DisposeScope(newJob, nestedScope);
                }
                throw new SchedulerConfigException(string.Format(CultureInfo.InvariantCulture,
                                                                 "Failed to instantiate Job '{0}' of type '{1}'",
                                                                 bundle.JobDetail.Key, bundle.JobDetail.JobType), ex);
            }
            return(newJob);
        }
Exemplo n.º 3
0
        /// <summary>
        ///     Called by the scheduler at the time of the trigger firing, in order to
        ///     produce a <see cref="T:Quartz.IJob" /> instance on which to call Execute.
        /// </summary>
        /// <remarks>
        ///     It should be extremely rare for this method to throw an exception -
        ///     basically only the the case where there is no way at all to instantiate
        ///     and prepare the Job for execution.  When the exception is thrown, the
        ///     Scheduler will move all triggers associated with the Job into the
        ///     <see cref="F:Quartz.TriggerState.Error" /> state, which will require human
        ///     intervention (e.g. an application restart after fixing whatever
        ///     configuration problem led to the issue wih instantiating the Job.
        /// </remarks>
        /// <param name="bundle">
        ///     The TriggerFiredBundle from which the <see cref="T:Quartz.IJobDetail" />
        ///     and other info relating to the trigger firing can be obtained.
        /// </param>
        /// <param name="scheduler">a handle to the scheduler that is about to execute the job</param>
        /// <throws>SchedulerException if there is a problem instantiating the Job. </throws>
        /// <returns>
        ///     the newly instantiated Job
        /// </returns>
        /// <exception cref="ArgumentNullException"><paramref name="bundle" /> is <see langword="null" />.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="scheduler" /> is <see langword="null" />.</exception>
        /// <exception cref="SchedulerConfigException">
        ///     Error resolving exception. Original exception will be stored in
        ///     <see cref="Exception.InnerException" />.
        /// </exception>
        public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        {
            if (bundle == null)
            {
                throw new ArgumentNullException("bundle");
            }
            if (scheduler == null)
            {
                throw new ArgumentNullException("scheduler");
            }

            var jobType     = bundle.JobDetail.JobType;
            var nestedScope = _lifetimeScope.BeginLifetimeScope(_scopeName);

            var jobName = bundle.JobDetail.Key.Name;

            IJob newJob = null;

            try
            {
                newJob = //(IJob) nestedScope.Resolve(jobType);
                         (IJob)nestedScope.ResolveKeyed(jobName, jobType);

                var jobTrackingInfo = new JobTrackingInfo(nestedScope);
                RunningJobs[newJob] = jobTrackingInfo;

                if (SLog.IsTraceEnabled)
                {
                    SLog.TraceFormat(CultureInfo.InvariantCulture, "Scope 0x{0:x} associated with Job 0x{1:x}",
                                     jobTrackingInfo.Scope.GetHashCode(), newJob.GetHashCode());
                }

                nestedScope = null;
            }
            catch (Exception ex)
            {
                if (nestedScope != null)
                {
                    DisposeScope(newJob, nestedScope);
                }
                throw new SchedulerConfigException(string.Format(CultureInfo.InvariantCulture,
                                                                 "Failed to instantiate Job '{0}' of type '{1}'",
                                                                 bundle.JobDetail.Key, bundle.JobDetail.JobType), ex);
            }
            return(newJob);
        }
Exemplo n.º 4
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="bundle"></param>
        /// <param name="scheduler"></param>
        /// <returns></returns>
        public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        {
            if (bundle == null)
            {
                throw new ArgumentNullException(nameof(bundle));
            }
            if (scheduler == null)
            {
                throw new ArgumentNullException(nameof(scheduler));
            }

            var jobType = bundle.JobDetail.JobType;

            var nestedScope = _container.CreateScope();

            IJob newJob;

            try
            {
                newJob = (IJob)nestedScope.ServiceProvider.GetService(jobType);
                var jobTrackingInfo = new JobTrackingInfo(nestedScope);
                RunningJobs[newJob] = jobTrackingInfo;
                nestedScope         = null;
            }
            catch (Exception ex)
            {
                if (nestedScope != null)
                {
                    nestedScope?.Dispose();
                }

                throw new SchedulerConfigException(string.Format(CultureInfo.InvariantCulture,
                                                                 "Failed to instantiate Job '{0}' of type '{1}'",
                                                                 bundle.JobDetail.Key, bundle.JobDetail.JobType), ex);
            }
            return(newJob);
        }
        /// <summary>
        ///     Called by the scheduler at the time of the trigger firing, in order to
        ///     produce a <see cref="T:Quartz.IJob" /> instance on which to call Execute.
        /// </summary>
        /// <remarks>
        ///     It should be extremely rare for this method to throw an exception -
        ///     basically only the the case where there is no way at all to instantiate
        ///     and prepare the Job for execution.  When the exception is thrown, the
        ///     Scheduler will move all triggers associated with the Job into the
        ///     <see cref="F:Quartz.TriggerState.Error" /> state, which will require human
        ///     intervention (e.g. an application restart after fixing whatever
        ///     configuration problem led to the issue wih instantiating the Job.
        /// </remarks>
        /// <param name="bundle">
        ///     The TriggerFiredBundle from which the <see cref="T:Quartz.IJobDetail" />
        ///     and other info relating to the trigger firing can be obtained.
        /// </param>
        /// <param name="scheduler">a handle to the scheduler that is about to execute the job</param>
        /// <throws>SchedulerException if there is a problem instantiating the Job. </throws>
        /// <returns>
        ///     the newly instantiated Job
        /// </returns>
        public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
        {
            if (bundle == null) throw new ArgumentNullException("bundle");
            if (scheduler == null) throw new ArgumentNullException("scheduler");

            Type jobType = bundle.JobDetail.JobType;

            string jobName = bundle.JobDetail.Key.Name;
            ILifetimeScope nestedScope = _lifetimeScope.BeginLifetimeScope(_scopeName);

            IJob newJob = null;
            try
            {
                newJob = (IJob) nestedScope.ResolveNamed(jobName, jobType);
                var jobTrackingInfo = new JobTrackingInfo(nestedScope);
                RunningJobs[newJob] = jobTrackingInfo;

                _logDebug(newJob,
                    string.Format(CultureInfo.InvariantCulture, "Scope 0x{0:x} associated with Job 0x{1:x}",
                        jobTrackingInfo.Scope.GetHashCode(), newJob.GetHashCode()));
                
                nestedScope = null;
            }
            catch (Exception ex)
            {
                if (nestedScope != null)
                {
                    DisposeScope(newJob, nestedScope);
                }
                throw new SchedulerConfigException(string.Format(CultureInfo.InvariantCulture,
                    "Failed to instantiate Job '{0}' of type '{1}'",
                    bundle.JobDetail.Key, bundle.JobDetail.JobType), ex);
            }
            return newJob;
        }