0

How to retrieve and display all stored jobs with on hangfire without UI?

This was one of my first tries:

[HttpGet("jobs")]
[ProducesResponseType(StatusCodes.Status200OK)]
public IActionResult GetJobs()
{
    var monitoringApi = JobStorage.Current.GetMonitoringApi();
    var jobDetails = monitoringApi.EnqueuedJobs();
    var response = new List<object>();

    foreach (var job in jobDetails)
    {
        response.Add(new
        {
            JobId = job.Key,
            JobName = job.Value.Job.Type.FullName,
            EnqueuedAt = job.Value.EnqueuedAt,
            State = job.Value.State
        });
    }

    return Ok(response);
}
Florian
  • 1,019
  • 6
  • 22
MarcGr
  • 1
  • Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. – Community Apr 04 '23 at 13:24

1 Answers1

1

Option #1 - Use the MonitoringApi

You've already tried this, but you'll have to get each job list individually (scheduled, processing, succeeded, etc.) by using this interface.

Unfortunately, there isn't a simple way of making some generic method that retrieves everything, since Hangfire defines each job state as a different class with no common base (e.g. EnqueuedJobDto and ScheduledJobDto share no base class).

Option #2 - Get the information straight from your database

Pretty self explanatory, but it might depend on the storage you're using for Hangfire.

Xzenon
  • 1,193
  • 2
  • 15
  • 37