Hangfire如何检索和显示所有存储的作业?

huangapple go评论61阅读模式
英文:

Hangfire how to retrieve and display all stored jobs?

问题

以下是翻译好的部分:

[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);
}

如果您需要进一步的帮助,请告诉我。

英文:

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

This was one of my first tries:

[HttpGet(&quot;jobs&quot;)]
[ProducesResponseType(StatusCodes.Status200OK)]
public IActionResult GetJobs()
{
	var monitoringApi = JobStorage.Current.GetMonitoringApi();
	var jobDetails = monitoringApi.EnqueuedJobs();
	var response = new List&lt;object&gt;();

	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);
}

答案1

得分: 1

选项 #1 - 使用 MonitoringApi

您已经尝试过这个方法,但您必须使用此链接接口逐个获取每个作业列表(已调度、处理中、成功等)。

不幸的是,没有简单的方法来获取所有内容,因为 Hangfire 将每个作业状态定义为不同的类,没有共同的基类(例如,EnqueuedJobDtoScheduledJobDto 没有共同的基类)。

选项 #2 - 直接从您的数据库中获取信息

相当明了,但这可能取决于您在 Hangfire 中使用的存储方式。

英文:

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.

huangapple
  • 本文由 发表于 2023年4月4日 17:54:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75927979.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定