HangfireIO / Hangfire

An easy way to perform background job processing in .NET and .NET Core applications. No Windows Service or separate process required
https://www.hangfire.io
Other
9.41k stars 1.7k forks source link

Hangfire Pro Recurring Jobs issues #2297

Closed knopa closed 1 year ago

knopa commented 1 year ago

I am using that version of hangfire packages while migration from sql to redis I got 2 issue .net core 7 Hangfire.Core 1.8.5 Hangfire.SqlServer 1.8.5 Hangfire.Pro 2.3.2 Hangfire.Pro.Redis 3.0.2 redis:latest

1. When Recurring job trying to run by schedule [16:05:09 WRN] Recurring job 'UserActivityJob' can't be scheduled due to an error and will be retried in 00:00:15.

System.ArgumentException: A null value is not valid in this context
   at StackExchange.Redis.RedisValue.AssertNotNull()
   at StackExchange.Redis.Message.CommandKeyValuesMessage..ctor(Int32 db, CommandFlags flags, RedisCommand command, RedisKey key, RedisValue[] values)
   at StackExchange.Redis.Message.Create(Int32 db, CommandFlags flags, RedisCommand command, RedisKey key, RedisValue[] values)
   at StackExchange.Redis.RedisDatabase.GetHashSetMessage(RedisKey key, HashEntry[] hashFields, CommandFlags flags)
   at StackExchange.Redis.RedisDatabase.HashSetAsync(RedisKey key, HashEntry[] hashFields, CommandFlags flags)
   at StackExchange.Redis.KeyspaceIsolation.WrapperBase`1.HashSetAsync(RedisKey key, HashEntry[] hashFields, CommandFlags flags)
   at Hangfire.Pro.Redis.RedisConnection.CreateExpiredJob(Job job, IDictionary`2 parameters, DateTime createdAt, TimeSpan expireIn)
   at Hangfire.Client.CoreBackgroundJobFactory.<>c__DisplayClass15_0.<CreateBackgroundJobTwoSteps>b__0(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.CreateBackgroundJobTwoSteps(CreateContext context, Dictionary`2 parameters, DateTime createdAt, TimeSpan expireIn)
   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.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 TMS.Infrastructure.Cron.ApplicationInsightsBackgroundJobFactory.Create(CreateContext context) in ApplicationInsightsBackgroundJobFactory.cs:line 22
   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)

2. Restart Server issue RecurringJob.AddOrUpdate(jobName, methodCall, jobSettings.Schedule, new RecurringJobOptions() { TimeZone = timezoneInfo ?? TimeZoneInfo.Utc }); When any recurring job was in execution - after restart server - it failes with next exception

Hangfire.Storage.DistributedLockTimeoutException: 'Timeout expired. The timeout elapsed prior to obtaining a distributed lock on the 'lock:recurring-job:UserActivityJob' resource.'

knopa commented 1 year ago

I think i had figure out first one

When you have own IClientFilter and you set inside job parameter in my case it's operation id from activity and for recurring jobs there is no activity - it failed on redis update for schedule jobs

so check if value not null before set helps filterContext.SetJobParameter

As for seconds still no clue how it happens, because it not 100% reproducible

odinserj commented 1 year ago

Fixed with Hangfire.Pro.Redis 3.0.3.