private void AddJob(IScheduler scheduler, SystemJob jobItem, Func <Type, IJobActivity> getJob)
        {
            var jobKey = new JobKey(jobItem.SystemJobId, jobItem.JobClassType);
            var type   = Type.GetType(jobItem.JobClassType, true);

            // Define the Job to be scheduled
            var quartzJobDetail = JobBuilder.Create(typeof(QuartzJob))
                                  .WithIdentity(jobKey)
                                  .RequestRecovery()
                                  .Build();

            Func <IJobActivity> jobActivityConstructor = () => getJob(type);

            Func <DateTime, Action <string> > getAudit = startDateTime =>
                                                         message => _schedulerDbTools.CreateSystemJobLogEntry(
                jobItem.SystemJobId, startDateTime, DateTime.Now, message,
                Thread.CurrentThread.Name, null, jobItem.AllowMultipleInstances);

            var section = ConfigurationManager.GetSection("traceContextConfiguration") ?? new TraceContextConfigurationSection();
            ITraceContextConfigurator config = (TraceContextConfigurationSection)section;
            var contextName   = new ContextName(type.FullName, "");
            var configuration = config.GetDefault(contextName.Service, contextName.Method);
            var traceSource   = new TraceSource("VirtoCommerce.ScheduleService.Trace");
            var traceContext  = new TraceContext(configuration, contextName, Guid.NewGuid(), traceSource);

            quartzJobDetail.JobDataMap.Add("realization", jobActivityConstructor);
            quartzJobDetail.JobDataMap.Add("getAudit", getAudit);
            quartzJobDetail.JobDataMap.Add("context", traceContext);
            quartzJobDetail.JobDataMap.Add("parameters", jobItem.JobParameters.ToDictionary(pk => pk.Name.ToLowerInvariant(), pv => pv.Value));

            // Associate a trigger with the Job
            var trigger = TriggerBuilder.Create()
                          .WithIdentity(jobItem.SystemJobId, jobItem.JobClassType)
                          .WithSimpleSchedule(x =>
            {
                x.WithInterval(TimeSpan.FromSeconds(jobItem.Period));
                x.RepeatForever();
            })
                          .StartAt(DateTime.UtcNow)
                          .WithPriority(jobItem.Priority)
                          .Build();

            // Validate that the job doesn't already exists
            if (scheduler.CheckExists(jobKey))
            {
                scheduler.DeleteJob(jobKey);
            }
            scheduler.ScheduleJob(quartzJobDetail, trigger);
        }
        private void AddJob(IScheduler scheduler, SystemJob jobItem, Func<Type, IJobActivity> getJob)
        {
            var jobKey = new JobKey(jobItem.SystemJobId, jobItem.JobClassType);
            var type = Type.GetType(jobItem.JobClassType, true);

            // Define the Job to be scheduled
            var quartzJobDetail = JobBuilder.Create(typeof(QuartzJob))
                .WithIdentity(jobKey)
                .RequestRecovery()
                .Build();

            Func<IJobActivity> jobActivityConstructor = () => getJob(type);

            Func<DateTime, Action<string>> getAudit = startDateTime =>
                message => _schedulerDbTools.CreateSystemJobLogEntry(
                jobItem.SystemJobId, startDateTime, DateTime.Now, message,
                Thread.CurrentThread.Name, null, jobItem.AllowMultipleInstances);

            var section = ConfigurationManager.GetSection("traceContextConfiguration") ?? new TraceContextConfigurationSection();
            ITraceContextConfigurator config = (TraceContextConfigurationSection)section;
            var contextName = new ContextName(type.FullName, "");
            var configuration = config.GetDefault(contextName.Service, contextName.Method);
            var traceSource = new TraceSource("VirtoCommerce.ScheduleService.Trace");
            var traceContext = new TraceContext(configuration, contextName, Guid.NewGuid(), traceSource);

            quartzJobDetail.JobDataMap.Add("realization", jobActivityConstructor);
            quartzJobDetail.JobDataMap.Add("getAudit", getAudit);
            quartzJobDetail.JobDataMap.Add("context", traceContext);
            quartzJobDetail.JobDataMap.Add("parameters", jobItem.JobParameters.ToDictionary(pk => pk.Name.ToLowerInvariant(), pv => pv.Value));

            // Associate a trigger with the Job
            var trigger = TriggerBuilder.Create()
                .WithIdentity(jobItem.SystemJobId, jobItem.JobClassType)
                .WithSimpleSchedule(x =>
                {
                    x.WithInterval(TimeSpan.FromSeconds(jobItem.Period));
                    x.RepeatForever();
                })
                .StartAt(DateTime.UtcNow)
                .WithPriority(jobItem.Priority)
                .Build();

            // Validate that the job doesn't already exists
            if (scheduler.CheckExists(jobKey))
            {
                scheduler.DeleteJob(jobKey);
            }
            scheduler.ScheduleJob(quartzJobDetail, trigger);
        }