Skip to content

KeyNotFoundException when retrieving recurring jobs #2412

@redstarty

Description

@redstarty

Hey there!

We use Hangfire with SQL server and the following code to check if the job exists:

var job = JobStorage.Current.GetConnection().GetRecurringJobs(new[] { id }).FirstOrDefault();

It seems to be randomly failing with

System.Collections.Generic.KeyNotFoundException: The given key 'Cron' was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Hangfire.Storage.StorageConnectionExtensions.GetRecurringJobDtos(IStorageConnection connection, IEnumerable`1 ids) in C:\projects\hangfire-525\src\Hangfire.Core\Storage\StorageConnectionExtensions.cs:line 103

In the StorageConnectionExtensions I see the following lines:

var hash = connection.GetAllEntriesFromHash($"recurring-job:{id}");

// TODO: Remove this in 2.0 (breaking change)
if (hash == null)
{
    result.Add(new RecurringJobDto { Id = id, Removed = true });
    continue;
}

var dto = new RecurringJobDto
{
    Id = id,
    Cron = hash["Cron"]
};

So the question is how could it be that for the existing job (since hash is not null, it exists, right?) Cron is not saved?

Job are created using IRecurringJobManager's AddOrUpdateDynamic method where cron is always provided. Then the job could be deleted using RemoveIfExists method.

All comments and ideas are appreciated!

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions