简体   繁体   English

Hangfire .NET Core - 获取排队的作业列表

[英]Hangfire .NET Core - Get enqueued jobs list

Is there a method in the Hangfire API to get an enqueued job (probably by a Job id or something)? Hangfire API 中是否有一种方法可以获取排队的作业(可能通过作业 ID 或其他方式)?

I have done some research on this, but I could not find anything.我对此进行了一些研究,但找不到任何东西。

Please help me.请帮我。

I have found the answer in the official forum of Hangfire.我在Hangfire的官方论坛上找到了答案。

Here is the link: https://discuss.hangfire.io/t/checking-for-a-job-state/57/4这是链接: https : //discuss.hangfire.io/t/checking-for-a-job-state/57/4

According to an official developer of Hangfire, JobStorage.Current.GetMonitoringApi() gives you all the details regarding Jobs, Queues and the configured servers too!根据 Hangfire 的官方开发人员的说法, JobStorage.Current.GetMonitoringApi()为您提供了有关作业、队列和配置的服务器的所有详细信息!

It seems that this same API is being used by the Hangfire Dashboard. Hangfire 仪表板似乎正在使用相同的 API。

:-) :-)

I ran into a case where I wanted to see ProcessingJobs, EnqueuedJobs, and AwaitingState jobs for a particular queue.我遇到了一个案例,我想查看特定队列的 ProcessingJobs、EnqueuedJobs 和 AwaitingState 作业。 I never found a great way to do this out of the box, but I did discover a way to create a "set" of jobs in Hangfire.我从来没有找到一种开箱即用的好方法,但我确实发现了一种在 Hangfire 中创建“一组”作业的方法。 My solution was to add each job to a set, then later query for all items in the matching set.我的解决方案是将每个作业添加到一个集合中,然后查询匹配集合中的所有项目。 When the job reaches a final state, remove the job from the set.当作业达到最终状态时,从集合中删除该作业。

Here's the attribute to create the set:这是创建集合的属性:

public class ProcessQueueAttribute : JobFilterAttribute, IApplyStateFilter
{
    private readonly string _queueName;

    public ProcessQueueAttribute()
        : base() { }

    public ProcessQueueAttribute(string queueName) 
        : this()
    {
        _queueName = queueName;
    }

    public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
    {
        if (string.IsNullOrEmpty(context.OldStateName))
        {
            transaction.AddToSet(_queueName, context.BackgroundJob.Id);
        }
        else if (context.NewState.IsFinal)
        {
            transaction.RemoveFromSet(_queueName, context.BackgroundJob.Id);
        }
    }

    public void OnStateUnapplied(ApplyStateContext context, IWriteOnlyTransaction transaction) { }
}

You decorate your job this way:你这样装饰你的工作:

[ProcessQueue("queueName")]
public async Task DoSomething() {}

Then you can query that set as follows:然后您可以按如下方式查询该集合:

using (var conn = JobStorage.Current.GetConnection())
{
    var storage = (JobStorageConnection)conn;
    if (storage != null)
    {
        var itemsInSet = storage.GetAllItemsFromSet("queueName");
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM