imranmomin / Hangfire.AzureCosmosDb

Azure Cosmos DB storage provider for Hangfire
https://www.hangfire.io/
MIT License
17 stars 16 forks source link

Exception in triggering RecurringJob when CosmosClientOptions.EnableContentResponseOnWrite = false #46

Closed Last8Exile closed 2 years ago

Last8Exile commented 2 years ago

I updated from 1.3 to 2.0.1 and trying to use new CosmosDbStorage constructor with shared CosmosClient. But i got exception when Hangfire triggering recurring job scheduled with: RecurringJob.AddOrUpdate("Test", ()=>Console.WriteLine("Test"), Cron.Minutely());

System.NullReferenceException: Object reference not set to an instance of an object.
   at Hangfire.Azure.CosmosDbConnection.CreateExpiredJob(Job job, IDictionary`2 parameters, DateTime createdAt, TimeSpan expireIn)
   at Hangfire.Client.CoreBackgroundJobFactory.<>c__DisplayClass14_0.<Create>b__2(Int32 _)
   at Hangfire.Client.CoreBackgroundJobFactory.RetryOnException[T](Int32& attemptsLeft, Func`2 action)
--- End of stack trace from previous location ---
   at Hangfire.Client.CoreBackgroundJobFactory.RetryOnException[T](Int32& attemptsLeft, Func`2 action)
   at Hangfire.Client.CoreBackgroundJobFactory.Create(CreateContext context)
   at Hangfire.Client.BackgroundJobFactory.<>c__DisplayClass12_0.<CreateWithFilters>b__0()
   at Hangfire.Client.BackgroundJobFactory.InvokeClientFilter(IClientFilter filter, CreatingContext preContext, Func`1 continuation)
   at Hangfire.Client.BackgroundJobFactory.<>c__DisplayClass12_1.<CreateWithFilters>b__2()
   at Hangfire.Client.BackgroundJobFactory.CreateWithFilters(CreateContext context, IEnumerable`1 filters)
   at Hangfire.Client.BackgroundJobFactory.Create(CreateContext context)
   at Hangfire.RecurringJobExtensions.TriggerRecurringJob(IBackgroundJobFactory factory, JobStorage storage, IStorageConnection connection, IProfiler profiler, RecurringJobEntity recurringJob, DateTime now)
   at Hangfire.Server.RecurringJobScheduler.ScheduleRecurringJob(BackgroundProcessContext context, IStorageConnection connection, String recurringJobId, RecurringJobEntity recurringJob, DateTime now)

After investigation i figured out that CosmosClientOptions.EnableContentResponseOnWrite must be enabled to work properly. I disable that setting to save bandwidth, because i don't have triggers and post process in database.

I think this should be mentioned in docs and CosmosDbStorage constructor should throw exception with proper message if this setting not enabled (until support for both cases are implemented).

imranmomin commented 2 years ago

@Last8Exile - Thank you. Sure will soon update the document. Until then this issue can be referred if someone else faces similar issue.

Last8Exile commented 2 years ago

@imranmomin It is possible to manually set ItemRequestOptions.EnableContentResponseOnWrite = true on each request that needs it.

I try to find time to make pull request with forced content responses.