【问题标题】:Hangfire is not executing job in manner which it is called?Hangfire 没有以它被称为的方式执行作业?
【发布时间】:2020-09-23 07:00:16
【问题描述】:

这是我的示例代码,我正在尝试使用以下软件包制作控制台应用程序

using Hangfire;
using Hangfire.MemoryStorage;
using System; 

代码如下:

class Program
    {
        static void Main(string[] args)
        {
            GlobalConfiguration.Configuration.UseMemoryStorage();

            BackgroundJob.Enqueue(() => ExecuteThis(1));
            
            BackgroundJob.Enqueue(() => ExecuteThis(2));
            
            BackgroundJob.Enqueue(() => ExecuteThis(3));
            
            BackgroundJob.Enqueue(() => ExecuteThis(4));

            using (var server = new BackgroundJobServer())
            {
                Console.ReadLine();
            }
        }

        public static void ExecuteThis(int number)
        {
            Console.WriteLine("Start " + number);
            Console.WriteLine("End " + number);
        }
    } 

预期结果:

Start 1
End 1
Start 2
End 2
Start 3
End 3
Start 4
End 4

但是随机开始和结束工作,我只想在前一个工作完成后执行下一个工作。 有人可以帮忙吗?

【问题讨论】:

    标签: c# asp.net hangfire


    【解决方案1】:

    你可以使用 Hangfire 的Continuations

    Continuations 允许您通过链接来定义复杂的工作流程 多个后台作业一起。

    var id = BackgroundJob.Enqueue(() => Console.WriteLine("Hello, "));
    BackgroundJob.ContinueWith(id, () => Console.WriteLine("world!")); 
    

    在你的例子中:

    static void Main(string[] args)
        {
            GlobalConfiguration.Configuration.UseMemoryStorage();
    
            var id1 = BackgroundJob.Enqueue(() => ExecuteThis(1));
            
            var id2 = BackgroundJob.ContinueWith(id1, () => ExecuteThis(2));
            
            var id3 = BackgroundJob.ContinueWith(id2, () => ExecuteThis(3));
            
            BackgroundJob.ContinueWith(id3, () => ExecuteThis(4));
    
            using (var server = new BackgroundJobServer())
            {
                Console.ReadLine();
            }
        }
    

    还有 批次,很适合您的情况,但在 Pro 包中。

    【讨论】:

    • 是的,如果我们有以前的作业 ID,这可以工作,但在我的情况下,可以从应用程序的任何位置调用此作业,即使同时 2 个用户可以执行此操作,所以我想确保无论用户首先调用该方法,都应该首先更新数据。所以简单来说,就是希望用户调用队列,队列会按照调用它的方式在后台自动管理实际执行,而不是随机的。
    • 更新后的示例在​​您的情况下是否有效?随机调用是什么意思?你不知道哪个执行是管道中的第一个执行吗?如果是这样的话,也许 Hang-fire Free 不是你需要的,至少不是它本身。
    • @AkashThakkar,HangFire 通常用于需要定期执行“某事”而无需用户干预的情况。查看您的评论,您真正需要的是“队列”实现。
    • 当我知道首先在管道中执行哪个执行时有效。但就我而言,多个用户可以同时处理同一个文档。因此,无论哪个被调用先保存,都需要先保存,然后才进入队列并获取下一个执行。如果在这种情况下不是挂火,那么您知道可以使用哪个队列系统吗?或任何包/库?
    • @Thangadurai 是的,我正在寻找一个以排队方式执行我的工作的队列。因此,如果可行,请尝试使用 Hangfire BackgroundJob.Enqueue。它正在工作,但执行是随机的,而不是以我排队的方式。
    【解决方案2】:

    你试过DisableConcurrentExecution属性吗?

        [DisableConcurrentExecution(60)]
        public static void ExecuteThis(int number)
        {
            Console.WriteLine("Start " + number);
            Console.WriteLine("End " + number);
        }
    

    【讨论】:

    • 是的,但没有帮助。
    • @AkashThakkar 可能与 MemoryStorage 有关,您是否尝试过使用数据库?
    • 是的,结果相同。实际上它不依赖于内存或数据库。
    猜你喜欢
    • 1970-01-01
    • 2019-01-27
    • 1970-01-01
    • 2021-01-11
    • 2021-12-18
    • 2014-12-06
    • 2015-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多