Esempio n. 1
0
        public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
        {

            Logger.Trace(DateTime.Now.ToLongTimeString() + " enter Dequeue()");

            MySqlFetchedJob job = null;
            do
            {
                cancellationToken.ThrowIfCancellationRequested();
                UsingDatabase(db =>
                {

                    string token = Guid.NewGuid().ToString();
                    int nUpdated = db.GetTable<JobQueue>(true)
                        .Where(jq => jq.FetchedAt == NullDateTime)
                        .Where(jq => queues.Contains(jq.Queue))
                        .Take(1)
                        .Set(jq => jq.FetchedAt, DateTime.UtcNow)
                        .Set(jq => jq.FetchToken, token)
                        .Update();

                    if (nUpdated != 0)
                    {

                        nUpdated.Should().Be(1);
                        var jobQueue = db.GetTable<JobQueue>(true).Single(jq => jq.FetchToken == token);

                        Logger.Trace(DateTime.Now.ToLongTimeString() + " returning fetched job " + jobQueue.JobId);

                        job = new MySqlFetchedJob(ConnectionString,
                            jobQueue.Id,
                            jobQueue.JobId.ToString(CultureInfo.InvariantCulture),
                            jobQueue.Queue);
                    }


                    Logger.Trace(DateTime.Now.ToLongTimeString() + " waiting");


                    cancellationToken.WaitHandle.WaitOne(_options.QueuePollInterval);
                    cancellationToken.ThrowIfCancellationRequested();


                });
            } while (job == null);

            return job;

        }
Esempio n. 2
0
        public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
        {
            Logger.Trace(DateTime.Now.ToLongTimeString() + " enter Dequeue()");

            MySqlFetchedJob job = null;

            do
            {
                cancellationToken.ThrowIfCancellationRequested();
                UsingDatabase(db =>
                {
                    string token = Guid.NewGuid().ToString();
                    int nUpdated = db.GetTable <JobQueue>(true)
                                   .Where(jq => jq.FetchedAt == NullDateTime)
                                   .Where(jq => queues.Contains(jq.Queue))
                                   .Take(1)
                                   .Set(jq => jq.FetchedAt, DateTime.UtcNow)
                                   .Set(jq => jq.FetchToken, token)
                                   .Update();

                    if (nUpdated != 0)
                    {
                        nUpdated.Should().Be(1);
                        var jobQueue = db.GetTable <JobQueue>(true).Single(jq => jq.FetchToken == token);

                        Logger.Trace(DateTime.Now.ToLongTimeString() + " returning fetched job " + jobQueue.JobId);

                        job = new MySqlFetchedJob(ConnectionString,
                                                  jobQueue.Id,
                                                  jobQueue.JobId.ToString(CultureInfo.InvariantCulture),
                                                  jobQueue.Queue);
                    }


                    Logger.Trace(DateTime.Now.ToLongTimeString() + " waiting");


                    cancellationToken.WaitHandle.WaitOne(_options.QueuePollInterval);
                    cancellationToken.ThrowIfCancellationRequested();
                });
            } while (job == null);

            return(job);
        }