Beispiel #1
0
        /// <summary>
        /// Constructs Job Storage by Mongo client, name and options
        /// </summary>
        /// <param name="mongoClient"></param>
        /// <param name="databaseName"></param>
        /// <param name="storageOptions"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public MongoStorage(MongoClient mongoClient, string databaseName, MongoStorageOptions storageOptions)
        {
            if (mongoClient == null)
            {
                throw new ArgumentNullException(nameof(mongoClient));
            }
            if (string.IsNullOrWhiteSpace(databaseName))
            {
                throw new ArgumentNullException(nameof(databaseName));
            }
            if (storageOptions == null)
            {
                throw new ArgumentNullException(nameof(storageOptions));
            }

            _mongoClient    = mongoClient;
            _databaseName   = databaseName;
            _storageOptions = storageOptions;

            _dbContext = new HangfireDbContext(mongoClient, databaseName, _storageOptions.Prefix);

            if (_storageOptions.CheckConnection)
            {
                CheckConnection();
            }

            MongoMigrationManager.MigrateIfNeeded(storageOptions, _dbContext.Database);
        }
        private void FullMigration(string seedFile)
        {
            using (var connection = new HangfireDbContext(ConnectionUtils.GetConnectionString(), "Hangfire-Mongo-Migration-Tests"))
            {
                // ARRANGE
                connection.Client.DropDatabase(connection.Database.DatabaseNamespace.DatabaseName);
                if (seedFile != null)
                {
                    SeedCollectionFromZipArchive(connection, Path.Combine("Migration", seedFile));
                }

                var storageOptions = new MongoStorageOptions
                {
                    MigrationOptions = new MongoMigrationOptions
                    {
                        Strategy       = MongoMigrationStrategy.Migrate,
                        BackupStrategy = MongoBackupStrategy.None
                    }
                };

                var migrationManager = new MongoMigrationManager(storageOptions);

                // ACT
                migrationManager.Migrate(connection);

                // ASSERT
            }
        }
Beispiel #3
0
        public void Migrate_Full_Success(string seedFile, bool assertCollectionHasItems)
        {
            using (var dbContext = new HangfireDbContext(ConnectionUtils.GetConnectionString(), "Hangfire-Mongo-Migration-Tests"))
            {
                // ARRANGE
                dbContext.Client.DropDatabase(dbContext.Database.DatabaseNamespace.DatabaseName);
                if (seedFile != null)
                {
                    SeedCollectionFromZipArchive(dbContext, Path.Combine("Migration", seedFile));
                }

                var storageOptions = new MongoStorageOptions
                {
                    MigrationOptions = new MongoMigrationOptions
                    {
                        Strategy       = MongoMigrationStrategy.Migrate,
                        BackupStrategy = MongoBackupStrategy.None
                    }
                };

                var migrationManager = new MongoMigrationManager(storageOptions, dbContext.Database);

                // ACT
                MongoMigrationManager.MigrateIfNeeded(storageOptions, dbContext.Database);

                // ASSERT
                AssertDataIntegrity(dbContext, assertCollectionHasItems);
            }
        }
Beispiel #4
0
        public void Migrate_DropNoBackup_Success()
        {
            using (var dbContext = new HangfireDbContext(ConnectionUtils.GetConnectionString(), "Hangfire-Mongo-Migration-Tests"))
            {
                // ARRANGE
                dbContext.Client.DropDatabase(dbContext.Database.DatabaseNamespace.DatabaseName);
                SeedCollectionFromZipArchive(dbContext, Path.Combine("Migration", "Hangfire-Mongo-Schema-006.zip"));

                var storageOptions = new MongoStorageOptions
                {
                    MigrationOptions = new MongoMigrationOptions
                    {
                        Strategy       = MongoMigrationStrategy.Drop,
                        BackupStrategy = MongoBackupStrategy.None
                    }
                };

                var migrationManager = new MongoMigrationManager(storageOptions, dbContext);

                // ACT
                migrationManager.Migrate();

                // ASSERT
                AssertDataIntegrity(dbContext, assertCollectionHasItems: false);
            }
        }
        /// <summary>
        /// Constructs Job Storage by Mongo client settings, name and options
        /// </summary>
        /// <param name="mongoClientSettings">Client settings for MongoDB</param>
        /// <param name="databaseName">Database name</param>
        /// <param name="storageOptions">Storage options</param>
        public MongoStorage(MongoClientSettings mongoClientSettings, string databaseName, MongoStorageOptions storageOptions)
        {
            if (mongoClientSettings == null)
            {
                throw new ArgumentNullException(nameof(mongoClientSettings));
            }
            if (string.IsNullOrWhiteSpace(databaseName))
            {
                throw new ArgumentNullException(nameof(databaseName));
            }
            if (storageOptions == null)
            {
                throw new ArgumentNullException(nameof(storageOptions));
            }

            _mongoClientSettings = mongoClientSettings;
            _databaseName        = databaseName;
            _storageOptions      = storageOptions;

            _dbContext = new HangfireDbContext(mongoClientSettings, databaseName, _storageOptions.Prefix);

            using (var migrationManager = new MongoMigrationManager(storageOptions, _dbContext))
            {
                migrationManager.Migrate();
            }
        }
        /// <summary>
        /// Constructs Job Storage by database connection string, name and options
        /// </summary>
        /// <param name="connectionString">MongoDB connection string</param>
        /// <param name="databaseName">Database name</param>
        /// <param name="storageOptions">Storage options</param>
        public MongoStorage(string connectionString, string databaseName, MongoStorageOptions storageOptions)
        {
            if (string.IsNullOrWhiteSpace(connectionString))
            {
                throw new ArgumentNullException(nameof(connectionString));
            }
            if (string.IsNullOrWhiteSpace(databaseName))
            {
                throw new ArgumentNullException(nameof(databaseName));
            }

            _connectionString = connectionString;
            _databaseName     = databaseName;
            _storageOptions   = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));

            Connection = new HangfireDbContext(connectionString, databaseName, storageOptions.Prefix);

            var migrationManager = new MongoMigrationManager(storageOptions);

            migrationManager.Migrate(Connection);

            var defaultQueueProvider = new MongoJobQueueProvider(_storageOptions);

            QueueProviders = new PersistentJobQueueProviderCollection(defaultQueueProvider);
        }
Beispiel #7
0
        public void Migrate_MultipleInstances_ThereCanBeOnlyOne()
        {
            using (var dbContext =
                       new HangfireDbContext(ConnectionUtils.GetConnectionString(), ConnectionUtils.GetDatabaseName()))
            {
                // ARRANGE
                dbContext.Database.DropCollection(dbContext.Schema.CollectionNamespace.CollectionName);
                var storageOptions = new MongoStorageOptions
                {
                    MigrationOptions = new MongoMigrationOptions
                    {
                        Strategy       = MongoMigrationStrategy.Drop,
                        BackupStrategy = MongoBackupStrategy.None
                    }
                };

                var signal        = new ManualResetEvent(false);
                var signalToStart = new ManualResetEvent(false);
                var taskCount     = 10;
                var tasks         = new Task <bool> [taskCount];
                var count         = 0;

                // ACT
                for (int i = 0; i < taskCount; i++)
                {
                    tasks[i] = Task.Factory.StartNew <bool>(() =>
                    {
                        count++;
                        if (count == taskCount)
                        {
                            signalToStart.Set();
                        }
                        signal.WaitOne();
                        return(MongoMigrationManager.MigrateIfNeeded(storageOptions, dbContext.Database));
                    }, TaskCreationOptions.LongRunning);
                }

                signalToStart.WaitOne();
                signal.Set();
                Task.WaitAll(tasks);

                // ASSERT
                Assert.True(tasks.Select(t => t.Result).Single(b => b));
            }
        }
Beispiel #8
0
        /// <summary>
        /// Constructs Job Storage by Mongo client, name and options
        /// </summary>
        /// <param name="mongoClient"></param>
        /// <param name="databaseName"></param>
        /// <param name="storageOptions"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public MongoStorage(MongoClient mongoClient, string databaseName, MongoStorageOptions storageOptions)
        {
            if (string.IsNullOrWhiteSpace(databaseName))
            {
                throw new ArgumentNullException(nameof(databaseName));
            }
            DatabaseName      = databaseName;
            MongoClient       = mongoClient ?? throw new ArgumentNullException(nameof(mongoClient));
            StorageOptions    = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));
            HangfireDbContext = StorageOptions.Factory.CreateDbContext(mongoClient, databaseName, storageOptions.Prefix);

            if (StorageOptions.CheckConnection)
            {
                CheckConnection();
            }

            MongoMigrationManager.MigrateIfNeeded(storageOptions, HangfireDbContext.Database);
        }
        /// <summary>
        /// Constructs Job Storage by Mongo client settings, name and options
        /// </summary>
        /// <param name="mongoClientSettings">Client settings for MongoDB</param>
        /// <param name="databaseName">Database name</param>
        /// <param name="storageOptions">Storage options</param>
        public MongoStorage(MongoClientSettings mongoClientSettings, string databaseName, MongoStorageOptions storageOptions)
        {
            _mongoClientSettings = mongoClientSettings ?? throw new ArgumentNullException(nameof(mongoClientSettings));
            _databaseName        = databaseName ?? throw new ArgumentNullException(nameof(databaseName));
            _storageOptions      = storageOptions ?? throw new ArgumentNullException(nameof(storageOptions));

            if (string.IsNullOrWhiteSpace(databaseName))
            {
                throw new ArgumentException("Please state a connection name", nameof(databaseName));
            }

            Connection = new HangfireDbContext(mongoClientSettings, databaseName, _storageOptions.Prefix);

            var migrationManager = new MongoMigrationManager(storageOptions);

            migrationManager.Migrate(Connection);

            var defaultQueueProvider = new MongoJobQueueProvider(_storageOptions);

            QueueProviders = new PersistentJobQueueProviderCollection(defaultQueueProvider);
        }
        public void Migrate_DropNoBackup_Success()
        {
            var dbContext = ConnectionUtils.CreateDbContext("Hangfire-Mongo-Migration-Tests");

            // ARRANGE
            dbContext.Client.DropDatabase(dbContext.Database.DatabaseNamespace.DatabaseName);
            SeedCollectionFromZipArchive(dbContext, Path.Combine("Migration", "Hangfire-Mongo-Schema-006.zip"));

            var storageOptions = new MongoStorageOptions
            {
                MigrationOptions = new MongoMigrationOptions
                {
                    MigrationStrategy = new DropMongoMigrationStrategy(),
                    BackupStrategy    = new NoneMongoBackupStrategy()
                }
            };

            // ACT
            MongoMigrationManager.MigrateIfNeeded(storageOptions, dbContext.Database);

            // ASSERT
            AssertDataIntegrity(dbContext, assertCollectionHasItems: false);
        }
        /// <summary>
        /// Initializes intial collections schema for Hangfire
        /// </summary>
        public void Init(MongoStorageOptions storageOptions)
        {
            var migrationManager = new MongoMigrationManager(storageOptions);

            migrationManager.Migrate(this);
        }