Esempio n. 1
0
        public static void Delete(Database database, string name)
        {
            if (database == null)
            {
                throw new ArgumentNullException("database", "database is null.");
            }
            if (name == null)
            {
                throw new ArgumentNullException("name", "name is null.");
            }
            if (!Queue.Exists(database, name))
            {
                throw new ArgumentException(string.Format("Queue with the name \"{0}\" not found.", name), "name");
            }
            if (!Queue.IsEmpty(database, name))
            {
                throw new InvalidOperationException(string.Format("Failed deleting queue with name \"{0}\", queue is not empty.", name));
            }

            var queue = Queue.Get(database, name);

            queue.QueueLock.EnterWriteLock();
            try
            {
                if (queue.QueuedTasks.Any())
                {
                    throw new InvalidOperationException(string.Format("Failed deleting queue with name \"{0}\", queue is not empty.", name));
                }

                database.QueuesLock.EnterWriteLock();
                try
                {
                    queue.QueuedTasks = null;
                    database.Queues.Remove(queue.Id);
                    database.QueueNames.Remove(queue.Name);
                }
                finally
                {
                    database.QueuesLock.ExitWriteLock();
                }
            }
            finally
            {
                queue.QueueLock.ExitWriteLock();
            }
        }
Esempio n. 2
0
        public static QueueTask Dequeue(Database database, string queueName)
        {
            if (database == null)
            {
                throw new ArgumentNullException("database", "database is null.");
            }
            if (queueName == null)
            {
                throw new ArgumentNullException("queueName", "queueName is null.");
            }

            if (Queue.IsEmpty(database, queueName))
            {
                return(null);
            }

            Workflow workflow;
            Task     task;
            Queue    queue;
            Guid     workflowId, taskId;

            for (int attemptNum = 0; attemptNum < MAX_DEQUEUE_ATTEMPTS; attemptNum++)
            {
                queue = Queue.Get(database, queueName);
                queue.QueueLock.EnterReadLock();
                try
                {
                    if (queue.QueuedTasks.Count == 0)
                    {
                        return(null);
                    }
                    var first = queue.QueuedTasks.First.Value;
                    workflowId = first.Item1;
                    taskId     = first.Item2;
                }
                finally
                {
                    queue.QueueLock.ExitReadLock();
                }

                workflow = Workflow.Get(database, workflowId);
                if (workflow == null)
                {
                    continue;
                }
                workflow.WorkflowLock.EnterReadLock();
                try
                {
                    task = workflow.Tasks[taskId];
                }
                finally
                {
                    workflow.WorkflowLock.ExitReadLock();
                }

                workflow.WorkflowLock.EnterWriteLock();
                try
                {
                    queue.QueueLock.EnterWriteLock();
                    try
                    {
                        // Validate state;
                        if (queue.QueuedTasks.Count == 0)
                        {
                            return(null);
                        }
                        if (queue.QueuedTasks.First().Item2 != taskId)
                        {
                            continue;
                        }
                        queue.QueuedTasks.RemoveFirst();
                        queue.RunningTasks.Add(taskId);
                        task.State = TaskState.Running;
                        return(new QueueTask()
                        {
                            WorkflowName = workflow.Name,
                            TaskName = task.Name,
                        });
                    }
                    finally
                    {
                        queue.QueueLock.ExitWriteLock();
                    }
                }
                finally
                {
                    workflow.WorkflowLock.ExitWriteLock();
                }
            }
            return(null);
        }