コード例 #1
0
        public MySqlStorageConnectionTest()
        {
            var options    = GetService <MySqlOptions>();
            var capOptions = GetService <CapOptions>();

            _storage = new MySqlStorageConnection(options, capOptions);
        }
コード例 #2
0
    public MySqlStorageTransaction(MySqlStorageConnection connection)
    {
        var options = connection.Options;

        _prefix = options.TableNamePrefix;

        _dbConnection = new MySqlConnection(options.ConnectionString);
    }
コード例 #3
0
 private void UseConnections(Action <MySqlConnection, MySqlStorageConnection> action)
 {
     using (var sqlConnection = ConnectionUtils.CreateConnection())
     {
         var storage = new MySqlStorage(sqlConnection, new MySqlStorageOptions());
         using (var connection = new MySqlStorageConnection(storage, new MySqlStorageOptions()))
         {
             action(sqlConnection, connection);
         }
     }
 }
コード例 #4
0
        private void UseConnection(Action <MySqlStorageConnection> action)
        {
            using (var sql = ConnectionUtils.CreateConnection())
            {
                var storage = new Mock <MySqlStorage>(sql, new MySqlStorageOptions());
                storage.Setup(x => x.QueueProviders).Returns(_providers);

                using (var connection = new MySqlStorageConnection(storage.Object, new MySqlStorageOptions()))
                {
                    action(connection);
                }
            }
        }
コード例 #5
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");
            }

            FetchedJob      fetchedJob = null;
            MySqlConnection connection = null;

            do
            {
                cancellationToken.ThrowIfCancellationRequested();

                try
                {
                    connection = _storage.CreateAndOpenConnection();

                    //var joinedQueues = string.Join(",", queues.Select(q => "'" + q.Replace("'", "''") + "'"));

                    /*var resource = ("JobQueue:" + joinedQueues);
                     * if (resource.Length > 100)
                     *  resource = resource.Substring(0, 100);
                     *
                     * using (new MySqlDistributedLock(_storage, resource, TimeSpan.FromSeconds(70)))*/
                    {
                        string token = Guid.NewGuid().ToString();

                        int nUpdated;

                        while (!_semaphoreSlim.WaitOne(TimeSpan.FromSeconds(5)))
                        {
                            cancellationToken.ThrowIfCancellationRequested();
                        }
                        //_semaphoreSlim.Wait(cancellationToken);

                        try
                        {
                            nUpdated = MySqlStorageConnection.AttemptActionReturnObject(() => connection.Execute(
                                                                                            "update JobQueue set FetchedAt = DATE_ADD(UTC_TIMESTAMP(), INTERVAL @timeout SECOND), FetchToken = @fetchToken " +
                                                                                            "where (FetchedAt is null or FetchedAt < UTC_TIMESTAMP()) " +
                                                                                            "   and Queue in @queues " +
                                                                                            // "ORDER BY FIELD(Queue, " + joinedQueues + "), JobId " +
                                                                                            "ORDER BY Priority DESC, JobId " +
                                                                                            "LIMIT 1;",
                                                                                            new
                            {
                                queues     = queues,
                                timeout    = 45,  //_options.InvisibilityTimeout.Negate().TotalSeconds,
                                fetchToken = token
                            },
                                                                                            commandTimeout: 15), 3);
                        }
                        finally
                        {
                            _semaphoreSlim.Release();
                        }

                        if (nUpdated != 0)
                        {
                            fetchedJob =
                                MySqlStorageConnection.AttemptActionReturnObject(() => connection
                                                                                 .Query <FetchedJob>(
                                                                                     "select Id, JobId, Queue " +
                                                                                     "from JobQueue " +
                                                                                     "where FetchToken = @fetchToken;",
                                                                                     new
                            {
                                fetchToken = token
                            },
                                                                                     commandTimeout: 15)
                                                                                 .SingleOrDefault(), 3);

                            if (fetchedJob != null)
                            {
                                nUpdated = MySqlStorageConnection.AttemptActionReturnObject(() => connection.Execute(
                                                                                                "update JobQueue set FetchedAt = DATE_ADD(UTC_TIMESTAMP(), INTERVAL @timeout SECOND), FetchToken = @fetchToken " +
                                                                                                "where FetchToken = @fetchToken;",
                                                                                                new
                                {
                                    timeout    = _options.InvisibilityTimeout.TotalSeconds,
                                    fetchToken = token
                                }, commandTimeout: 15), 5);

                                if (nUpdated == 0)
                                {
                                    fetchedJob = null;
                                }
                            }
                        }
                    }
                }
                catch (MySqlException ex)
                {
                    Logger.ErrorException(ex.Message, ex);
                    throw;
                }
                finally
                {
                    _storage.ReleaseConnection(connection);
                }

                if (fetchedJob == null)
                {
                    cancellationToken.WaitHandle.WaitOne(_options.QueuePollInterval);
                    cancellationToken.ThrowIfCancellationRequested();
                }
            } while (fetchedJob == null);

            return(new MySqlFetchedJob(_storage, fetchedJob));
        }