codeyu / Hangfire.LiteDB

LiteDB storage for Hangfire.
MIT License
62 stars 31 forks source link

Hangfire.LiteDB get locked for concurrent access #23

Open vpatil007 opened 4 years ago

vpatil007 commented 4 years ago

We are having problems with accessing the LiteDB database with Hangfire implementation. We are using Hangfire.LiteDB (0.3.0) and looks like for concurrent multi process environment database is getting locked, which results in crashing the Hangfire jobs. The size of the database is really small (3MB).

We are simply creating the connection and attaching it to the hangfireDB. Following is the connection code,

`public void ConfigureServices(IServiceCollection service) { ApiStartupExtensions.ConfigureServices(Configuration, service, _currentHostingEnvironment);

        ApplicationContext context = new ApplicationContext()
        {
            ApplicationConfiguration = Configuration,
        };
        //Register RetryPolicies as Singelton
        service.AddSingleton<RetryPolicies>();
        service.AddHangfire(t => t.UseLiteDbStorage(Configuration.GetConnectionString("HangfireConnection")));            
    }`

Could you share an example of how to implement concurrency with Hangfire jobs using HangFire.LiteDB or You can suggest a necessary changes required?

felixclase commented 4 years ago

Try 0.3.1 version...

hxbb00 commented 3 years ago

Execution RecurringJobScheduler is in the Failed state now due to an exception, execution will be retried no more than in 00:00:04 Hangfire.Storage.DistributedLockTimeoutException: Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'Could not place a lock on the resource 'HangFire:recurring-jobs:lock': The lock request timed out.' resource. at Hangfire.LiteDB.LiteDbDistributedLock.Acquire(TimeSpan timeout) at Hangfire.LiteDB.LiteDbDistributedLock..ctor(String resource, TimeSpan timeout, HangfireDbContext database, LiteDbStorageOptions storageOptions) at Hangfire.LiteDB.LiteDbConnection.AcquireDistributedLock(String resource, TimeSpan timeout) at Hangfire.Server.RecurringJobScheduler.UseConnectionDistributedLock[T](JobStorage storage, Func2 action) at Hangfire.Server.RecurringJobScheduler.EnqueueNextRecurringJobs(BackgroundProcessContext context) at Hangfire.Server.RecurringJobScheduler.Execute(BackgroundProcessContext context) at Hangfire.Server.BackgroundProcessDispatcherBuilder.ExecuteProcess(Guid executionId, Object state) at Hangfire.Processing.BackgroundExecution.Run(Action2 callback, Object state)

i meet this exception too

majora2007 commented 3 years ago

I get this all the time, I was never able to solve (I'm on 0.4.0 with .NET 5) and switched to MemoryStorage

mrsquish commented 6 months ago

Yep, me too. Brand new app .Net8, still in development. Couldn't work out why the scheduled jobs wouldn't trigger. Then got the same exception as above.

Hangfire.Processing.BackgroundExecution: Error: Execution RecurringJobScheduler is in the Failed state now due to an exception, execution will be retried no more than in 00:00:04

Hangfire.Storage.DistributedLockTimeoutException: Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'Could not place a lock on the resource 'HangFire:recurring-jobs:lock': The lock request timed out.' resource.
   at Hangfire.LiteDB.LiteDbDistributedLock.Acquire(TimeSpan timeout)
   at Hangfire.LiteDB.LiteDbDistributedLock..ctor(String resource, TimeSpan timeout, HangfireDbContext database, LiteDbStorageOptions storageOptions)
   at Hangfire.LiteDB.LiteDbConnection.AcquireDistributedLock(String resource, TimeSpan timeout)
   at Hangfire.Server.RecurringJobScheduler.UseConnectionDistributedLock[T](JobStorage storage, Func`2 action) in C:\projects\hangfire-525\src\Hangfire.Core\Server\RecurringJobScheduler.cs:line 389
   at Hangfire.Server.RecurringJobScheduler.EnqueueNextRecurringJobs(BackgroundProcessContext context) in C:\projects\hangfire-525\src\Hangfire.Core\Server\RecurringJobScheduler.cs:line 186
   at Hangfire.Server.RecurringJobScheduler.Execute(BackgroundProcessContext context) in C:\projects\hangfire-525\src\Hangfire.Core\Server\RecurringJobScheduler.cs:line 159
   at Hangfire.Server.BackgroundProcessDispatcherBuilder.ExecuteProcess(Guid executionId, Object state) in C:\projects\hangfire-525\src\Hangfire.Core\Server\BackgroundProcessDispatcherBuilder.cs:line 82
   at Hangfire.Processing.BackgroundExecution.Run(Action`2 callback, Object state) in C:\projects\hangfire-525\src\Hangfire.Core\Processing\BackgroundExecution.cs:line 118

It would seem that LiteDB is just not fit for purpose here.