gottscj / Hangfire.Mongo

Mongo DB support for Hangfire
MIT License
259 stars 84 forks source link

Could not complete migration, Never acquired lock within allowed time: when trying to connect mongodb on host only #389

Closed priyanka945 closed 4 months ago

priyanka945 commented 5 months ago

Hi,

I am trying to integrate Hangfire to mongodb in my .net 6 web api application. The mongo db has some application related collections and after integrating with hangfire library , running this locally pointing to the database: it created below hang_fire collection in the db specified. And on the localhost the app is up and running.

image

Package referenced: "Hangfire" Version="1.8.10" "Hangfire.Mongo" Version="1.10.3" "MongoDB.Driver" Version="2.24.0" "Hangfire.Console" Version="1.4.3" "Hangfire.Dashboard.Basic.Authentication" Version="5.0.0"

But when the app is deployed to Host(PCF), it started returning error as below:

Code:

public static IServiceCollection ConfigureHangfireService(this IServiceCollection serviceCollection, IConfiguration configuration)
  {
      var mongoUrlBuilder = new MongoUrlBuilder("mongodb://<mongo_user_name>:<password>@<host>:<port>/?replicaSet=<replica set name>&retryWrites=true&w=majority");
      {
          DatabaseName = "dbName",
          AuthenticationMechanism = "SCRAM-SHA-256",
          AuthenticationSource = "admin"
      };
      var uri = mongoUrlBuilder.ToMongoUrl();
      var settings = MongoClientSettings.FromUrl(uri);
      settings.ServerApi = new ServerApi(ServerApiVersion.V1);

      var mongoClient = new MongoClient(settings);

      var storageOptions = new MongoStorageOptions
      {
          MigrationOptions = new MongoMigrationOptions
          {
              MigrationStrategy = new DropMongoMigrationStrategy(),
              BackupStrategy = new NoneMongoBackupStrategy()
          },
          Prefix = "Hang_fire",
          CheckConnection = true,
          MigrationLockTimeout = TimeSpan.FromMinutes(2),
          CheckQueuedJobsStrategy = CheckQueuedJobsStrategy.Poll
      };

      serviceCollection.AddHangfire(configuration => configuration
         .SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
         .UseMongoStorage(mongoClient, mongoUrlBuilder.DatabaseName, storageOptions)
         .UseConsole());

      return serviceCollection;

  }

Program.cs: builder.Services.ConfigureHangfireService(configuration);

Note : When the app is getting deployed to host (PCF) 'Hang_fire.migrationLock' collection already exist in the db it is pointing to. Error Log: image

Approach2: As suggested above manually dropped collection: 'Hang_fire.migrationLock' in db, kept rest of the Hang_fire collections and started app again in host still the same error.

Approach3: Dropped all the hangfire related collections and gave the new Prefix "Hangfire_mongo" in MongoStorageOptions, started the app on host. Still the same error, but now it has only created 'Hangfire_mongo.migrationLock' collection and rest of the hangfire related collections are not created and the app fails to start with following error:
image Is it looking for deleted 'Hang_fire.migrationLock' collection?

Note1 : Increased MigrationLockTimeout till 5minutes, still the same error. And connection from Host to Mongo db seems fine as its able to create collection Hangfire_mongo.migrationLock and issue occurrs only when app starts in Host.

Any pointer on how to resolve this isssue would be helpful.

gottscj commented 5 months ago

@priyanka945, looks strange.

try to drop the hangfire collections.

since you are using settings.ServerApi = new ServerApi(ServerApiVersion.V1);

you should set option SupportsCappedCollection = false

using the stable API needs this option as it else will try to create a capped collection

I hope this helps

gottscj commented 5 months ago

@priyanka945,

Did you have any progress on this?

Thanks

gottscj commented 4 months ago

closing due to inactivity