public void RemoveFromQueue()
 {
     Logger.TraceFormat("RemoveFromQueue JobId={0}", JobId);
     using (var session = _storage.GetSession())
     {
         session.CreateQuery(SqlUtil.DeleteJobQueueStatement)
         .SetParameter(SqlUtil.IdParameterName, _id)
         .ExecuteUpdate();
     }
     _removedFromQueue = true;
 }
Example #2
0
        public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
        {
            if (queues == null)
            {
                throw new ArgumentNullException("queues");
            }
            if (queues.Length == 0)
            {
                throw new ArgumentException("Queue array must be non-empty.", "queues");
            }
            queues = queues.Select(x => x.ToLower()).ToArray();
            Logger.Debug("Attempting to dequeue");


            while (true)
            {
                cancellationToken.ThrowIfCancellationRequested();


                try
                {
                    using (new FluentNHibernateDistributedLock(_storage, "JobQueue", TimeSpan.FromSeconds(60))
                           .Acquire())
                    {
                        var fluentNHibernateFetchedJob = SqlUtil.WrapForTransaction(() =>
                        {
                            var token = Guid.NewGuid().ToString();

                            if (queues.Any())
                            {
                                using (var session = _storage.GetSession())
                                {
                                    using (var transaction =
                                               session.BeginTransaction(IsolationLevel.Serializable))
                                    {
                                        var jobQueueFetchedAt = _storage.UtcNow;
                                        var next = jobQueueFetchedAt.AddSeconds(
                                            _options.InvisibilityTimeout.Negate().TotalSeconds);
                                        var jobQueue = session.Query <_JobQueue>()
                                                       .FirstOrDefault(i =>
                                                                       (i.FetchedAt == null ||
                                                                        i.FetchedAt < next) &&
                                                                       queues.Contains(i.Queue.ToLower()));
                                        if (jobQueue != null)
                                        {
                                            jobQueue.FetchedAt  = jobQueueFetchedAt;
                                            jobQueue.FetchToken = token;
                                            session.Update(jobQueue);
                                            session.Flush();

                                            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);
                        }
                    }
                }
                catch (FluentNHibernateDistributedLockException)
                {
                    // do nothing
                }
                catch (Exception ex)
                {
                    Logger.ErrorException(ex.Message, ex);
                    throw;
                }

                cancellationToken.WaitHandle.WaitOne(_options.QueuePollInterval);
                cancellationToken.ThrowIfCancellationRequested();
            }
        }
        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;

            try
            {
                while (true)
                {
                    cancellationToken.ThrowIfCancellationRequested();


                    try
                    {
                        using (new FluentNHibernateDistributedLock(_storage, "JobQueue", _storage.Options.JobQueueDistributedLockTimeout)
                               .Acquire())
                        {
                            var fluentNHibernateFetchedJob = SqlUtil.WrapForTransaction(() =>
                            {
                                var token = Guid.NewGuid().ToString();

                                if (queues.Any())
                                {
                                    using (var session = _storage.GetSession())
                                    {
                                        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);
                            }
                        }
                    }
                    catch (FluentNHibernateDistributedLockException)
                    {
                        // do nothing
                    }
                    catch (Exception ex)
                    {
                        Logger.ErrorException(ex.Message, ex);
                        throw;
                    }

                    cancellationToken.WaitHandle.WaitOne(_storage.Options.QueuePollInterval);
                    cancellationToken.ThrowIfCancellationRequested();
                }
            }
            catch (OperationCanceledException)
            {
                Logger.Debug("The dequeue operation was cancelled.");
                return(null);
            }
        }