Exemple #1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="JobManager"/> class.
 /// </summary>
 private JobManager()
 {
     _id                     = Guid.NewGuid().ToString("N").Substring(0, 10).ToLower();
     _providerJobs           = new Dictionary <JobProvider, JobCollection>();
     _jobLockProviders       = new JobLockProviderCollection();
     _defaultJobLockProvider = new DefaultJobLockProvider();
     _jobProviders           = new JobProviderCollection();
     _jobs                   = new JobCollection();
     _jobProviderTimer       = new Timer(OnJobProviderCallback);
 }
Exemple #2
0
        private void OnJobProviderCallback(object state)
        {
            bool wasReloaded = false;

            // make thread safe by making sure this can't run when Initialize is running
            lock (_initLock)
            {
                foreach (JobProvider provider in _jobProviders)
                {
                    if (!provider.IsReloadRequired(_lastInitilize))
                    {
                        continue;
                    }

                    Trace.TraceInformation("Reload jobs for provider {0}.", provider.ToString());

                    //reload this provider
                    JobCollection providerJobs;
                    if (!_providerJobs.TryGetValue(provider, out providerJobs))
                    {
                        providerJobs = new JobCollection();
                        _providerJobs.Add(provider, providerJobs);
                    }

                    //remove jobs
                    foreach (JobRunner job in providerJobs)
                    {
                        job.Stop(true);
                        _jobs.Remove(job);
                    }
                    providerJobs.Clear();

                    //add jobs back
                    AddJobs(provider.GetJobs(), provider);
                    wasReloaded = true;

                    foreach (JobRunner job in providerJobs)
                    {
                        job.Start();
                    }
                }
            }

            if (wasReloaded)
            {
                _lastInitilize = DateTime.Now;
            }
        }
Exemple #3
0
        public JobCollection GetJobsByGroup(string group)
        {
            var jobs = new JobCollection();

            lock (_initLock)
            {
                foreach (var job in _jobs)
                {
                    if (job.Group == group)
                    {
                        jobs.Add(job);
                    }
                }
            }

            return(jobs);
        }
Exemple #4
0
        private void AddJobs(IEnumerable <IJobConfiguration> jobs, JobProvider provider)
        {
            if (jobs == null)
            {
                return;
            }

            foreach (var jobConfiguration in jobs)
            {
                Type jobType = Type.GetType(jobConfiguration.Type, false, true);
                if (jobType == null)
                {
                    throw new ConfigurationErrorsException(
                              string.Format("Could not load type '{0}' for job '{1}'.",
                                            jobConfiguration.Type, jobConfiguration.Name));
                }

                JobLockProvider jobLockProvider = _defaultJobLockProvider;

                if (!string.IsNullOrEmpty(jobConfiguration.JobLockProvider))
                {
                    // first try getting from provider collection
                    jobLockProvider = _jobLockProviders[jobConfiguration.JobLockProvider];
                    if (jobLockProvider == null)
                    {
                        // next, try loading type
                        Type lockType = Type.GetType(jobConfiguration.JobLockProvider, false, true);
                        if (lockType == null)
                        {
                            throw new ConfigurationErrorsException(
                                      string.Format("Could not load job lock type '{0}' for job '{1}'.",
                                                    jobConfiguration.JobLockProvider, jobConfiguration.Name));
                        }

                        jobLockProvider = Activator.CreateInstance(lockType) as JobLockProvider;
                    }

                    // if not found in provider collection or couldn't create type.
                    if (jobLockProvider == null)
                    {
                        throw new ConfigurationErrorsException(
                                  string.Format("Could not find job lock provider '{0}' for job '{1}'.", jobConfiguration.JobLockProvider, jobConfiguration.Name));
                    }
                }

                JobHistoryProvider jobHistoryProvider = null;
                if (!string.IsNullOrEmpty(jobConfiguration.JobHistoryProvider))
                {
                    Type historyType = Type.GetType(jobConfiguration.JobHistoryProvider, false, true);
                    if (historyType == null)
                    {
                        throw new ConfigurationErrorsException(
                                  string.Format("Could not load job history type '{0}' for job '{1}'.", jobConfiguration.JobHistoryProvider, jobConfiguration.Name));
                    }

                    jobHistoryProvider = Activator.CreateInstance(historyType) as JobHistoryProvider;
                }

                var j = new JobRunner(jobConfiguration, jobType, jobLockProvider, jobHistoryProvider);
                _jobs.Add(j);

                // keep track of jobs for providers so they can be sync'd later
                if (provider == null)
                {
                    continue;
                }

                JobCollection providerJobs;
                if (!_providerJobs.TryGetValue(provider, out providerJobs))
                {
                    providerJobs = new JobCollection();
                    _providerJobs.Add(provider, providerJobs);
                }
                providerJobs.Add(j);
            }
        }