/// <inheritdoc />
        public void Fail(Guid taskId, DateTime timestampUtc, Exception error)
        {
            if (error == null)
            {
                throw new ArgumentNullException(nameof(error));
            }

            Trace.WriteLine("ENTER: Record task '{0}' failed with error '{1}' ...".FormatInvariant(taskId, error.Message));

            string taskIdAsString = RedisConverter.ToString(taskId);

            string entityKey = RedisTaskRuntimeInfoRepository.GetEntityKey(taskIdAsString);

            RedisTaskRuntimeInfo taskInfo = this.GetById(taskId, false);

            taskInfo.Status       = TaskStatus.Failed;
            taskInfo.CompletedUtc = timestampUtc;
            taskInfo.Error        = error.ToString();

            byte[] content = this.serializer.Serialize(taskInfo);

            using (IRedisTransaction transaction = this.Provider.CreateTransaction())
            {
                transaction.RemoveKey(entityKey);
                transaction.RemoveFromList(RedisTaskRuntimeInfoRepository.ActiveTasksList, taskIdAsString);
                transaction.AddToList(RedisTaskRuntimeInfoRepository.FailedTasksList, taskIdAsString);
                transaction.SetHashValue(RedisTaskRuntimeInfoRepository.ArchiveTasksHash, taskIdAsString, content);

                transaction.Commit();
            }

            Trace.WriteLine("EXIT: Task '{0}' failed with error '{1}' recorded.".FormatInvariant(taskId, error.Message));
        }
        /// <inheritdoc />
        public void Add(ITaskRuntimeInfo taskInfo)
        {
            if (taskInfo == null)
            {
                throw new ArgumentNullException("taskInfo");
            }

            Trace.WriteLine("ENTER: Adding runtime information for task '{0}' of type '{1}' with priority '{2}' in polling queue '{3}' ...".FormatInvariant(taskInfo.TaskId, taskInfo.TaskType, taskInfo.Priority, taskInfo.PollingQueue));

            taskInfo.ValidateForAdd();

            string entityKey = RedisTaskRuntimeInfoRepository.GetEntityKey(taskInfo.TaskId);

            string addToListKey;

            Dictionary <string, string> values = new Dictionary <string, string>()
            {
                { "Id", RedisConverter.ToString(taskInfo.TaskId) },
                { "TaskType", RedisConverter.ToString(taskInfo.TaskType, true) },
                { "SubmittedUtc", RedisConverter.ToString(taskInfo.SubmittedUtc) },
                { "Status", RedisConverter.ToString(taskInfo.Status) }
            };

            if (string.IsNullOrEmpty(taskInfo.PollingQueue))
            {
                values.Add("Priority", RedisConverter.ToString(taskInfo.Priority));

                addToListKey = RedisTaskRuntimeInfoRepository.PendingTasksList;
            }
            else
            {
                values.Add("PollingQueue", taskInfo.PollingQueue);

                addToListKey = RedisTaskRuntimeInfoRepository.GetPollingQueueRedisKey(taskInfo.PollingQueue, TaskStatus.Pending);
            }

            using (IRedisTransaction transaction = this.Provider.CreateTransaction())
            {
                transaction.SetHashValues(entityKey, values);

                transaction.AddToList(addToListKey, RedisConverter.ToString(taskInfo.TaskId));

                transaction.Commit();
            }

            Trace.WriteLine("EXIT: Runtime information for task '{0}' of type '{1}' with priority '{2}' in polling queue '{3}' added.".FormatInvariant(taskInfo.TaskId, taskInfo.TaskType, taskInfo.Priority, taskInfo.PollingQueue));
        }
Beispiel #3
0
        /// <inheritdoc />
        public void Push(IUniqueMessage message)
        {
            if (message == null)
            {
                throw new ArgumentNullException("message");
            }

            if (string.IsNullOrEmpty(message.MessageUniqueId))
            {
                throw new ArgumentException("MessageUniqueId is null or empty.".FormatInvariant(message), "message");
            }

            if (this.isDisposed)
            {
                throw new ObjectDisposedException(this.GetType().Name);
            }

            Trace.WriteLine("ENTER: Adding message {0}:{1} to list '{2}' ...".FormatInvariant(message.GetType().Name, message.MessageUniqueId, RedisTaskProcessorMessageQueue.MessageQueueListKey));

            byte[] content = this.serializer.Serialize(message);

            if (this.serializer.CanDetermineEntityTypeFromContent)
            {
                this.provider.AddToList(RedisTaskProcessorMessageQueue.MessageQueueListKey, content);
            }
            else
            {
                string messageUniqueId = string.Join("$", message.GetType().Name, message.MessageUniqueId);

                using (IRedisTransaction transaction = this.provider.CreateTransaction())
                {
                    transaction.AddToList(RedisTaskProcessorMessageQueue.MessageQueueListKey, messageUniqueId);

                    transaction.SetHashValue(RedisTaskProcessorMessageQueue.MessageQueueContentKey, messageUniqueId, content);
                    transaction.SetHashValue(RedisTaskProcessorMessageQueue.MessageQueueContentKey, messageUniqueId + "$Type", RedisConverter.ToString(message.GetType(), false));

                    transaction.Commit();
                }
            }

            this.provider.PublishMessage(RedisTaskProcessorMessageQueue.MasterCommandsChannel, string.Empty);

            Trace.WriteLine("EXIT: Message {0}:{1} added to list '{2}'.".FormatInvariant(message.GetType().Name, message.MessageUniqueId, RedisTaskProcessorMessageQueue.MessageQueueListKey));
        }
        /// <inheritdoc />
        public void Start(Guid taskId, Guid taskProcessorId, DateTime timestampUtc)
        {
            Trace.WriteLine("ENTER: Recording task '{0}' started by processor '{1}' ...".FormatInvariant(taskId, taskProcessorId));

            string entityKey = RedisTaskRuntimeInfoRepository.GetEntityKey(taskId);

            Dictionary <string, string> values = new Dictionary <string, string>()
            {
                { "Status", RedisConverter.ToString(TaskStatus.InProgress) },
                { "TaskProcessorId", RedisConverter.ToString(taskProcessorId) },
                { "StartedUtc", RedisConverter.ToString(timestampUtc) }
            };

            using (IRedisTransaction transaction = this.Provider.CreateTransaction())
            {
                transaction.SetHashValues(entityKey, values);
                transaction.RemoveFromList(RedisTaskRuntimeInfoRepository.PendingTasksList, RedisConverter.ToString(taskId));
                transaction.AddToList(RedisTaskRuntimeInfoRepository.ActiveTasksList, RedisConverter.ToString(taskId));

                transaction.Commit();
            }

            Trace.WriteLine("EXIT: Task '{0}' assigned to processor '{1}' recorded.".FormatInvariant(taskId, taskProcessorId));
        }