Skip to content

Conversation

@nicolaj-hartmann
Copy link

Problem

When a job has multiple entries in the jobqueue table (e.g., due to re-queuing or race conditions), the Hangfire dashboard crashes with:

System.ArgumentException: An item with the same key has already been added. Key: 185

This occurs in PostgreSqlMonitoringApi.FetchedJobs because the SQL query returns duplicate job IDs which are then passed to ToDictionary().

Solution

  • Add DISTINCT to GetQueuedOrFetchedJobIds to ensure unique job IDs are returned
  • Add DISTINCT ON (j."id") to FetchedJobs and EnqueuedJobs SQL queries as a safeguard

Testing

Added integration test FetchedJobs_WithDuplicateJobQueueEntries_DoesNotThrow that:

  1. Creates a job with two jobqueue entries (same jobId)
  2. Calls FetchedJobs via the monitoring API
  3. Verifies no exception is thrown and only one job is returned

…tries

Add DISTINCT/DISTINCT ON to SQL queries in monitoring API to handle
cases where a job has multiple entries in the jobqueue table. This
prevents ArgumentException when the dashboard displays fetched jobs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant