簡體   English   中英

Hangfire .NET Core - 獲取排隊的作業列表

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

Hangfire API 中是否有一種方法可以獲取排隊的作業(可能通過作業 ID 或其他方式)?

我對此進行了一些研究,但找不到任何東西。

請幫我。

我在Hangfire的官方論壇上找到了答案。

這是鏈接: https : //discuss.hangfire.io/t/checking-for-a-job-state/57/4

根據 Hangfire 的官方開發人員的說法, JobStorage.Current.GetMonitoringApi()為您提供了有關作業、隊列和配置的服務器的所有詳細信息!

Hangfire 儀表板似乎正在使用相同的 API。

:-)

我遇到了一個案例,我想查看特定隊列的 ProcessingJobs、EnqueuedJobs 和 AwaitingState 作業。 我從來沒有找到一種開箱即用的好方法,但我確實發現了一種在 Hangfire 中創建“一組”作業的方法。 我的解決方案是將每個作業添加到一個集合中,然后查詢匹配集合中的所有項目。 當作業達到最終狀態時,從集合中刪除該作業。

這是創建集合的屬性:

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

你這樣裝飾你的工作:

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

然后您可以按如下方式查詢該集合:

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