/// <summary> /// Delete server information form the database using server's identifier. /// </summary> /// <param name="serverId">The server identifier.</param> public void DeleteServer(Int64 serverId) { logger.WriteTrace("Deleting server by its identifier ({0}) from the database...", serverId); if (serverId <= 0) { throw new ArgumentException("Server's identifier should be greate than 0. Now it is " + serverId, "serverId"); } string sqlCommand = string.Format(deleteStatement, sqlSettings.WiseQueueDefaultSchema, serverTableName, taskTableName, serverId, (short)TaskStates.New, (short)TaskStates.Running); logger.WriteTrace("The SqlCommand has been generated. Result: {0}", sqlCommand); logger.WriteTrace("Executing sql command..."); using (IDbConnection connection = connectionFactory.CreateConnection()) { using (IDbCommand command = connectionFactory.CreateCommand(connection)) { command.CommandText = sqlCommand; int affectedRows = command.ExecuteNonQuery(); logger.WriteTrace("The command has been executed. The entity has been deleted. Affected rows = {0}.", affectedRows); } } }
/// <summary> /// Get queue by its name. /// </summary> /// <param name="queueName">The queue name.</param> /// <returns>The <see cref="QueueModel"/> instance.</returns> public QueueModel GetQueueByName(string queueName) { string sqlCommand = string.Format("SELECT * FROM {0}.{1} WHERE [Name] = '{2}'", sqlSettings.WiseQueueDefaultSchema, queueTableName, queueName); logger.WriteTrace("Executing sql command..."); using (IDbConnection connection = connectionFactory.CreateConnection()) { using (IDbCommand command = connectionFactory.CreateCommand(connection)) { command.CommandText = sqlCommand; using (IDataReader rdr = command.ExecuteReader()) { while (rdr.Read()) { QueueEntity queueEntity = new QueueEntity { Id = (Int64)rdr["Id"], Name = (string)rdr["Name"], Description = (string)rdr["Description"] }; QueueModel queueModel = queueConverter.Convert(queueEntity); logger.WriteTrace("The command has been executed. Result = {0}", queueModel); return(queueModel); } } } } return(null); //TODO: It is not a good idea. We should return something like NullTaskEntity. }
/// <summary> /// Insert a task into the storage. /// </summary> /// <param name="taskModel">The <see cref="TaskModel"/> instance.</param> /// <returns>The task's identifier.</returns> /// <exception cref="ArgumentException">Task's identifier should be 0.</exception> public Int64 InsertTask(TaskModel taskModel) { logger.WriteTrace("Inserting {0} into the database...", taskModel); if (taskModel.Id != 0) { throw new ArgumentException("Task's identifier should be 0. Now it is " + taskModel.Id, "taskModel"); } logger.WriteTrace("Converting {0} into the TaskEntity...", taskModel); TaskEntity taskEntity = taskConverter.Convert(taskModel); logger.WriteTrace("{0} has been converted. Generating sql command for {1}...", taskModel, taskEntity); string instanceType = taskEntity.InstanceType; string method = taskEntity.Method; string parametersTypes = taskEntity.ParametersTypes; string arguments = taskEntity.Arguments; Int64 queueId = taskEntity.QueueId; string sqlCommand = string.Format(insertStatement, sqlSettings.WiseQueueDefaultSchema, taskTableName, (short)taskEntity.TaskState, instanceType, method, parametersTypes, arguments, queueId); logger.WriteTrace("The SqlCommand has been generated. Result: {0}", sqlCommand); logger.WriteTrace("Executing sql command..."); using (IDbConnection connection = connectionFactory.CreateConnection()) { using (IDbCommand command = connectionFactory.CreateCommand(connection)) { command.CommandText = sqlCommand; Int64 taskId = (Int64)command.ExecuteScalar(); logger.WriteTrace("The command has been executed. TaskId = {0}", taskId); return(taskId); } } }
/// <summary> /// Get available task from the storage. /// </summary> /// <param name="specification">The <see cref="TaskRequestSpecification"/> instance.</param> /// <param name="taskModels">List of <see cref="WiseQueue.Core.Common.Models.Tasks.TaskStateModel"/> instances if it has been found</param> /// <returns>True if the list of TaskModel instances has been populated. Otherwise, false.</returns> public bool TryGetAvailableTask(TaskRequestSpecification specification, out List <TaskModel> taskModels) { if (specification == null) { throw new ArgumentNullException("specification"); } StringBuilder stringBuilder = new StringBuilder(); stringBuilder.Append("Declare @TempTable table ([Id] [bigint], "); stringBuilder.Append("[QueueId] [bigint], "); stringBuilder.Append("[ServerId] [bigint] NULL, "); stringBuilder.Append("[State] [smallint], "); stringBuilder.Append("[CompletedAt] [datetime] NULL, "); stringBuilder.Append("[ExecuteAt] [datetime] NOT NULL, "); stringBuilder.Append("[RepeatCrashCount] [int] NOT NULL, "); stringBuilder.Append("[InstanceType] [nvarchar](4000), "); stringBuilder.Append("[Method] [nvarchar](4000), "); stringBuilder.Append("[ParametersTypes] [nvarchar](4000), "); stringBuilder.AppendLine("[Arguments] [nvarchar](4000)); "); stringBuilder.AppendFormat("UPDATE TOP ({0}) {1}.{2} ", specification.MaxTasks, sqlSettings.WiseQueueDefaultSchema, taskTableName); stringBuilder.AppendFormat("SET State = {0}, ", (short)TaskStates.Pending); stringBuilder.AppendFormat("ServerId = {0} ", specification.ServerId); //stringBuilder.Append("RepeatCount = RepeatCount - 1 "); stringBuilder.Append("OUTPUT inserted.* INTO @TempTable "); stringBuilder.AppendFormat("Where (State = {0} ", (short)TaskStates.New); stringBuilder.AppendFormat("OR ( (State = {0} OR State = {1}) AND [ServerId] IS NULL)) ", (short)TaskStates.Pending, (short)TaskStates.Running); stringBuilder.AppendFormat("AND (QueueId = {0}) AND ([ExecuteAt] <= GETUTCDATE()) AND [RepeatCrashCount] > 0;", specification.QueueId); stringBuilder.AppendLine(); stringBuilder.AppendLine("SELECT * FROM @TempTable"); using (IDbConnection connection = connectionFactory.CreateConnection()) { using (IDbTransaction transaction = connection.BeginTransaction(IsolationLevel.Serializable)) { using (IDbCommand command = connectionFactory.CreateCommand(connection, transaction)) { command.CommandText = stringBuilder.ToString(); using (IDataReader rdr = command.ExecuteReader()) { taskModels = new List <TaskModel>(); while (rdr.Read()) { Int64 id = (Int64)rdr["Id"]; Int64 queueId = (Int64)rdr["QueueId"]; Int64 serverId = (Int64)rdr["ServerId"]; TaskStates taskState = (TaskStates)(short)rdr["State"]; int repeatCrashCount = (int)rdr["RepeatCrashCount"]; string typeDetails = (string)rdr["InstanceType"]; string methodDetails = (string)rdr["Method"]; string parameterDetails = (string)rdr["ParametersTypes"]; string argumentDetails = (string)rdr["Arguments"]; TaskEntity taskEntity = new TaskEntity { Id = id, QueueId = queueId, ServerId = serverId, TaskState = taskState, InstanceType = typeDetails, Method = methodDetails, ParametersTypes = parameterDetails, Arguments = argumentDetails, RepeatCrashCount = repeatCrashCount }; TaskModel taskModel = taskConverter.Convert(taskEntity); taskModels.Add(taskModel); } } } if (taskModels.Count > 0) { transaction.Commit(); return(true); } } return(false); } }