Example #1
0
        public void RemoveFromQueue()
        {
            Logger.DebugFormat("RemoveFromQueue JobId={0}", JobId);
            using (var session = _storage.GetStatelessSession())
            {
                session.Query <_JobQueue>().Where(i => i.Id == _id).Delete();
            }

            _removedFromQueue = true;
        }
        public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
        {
            if (queues == null)
            {
                throw new ArgumentNullException(nameof(queues));
            }
            if (queues.Length == 0)
            {
                throw new ArgumentException("Queue array must be non-empty.", "queues");
            }
            Logger.Debug("Attempting to dequeue");

            var timeoutSeconds = _storage.Options.InvisibilityTimeout.Negate().TotalSeconds;

            while (!cancellationToken.IsCancellationRequested)
            {
                var fluentNHibernateDistributedLock = new FluentNHibernateDistributedLock(_storage, "JobQueue",
                                                                                          _storage.Options.JobQueueDistributedLockTimeout)
                                                      .Acquire();
                if (fluentNHibernateDistributedLock == null)
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        return(null);
                    }
                    cancellationToken.WaitHandle.WaitOne(_storage.Options.QueuePollInterval);
                }
                else
                {
                    using (fluentNHibernateDistributedLock)
                    {
                        var fluentNHibernateFetchedJob = SqlUtil.WrapForTransaction(() =>
                        {
                            var token = Guid.NewGuid().ToString();


                            using (var session = _storage.GetStatelessSession())
                            {
                                using (var transaction =
                                           session.BeginTransaction(IsolationLevel.Serializable))
                                {
                                    var jobQueueFetchedAt = _storage.UtcNow;

                                    var cutoff = jobQueueFetchedAt.AddSeconds(timeoutSeconds);
                                    if (Logger.IsDebugEnabled())
                                    {
                                        Logger.Debug(string.Format("Getting jobs where {0}=null or {0}<{1}",
                                                                   nameof(_JobQueue.FetchedAt), cutoff));
                                    }

                                    var jobQueue = session.Query <_JobQueue>()
                                                   .FirstOrDefault(i =>
                                                                   (i.FetchedAt == null ||
                                                                    i.FetchedAt < cutoff) && queues.Contains(i.Queue));
                                    if (jobQueue != null)
                                    {
                                        jobQueue.FetchToken = token;
                                        jobQueue.FetchedAt  = jobQueueFetchedAt;
                                        session.Update(jobQueue);
                                        transaction.Commit();


                                        Logger.DebugFormat("Dequeued job id {0} from queue {1}",
                                                           jobQueue.Job.Id,
                                                           jobQueue.Queue);
                                        var fetchedJob = new FetchedJob
                                        {
                                            Id    = jobQueue.Id,
                                            JobId = jobQueue.Job.Id,
                                            Queue = jobQueue.Queue
                                        };
                                        return(new FluentNHibernateFetchedJob(_storage, fetchedJob));
                                    }
                                }
                            }

                            return(null);
                        });
                        if (fluentNHibernateFetchedJob != null)
                        {
                            return(fluentNHibernateFetchedJob);
                        }
                    }
                }
            }

            return(null);
        }