public void CreateJob_HandlesDuplicatesAccordingToConflictAction(CreateJobConflictAction conflictAction,
                                                                         bool expectedResult)
        {
            Assert.IsTrue(jobStore.CreateJob(dummyJobSpec, new DateTime(2007, 5, 31), CreateJobConflictAction.Ignore));

            Assert.AreEqual(expectedResult, jobStore.CreateJob(dummyJobSpec, new DateTime(2007, 6, 1), conflictAction));

            if (expectedResult)
            {
                JobDetails jobDetails = jobStore.GetJobDetails(dummyJobSpec.Name);

                // In Update mode, the creation time should remain the same as the original job.
                // We should also preserve its history (though we don't check this right now.  -- TODO)
                if (conflictAction == CreateJobConflictAction.Update)
                {
                    DateTimeAssert.AreEqualIncludingKind(new DateTime(2007, 5, 31, 0, 0, 0, DateTimeKind.Utc),
                                                         jobDetails.CreationTimeUtc);
                }

                // In Replace mode, the creation time should be that of the newly created job.
                if (conflictAction == CreateJobConflictAction.Replace)
                {
                    DateTimeAssert.AreEqualIncludingKind(new DateTime(2007, 6, 1, 0, 0, 0, DateTimeKind.Utc),
                                                         jobDetails.CreationTimeUtc);
                }
            }
        }
        /// <inheritdoc />
        public override bool CreateJob(JobSpec jobSpec, DateTime creationTimeUtc, CreateJobConflictAction conflictAction)
        {
            if (jobSpec == null)
            {
                throw new ArgumentNullException("jobSpec");
            }
            if (!Enum.IsDefined(typeof(CreateJobConflictAction), conflictAction))
            {
                throw new ArgumentOutOfRangeException("conflictAction");
            }

            ThrowIfDisposed();

            return(jobStoreDao.CreateJob(clusterName, jobSpec, creationTimeUtc, conflictAction));
        }
        /// <inheritdoc />
        public bool CreateJob(JobSpec jobSpec, CreateJobConflictAction conflictAction)
        {
            if (jobSpec == null)
            {
                throw new ArgumentNullException("jobSpec");
            }
            if (!Enum.IsDefined(typeof(CreateJobConflictAction), conflictAction))
            {
                throw new ArgumentOutOfRangeException("conflictAction");
            }

            ThrowIfDisposed();
            ThrowIfNotInitialized();

            return(jobStore.CreateJob(jobSpec, DateTime.UtcNow, conflictAction));
        }
        /// <inheritdoc />
        public override bool CreateJob(JobSpec jobSpec, DateTime creationTimeUtc, CreateJobConflictAction conflictAction)
        {
            if (jobSpec == null)
            {
                throw new ArgumentNullException("jobSpec");
            }
            if (!Enum.IsDefined(typeof(CreateJobConflictAction), conflictAction))
            {
                throw new ArgumentOutOfRangeException("conflictAction");
            }

            lock (jobs)
            {
                ThrowIfDisposed();

                VersionedJobDetails existingJobDetails;
                if (jobs.TryGetValue(jobSpec.Name, out existingJobDetails))
                {
                    switch (conflictAction)
                    {
                    case CreateJobConflictAction.Ignore:
                        return(false);

                    case CreateJobConflictAction.Update:
                        InternalUpdateJob(existingJobDetails, jobSpec);
                        return(true);

                    case CreateJobConflictAction.Replace:
                        break;

                    case CreateJobConflictAction.Throw:
                        throw new SchedulerException(String.Format(CultureInfo.CurrentCulture,
                                                                   "There is already a job with name '{0}'.", jobSpec.Name));
                    }
                }

                VersionedJobDetails jobDetails = new VersionedJobDetails(jobSpec.Clone(), creationTimeUtc, 0);

                jobs[jobSpec.Name] = jobDetails;
                Monitor.PulseAll(jobs);
                return(true);
            }
        }
Exemple #5
0
        /// <inheritdoc />
        public override bool CreateJob(JobSpec jobSpec, DateTime creationTimeUtc, CreateJobConflictAction conflictAction)
        {
            if (jobSpec == null)
                throw new ArgumentNullException("jobSpec");
            if (!Enum.IsDefined(typeof (CreateJobConflictAction), conflictAction))
                throw new ArgumentOutOfRangeException("conflictAction");

            lock (jobs)
            {
                ThrowIfDisposed();

                VersionedJobDetails existingJobDetails;
                if (jobs.TryGetValue(jobSpec.Name, out existingJobDetails))
                {
                    switch (conflictAction)
                    {
                        case CreateJobConflictAction.Ignore:
                            return false;

                        case CreateJobConflictAction.Update:
                            InternalUpdateJob(existingJobDetails, jobSpec);
                            return true;

                        case CreateJobConflictAction.Replace:
                            break;

                        case CreateJobConflictAction.Throw:
                            throw new SchedulerException(String.Format(CultureInfo.CurrentCulture,
                                                                       "There is already a job with name '{0}'.", jobSpec.Name));
                    }
                }

                VersionedJobDetails jobDetails = new VersionedJobDetails(jobSpec.Clone(), creationTimeUtc, 0);

                jobs[jobSpec.Name] = jobDetails;
                Monitor.PulseAll(jobs);
                return true;
            }
        }
Exemple #6
0
        public void CreateJob_HandlesDuplicatesAccordingToConflictAction(CreateJobConflictAction conflictAction,
		                                                                 bool expectedResult)
        {
            Assert.IsTrue(jobStore.CreateJob(dummyJobSpec, new DateTime(2007, 5, 31), CreateJobConflictAction.Ignore));

            Assert.AreEqual(expectedResult, jobStore.CreateJob(dummyJobSpec, new DateTime(2007, 6, 1), conflictAction));

            if (expectedResult)
            {
                JobDetails jobDetails = jobStore.GetJobDetails(dummyJobSpec.Name);

                // In Update mode, the creation time should remain the same as the original job.
                // We should also preserve its history (though we don't check this right now.  -- TODO)
                if (conflictAction == CreateJobConflictAction.Update)
                    DateTimeAssert.AreEqualIncludingKind(new DateTime(2007, 5, 31, 0, 0, 0, DateTimeKind.Utc),
                                                         jobDetails.CreationTimeUtc);

                // In Replace mode, the creation time should be that of the newly created job.
                if (conflictAction == CreateJobConflictAction.Replace)
                    DateTimeAssert.AreEqualIncludingKind(new DateTime(2007, 6, 1, 0, 0, 0, DateTimeKind.Utc),
                                                         jobDetails.CreationTimeUtc);
            }
        }
		/// <summary>
		/// Creates a job in the database.
		/// </summary>
		/// <param name="clusterName">The cluster name, never null</param>
		/// <param name="jobSpec">The job specification, never null</param>
		/// <param name="creationTimeUtc">The job creation time</param>
		/// <param name="conflictAction">The action to take if a conflict occurs</param>
		/// <returns>True if the job was created or updated, false if a conflict occurred
		/// and no changes were made</returns>
		/// <exception cref="SchedulerException">Thrown if an error occurs</exception>
		public virtual bool CreateJob(string clusterName, JobSpec jobSpec, DateTime creationTimeUtc,
		                              CreateJobConflictAction conflictAction)
		{
			try
			{
				using (IDbConnection connection = CreateConnection())
				{
					IDbCommand command = CreateStoredProcedureCommand(connection, "spSCHED_CreateJob");

					CreateJobConflictActionCode conflictActionCode;
					switch (conflictAction)
					{
						default:
						case CreateJobConflictAction.Ignore:
						case CreateJobConflictAction.Throw:
							conflictActionCode = CreateJobConflictActionCode.Ignore;
							break;

						case CreateJobConflictAction.Replace:
							conflictActionCode = CreateJobConflictActionCode.Replace;
							break;

						case CreateJobConflictAction.Update:
							conflictActionCode = CreateJobConflictActionCode.Update;
							break;
					}

					AddInputParameter(command, "ClusterName", DbType.String, clusterName);
					AddInputParameter(command, "JobName", DbType.String, jobSpec.Name);
					AddInputParameter(command, "JobDescription", DbType.String, jobSpec.Description);
					AddInputParameter(command, "JobKey", DbType.String, jobSpec.JobKey);
					AddInputParameter(command, "TriggerObject", DbType.Binary,
					                  DbUtils.MapObjectToDbValue(DbUtils.SerializeObject(jobSpec.Trigger)));
					AddInputParameter(command, "JobDataObject", DbType.Binary,
					                  DbUtils.MapObjectToDbValue(DbUtils.SerializeObject(jobSpec.JobData)));
					AddInputParameter(command, "CreationTime", DbType.DateTime, creationTimeUtc);
					AddInputParameter(command, "ConflictActionCode", DbType.Int32, conflictActionCode);
					IDbDataParameter resultCodeParam = AddOutputParameter(command, "ResultCode", DbType.Int32);

					connection.Open();
					command.ExecuteNonQuery();

					CreateJobResultCode resultCode = (CreateJobResultCode) resultCodeParam.Value;
					switch (resultCode)
					{
						case CreateJobResultCode.JobCreated:
						case CreateJobResultCode.JobReplaced:
						case CreateJobResultCode.JobUpdated:
							return true;

						case CreateJobResultCode.JobWithSameNameExists:
							if (conflictAction == CreateJobConflictAction.Throw)
								throw new SchedulerException(String.Format(CultureInfo.CurrentCulture,
								                                           "Job '{0}' already exists.", jobSpec.Name));
							return false;

						default:
							throw new SchedulerException(String.Format(CultureInfo.CurrentCulture,
							                                           "spSCHED_CreateJob returned unrecognized result code '{0}'.",
							                                           resultCode));
					}
				}
			}
			catch (Exception ex)
			{
				throw new SchedulerException("The job store was unable to create a job in the database.", ex);
			}
		}
 public void CreateJob_ReturnsTrueIfCreated(CreateJobConflictAction conflictAction)
 {
     Assert.IsTrue(jobStore.CreateJob(dummyJobSpec, DateTime.UtcNow, conflictAction));
 }
Exemple #9
0
 /// <inheritdoc />
 public abstract bool CreateJob(JobSpec jobSpec, DateTime creationTimeUtc, CreateJobConflictAction conflictAction);
        /// <summary>
        /// Creates a job in the database.
        /// </summary>
        /// <param name="clusterName">The cluster name, never null</param>
        /// <param name="jobSpec">The job specification, never null</param>
        /// <param name="creationTimeUtc">The job creation time</param>
        /// <param name="conflictAction">The action to take if a conflict occurs</param>
        /// <returns>True if the job was created or updated, false if a conflict occurred
        /// and no changes were made</returns>
        /// <exception cref="SchedulerException">Thrown if an error occurs</exception>
        public virtual bool CreateJob(string clusterName, JobSpec jobSpec, DateTime creationTimeUtc,
                                      CreateJobConflictAction conflictAction)
        {
            try
            {
                using (IDbConnection connection = CreateConnection())
                {
                    IDbCommand command = CreateStoredProcedureCommand(connection, "spSCHED_CreateJob");

                    CreateJobConflictActionCode conflictActionCode;
                    switch (conflictAction)
                    {
                    default:
                    case CreateJobConflictAction.Ignore:
                    case CreateJobConflictAction.Throw:
                        conflictActionCode = CreateJobConflictActionCode.Ignore;
                        break;

                    case CreateJobConflictAction.Replace:
                        conflictActionCode = CreateJobConflictActionCode.Replace;
                        break;

                    case CreateJobConflictAction.Update:
                        conflictActionCode = CreateJobConflictActionCode.Update;
                        break;
                    }

                    AddInputParameter(command, "ClusterName", DbType.String, clusterName);
                    AddInputParameter(command, "JobName", DbType.String, jobSpec.Name);
                    AddInputParameter(command, "JobDescription", DbType.String, jobSpec.Description);
                    AddInputParameter(command, "JobKey", DbType.String, jobSpec.JobKey);
                    AddInputParameter(command, "TriggerObject", DbType.Binary,
                                      DbUtils.MapObjectToDbValue(DbUtils.SerializeObject(jobSpec.Trigger)));
                    AddInputParameter(command, "JobDataObject", DbType.Binary,
                                      DbUtils.MapObjectToDbValue(DbUtils.SerializeObject(jobSpec.JobData)));
                    AddInputParameter(command, "CreationTime", DbType.DateTime, creationTimeUtc);
                    AddInputParameter(command, "ConflictActionCode", DbType.Int32, conflictActionCode);
                    IDbDataParameter resultCodeParam = AddOutputParameter(command, "ResultCode", DbType.Int32);

                    connection.Open();
                    command.ExecuteNonQuery();

                    CreateJobResultCode resultCode = (CreateJobResultCode)resultCodeParam.Value;
                    switch (resultCode)
                    {
                    case CreateJobResultCode.JobCreated:
                    case CreateJobResultCode.JobReplaced:
                    case CreateJobResultCode.JobUpdated:
                        return(true);

                    case CreateJobResultCode.JobWithSameNameExists:
                        if (conflictAction == CreateJobConflictAction.Throw)
                        {
                            throw new SchedulerException(String.Format(CultureInfo.CurrentCulture,
                                                                       "Job '{0}' already exists.", jobSpec.Name));
                        }
                        return(false);

                    default:
                        throw new SchedulerException(String.Format(CultureInfo.CurrentCulture,
                                                                   "spSCHED_CreateJob returned unrecognized result code '{0}'.",
                                                                   resultCode));
                    }
                }
            }
            catch (Exception ex)
            {
                throw new SchedulerException("The job store was unable to create a job in the database.", ex);
            }
        }
Exemple #11
0
 public void CreateJob_ReturnsTrueIfCreated(CreateJobConflictAction conflictAction)
 {
     Assert.IsTrue(jobStore.CreateJob(dummyJobSpec, DateTime.UtcNow, conflictAction));
 }
Exemple #12
0
        /// <inheritdoc />
        public override bool CreateJob(JobSpec jobSpec, DateTime creationTimeUtc, CreateJobConflictAction conflictAction)
        {
            if (jobSpec == null)
                throw new ArgumentNullException("jobSpec");
            if (!Enum.IsDefined(typeof (CreateJobConflictAction), conflictAction))
                throw new ArgumentOutOfRangeException("conflictAction");

            ThrowIfDisposed();

            return jobStoreDao.CreateJob(clusterName, jobSpec, creationTimeUtc, conflictAction);
        }
		/// <summary>
		/// Creates a job in the database.
		/// </summary>
		/// <param name="clusterName">The cluster name, never null</param>
		/// <param name="jobSpec">The job specification, never null</param>
		/// <param name="creationTimeUtc">The job creation time</param>
		/// <param name="conflictAction">The action to take if a conflict occurs</param>
		/// <returns>True if the job was created or updated, false if a conflict occurred
		/// and no changes were made</returns>
		/// <exception cref="SchedulerException">Thrown if an error occurs</exception>
		public virtual bool CreateJob(string clusterName, JobSpec jobSpec, DateTime creationTimeUtc,
									  CreateJobConflictAction conflictAction)
		{
			try
			{
				using (IDbConnection connection = CreateConnection())
				{
					connection.Open();
					using (IDbTransaction transaction = connection.BeginTransaction())
					{
						try
						{
							//
							// Find the cluster.
							//

							int? clusterId = null;
							using (IDbCommand command = CreateCommand(connection, transaction, CreateJobSelectClusterId))
							{
								AddInputParameter(command, "ClusterName", DbType.String, clusterName);

								using (IDataReader rs = command.ExecuteReader())
								{
									if (rs.Read())
									{
										clusterId = (int)rs["clusterID"];
									}
								}
							}

							if (!clusterId.HasValue)
							{
								throw new SchedulerException("Could not create job because cluster name was not registered.");
							}

							//
							// Find the job if it already exists.
							//

							int? jobId = null;
							using (IDbCommand command = CreateCommand(connection, transaction, CreateJobSelectJobId))
							{
								AddInputParameter(command, "ClusterID", DbType.Int32, clusterId.Value);
								AddInputParameter(command, "JobName", DbType.String, jobSpec.Name);

								using (IDataReader rs = command.ExecuteReader())
								{
									if (rs.Read())
									{
										jobId = (int)rs["JobID"];
									}
								}
							}

							if (jobId.HasValue)
							{
								if (conflictAction == CreateJobConflictAction.Ignore)
								{
									transaction.Rollback();
									return false;
								}
								else if (conflictAction == CreateJobConflictAction.Throw)
								{
									throw new SchedulerException(String.Format(CultureInfo.CurrentCulture,
																			   "Job '{0}' already exists.", jobSpec.Name));
								}
								else if (conflictAction == CreateJobConflictAction.Replace)
								{
									using (IDbCommand command = CreateCommand(connection, transaction, CreateJobDeleteJob))
									{
										AddInputParameter(command, "JobID", DbType.Int32, jobId.Value);

										command.ExecuteNonQuery();
									}
								}
								else if (conflictAction == CreateJobConflictAction.Update)
								{
									using (IDbCommand command = CreateCommand(connection, transaction, CreateJobUpdateJob))
									{
										AddInputParameter(command, "JobDescription", DbType.String, jobSpec.Description);
										AddInputParameter(command, "JobKey", DbType.String, jobSpec.JobKey);
										AddInputParameter(command, "TriggerObject", DbType.Binary,
														  DbUtils.MapObjectToDbValue(DbUtils.SerializeObject(jobSpec.Trigger)));
										AddInputParameter(command, "JobDataObject", DbType.Binary,
														  DbUtils.MapObjectToDbValue(DbUtils.SerializeObject(jobSpec.JobData)));

										AddInputParameter(command, "JobState_Scheduled", DbType.Int32, (int)JobState.Scheduled);
										AddInputParameter(command, "JobState_Pending", DbType.Int32, (int)JobState.Pending);

										command.ExecuteNonQuery();
									}

									return true;
								}
								else
								{
									throw new NotSupportedException("Unexpected conflict action on duplicate job name.");
								}
							}

							//
							// Insert new job.
							//

							using (IDbCommand command = CreateCommand(connection, transaction, CreateJobInsertJob))
							{
								AddInputParameter(command, "ClusterID", DbType.Int32, clusterId.Value);
								AddInputParameter(command, "JobName", DbType.String, jobSpec.Name);
								AddInputParameter(command, "JobDescription", DbType.String, jobSpec.Description);
								AddInputParameter(command, "JobKey", DbType.String, jobSpec.JobKey);
								AddInputParameter(command, "TriggerObject", DbType.Binary,
												  DbUtils.MapObjectToDbValue(DbUtils.SerializeObject(jobSpec.Trigger)));
								AddInputParameter(command, "JobDataObject", DbType.Binary,
												  DbUtils.MapObjectToDbValue(DbUtils.SerializeObject(jobSpec.JobData)));
								AddInputParameter(command, "CreationTime", DbType.DateTime, creationTimeUtc);

								AddInputParameter(command, "JobState_Pending", DbType.Int32, (int)JobState.Pending);

								command.ExecuteNonQuery();

								transaction.Commit();
							}

							return true;
						}
						catch (Exception)
						{
							transaction.Rollback();
							throw;
						}
					}
				}
			}
			catch (Exception ex)
			{
				throw new SchedulerException("The job store was unable to create a job in the database.", ex);
			}
		}
		/// <inheritdoc />
		public bool CreateJob(JobSpec jobSpec, CreateJobConflictAction conflictAction)
		{
			if (jobSpec == null)
				throw new ArgumentNullException("jobSpec");
			if (!Enum.IsDefined(typeof (CreateJobConflictAction), conflictAction))
				throw new ArgumentOutOfRangeException("conflictAction");

			ThrowIfDisposed();
			ThrowIfNotInitialized();

			return jobStore.CreateJob(jobSpec, DateTime.UtcNow, conflictAction);
		}
Exemple #15
0
 /// <inheritdoc />
 public abstract bool CreateJob(JobSpec jobSpec, DateTime creationTimeUtc, CreateJobConflictAction conflictAction);