HangfireIO / HangfireIO/Hangfire
IMonitoringApi is returning results in reversing order
- Dominant language
- C#
- Stars
- 10.1k
- Forks
- 1.8k
- Avg merge
- 1h 19m
- Merged PRs (30d)
- 1
Description
Hi there.
My team is currently implementing metrics related to Hangfire so we can configure alarmistic based on certain types of events. Currently, we're fetching the failed jobs so we can fire alarms when a job from X type fails. Eg.:
`scheduler_job_count{type="hangfire",state="failed",job="IProcessNumber.Process"} 181`
Our Hangfire implementation uses MongoDB (outside of this repository, I know), however, I believe that there is a problem in the MongoDB implementation of the `IMonitoringApi` that originated here.
The `IMonitoringApi` defines, among others, the following methods:
```csharp
JobList ProcessingJobs(int from, int count);
JobList ScheduledJobs(int from, int count);
JobList SucceededJobs(int from, int count);
JobList FailedJobs(int from, int count);
JobList DeletedJobs(int from, int count);
```
When looking at these methods, one can think that the results are paginated from the oldest job to the new ones.
As an example, if I have 10000 jobs that failed and 5 new jobs failed after the last time I accessed the information I would think that to access the new jobs, I would call the FailedJobs with the `from = 10000` and the `count = 5`.
However, looking at the Hangfire.MongoDB implementation (I know, still not about this repo, we will get there) the query that is made to the database is inversing the order of the array before fetching the results, which means that the method invocation I exemplified above will give me not the most recent 5 failed jobs but the 5 oldest.
https://github.com/Hangfire-Mongo/Hangfire.Mongo/blob/master/src/Hangfire.Mongo/MongoMonitoringApi.cs#L563
I thought that it was an error in the Mongo implementation but then we went to check other databases implementations and realized that this is the behavior in some of them.
**MemoryStorage**
https://github.com/perrich/Hangfire.MemoryStorage/blob/master/src/Hangfire.MemoryStorage/MemoryStorageMonitoringApi.cs#L367
**SQLServer**
https://github.com/HangfireIO/Hangfire/blob/master/src/Hangfire.SqlServer/SqlServerMonitoringApi.cs#L176
So, the question is:
Is this the desired behavior for the `IMonitoringApi` implementations? Or is it an error that was made in the original implementation that was replicated in the other databases like MongoDB?
Honestly, for me, it looks a bit strange that to get the last _X_ jobs from a certain state I need to call the methods with the `from` argument as _0_ all the time and the `count` with the _X_ value.
Cheers
Contributor guide
Research direction
Start with the IMonitoringApi method signatures in the issue, then compare the ordering and pagination logic in MongoMonitoringApi.cs, MemoryStorageMonitoringApi.cs, and SqlServerMonitoringApi.cs at the linked locations. Determine the intended meaning of from and count across implementations; done means the behavior is decided and the affected implementations and documentation or tests consistently reflect it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 35/100