public override void RestoreHistory(JobRunner jobRunner)
        {
            RestoreCount++;

            jobRunner.LastResult = LastResult;
            jobRunner.LastRunStartTime = LastRunTime;
            jobRunner.LastStatus = LastStatus;
        }
        public override void SaveHistory(JobRunner jobRunner)
        {
            SaveCount++;

            LastResult = jobRunner.LastResult;
            LastRunTime = jobRunner.LastRunStartTime;
            LastStatus = jobRunner.LastStatus;
        }
Exemplo n.º 3
0
        public void StartMultiThreaded()
        {

            Type jobType = typeof(SleepJob);
            JobLockProvider jobLockProvider = new StaticLockProvider();
            var jobConfiguration = new JobConfiguration
            {
                Name = "SleepJob",
                Description = "Test Sleep Job with xml history",
                Interval = TimeSpan.FromSeconds(5),
                Type = typeof(SleepJob).AssemblyQualifiedName,
                JobLockProvider = typeof(StaticLockProvider).AssemblyQualifiedName,
            };

            jobConfiguration.Arguments.Add("sleep", 10);

            var log = new StringBuilder();
            var j = new JobRunner(jobConfiguration, jobType, jobLockProvider, null);
            JobManager.Current.JobStarting += (t, e) => log.AppendLine(e.ToString() + ":" + Thread.CurrentThread.ManagedThreadId);

            Thread t1 = new Thread(j.Start);
            Thread t2 = new Thread(j.Start);
            Thread t3 = new Thread(j.Start);
            Thread t4 = new Thread(j.Start);

            t1.Start();
            t2.Start();
            Thread.Sleep(TimeSpan.FromSeconds(5));
            t3.Start();
            t4.Start();

            // wait for job
            Thread.Sleep(TimeSpan.FromSeconds(45));

            string l = log.ToString();
            Assert.IsNotNull(l);

        }
Exemplo n.º 4
0
        public void StartWithTimeOfDayLaterThenStart()
        {

            Type jobType = typeof(SleepJob);
            JobLockProvider jobLockProvider = new StaticLockProvider();
            JobHistoryProvider jobHistoryProvider = new StaticHistoryProvider
                                                        {
                                                            LastResult = string.Empty,
                                                            LastRunTime = DateTime.Now,
                                                            LastStatus = JobStatus.None
                                                        };

            // run time 1 hour ago
            DateTime runTime = DateTime.Now.Subtract(TimeSpan.FromHours(1));

            var jobConfiguration = new JobConfiguration
                                       {
                                           Name = "SleepJob",
                                           Description = "Test Sleep Job with xml history",
                                           Interval = runTime.TimeOfDay,
                                           IsTimeOfDay = true,
                                           Type = typeof(SleepJob).AssemblyQualifiedName,
                                           JobLockProvider = typeof(StaticLockProvider).AssemblyQualifiedName,
                                           JobHistoryProvider = typeof(StaticHistoryProvider).AssemblyQualifiedName
                                       };

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

            Assert.IsNotNull(j);
            j.Start();

            DateTime nextRun = runTime.AddDays(1);

            Assert.AreEqual(nextRun.Date, j.NextRunTime.Date);
            Assert.AreEqual(nextRun.Hour, j.NextRunTime.Hour);
            Assert.AreEqual(nextRun.Minute, j.NextRunTime.Minute);
        }
Exemplo n.º 5
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);
            }

        }
Exemplo n.º 6
0
        public void StartWithTimeOfDayOlderThenOneDay()
        {

            Type jobType = typeof(SleepJob);
            StaticLockProvider jobLockProvider = new StaticLockProvider();
            StaticHistoryProvider jobHistoryProvider = new StaticHistoryProvider
            {
                LastResult = string.Empty,
                LastRunTime = DateTime.Now.Subtract(TimeSpan.FromDays(2)), // 2 days ago
                LastStatus = JobStatus.None
            };

            // run time 1 hour ago
            DateTime runTime = DateTime.Now.Subtract(TimeSpan.FromHours(1));

            var jobConfiguration = new JobConfiguration
            {
                Name = "SleepJob",
                Description = "Test Sleep Job with xml history",
                Interval = runTime.TimeOfDay,
                IsTimeOfDay = true,
                Type = typeof(SleepJob).AssemblyQualifiedName,
                JobLockProvider = typeof(StaticLockProvider).AssemblyQualifiedName,
                JobHistoryProvider = typeof(StaticHistoryProvider).AssemblyQualifiedName
            };

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

            Assert.IsNotNull(j);
            Assert.AreEqual(1, jobHistoryProvider.RestoreCount);
            Assert.AreEqual(JobStatus.None, j.LastStatus);

            j.Start();

            // wait 30 sec, then test
            Thread.Sleep(TimeSpan.FromSeconds(30));

            Assert.AreEqual(1, jobHistoryProvider.SaveCount);
            Assert.AreEqual(JobStatus.Completed, j.LastStatus);

            // should set next run time to tomorrow
            DateTime nextRun = runTime.AddDays(1);

            Assert.AreEqual(nextRun.Date, j.NextRunTime.Date);
            Assert.AreEqual(nextRun.Hour, j.NextRunTime.Hour);
            Assert.AreEqual(nextRun.Minute, j.NextRunTime.Minute);

        }
 /// <summary>
 /// Saves the history to the provider.
 /// </summary>
 /// <param name="jobRunner">The job to save the history on.</param>
 public abstract void SaveHistory(JobRunner jobRunner);
 /// <summary>
 /// Restores the latest job history from the provider.
 /// </summary>
 /// <param name="jobRunner">The job to restore the history to.</param>
 public abstract void RestoreHistory(JobRunner jobRunner);