gpt4 book ai didi

c# - 有没有办法强制Hangfire每次都重新执行代码?

转载 作者:太空宇宙 更新时间:2023-11-03 12:06:40 25 4
gpt4 key购买 nike

我正在尝试评估 Hangfire 作为我最新项目的潜在任务调度解决方案。

在处理重复性任务时,我注意到当我尝试在控制台写入当前时间时,每次都会将相同的时间写入控制台。

有人可以告诉我如何强制 hangfire 在每次运行时重新执行任务吗? Google 没有帮助,可能是因为我找不到合适的词来搜索。

这样“更新系统信息”任务就没用了。

我的代码:

using System;
using System.Globalization;
using Hangfire;
using Microsoft.Owin.Hosting;

namespace HangfireTests
{
public class HangfireTests
{
public HangfireTests()
{
}

public void Start()
{
WebApp.Start<Startup>("http://localhost:8888");
Console.WriteLine("Server started... press any key to shut down");

RecurringJob.AddOrUpdate("systeminfo",
() => Console.WriteLine($"updating system information {GetTime()}"), Cron.Minutely);
RecurringJob.AddOrUpdate("checktask",
() => Console.WriteLine($"checking tasks {GetTime()}"), Cron.Minutely);
}

public static string GetTime()
{
return DateTime.Now.ToString(CultureInfo.InvariantCulture);
}

public void Stop()
{
}
}
}

这是我的示例的控制台输出: hangfire console output

最佳答案

如果您将 GetTime 函数而不是 WriteLine 加入队列,它将在执行时进行评估。这将为您提供执行作业的时间。

using System;
using System.Globalization;
using Hangfire;
using Microsoft.Owin.Hosting;

namespace HangfireTests
{
public class HangfireTests
{
public HangfireTests()
{
}

public void Start()
{
WebApp.Start<Startup>("http://localhost:8888");
Console.WriteLine("Server started... press any key to shut down");

RecurringJob.AddOrUpdate("systeminfo",
() => GetTime(), Cron.Minutely);
RecurringJob.AddOrUpdate("checktask",
() => GetTime(), Cron.Minutely);
}

public static string GetTime()
{
var now = DateTime.Now.ToString(CultureInfo.InvariantCulture);
Console.WriteLine(now);
return now;
}

public void Stop()
{
}
}
}

关于c# - 有没有办法强制Hangfire每次都重新执行代码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54520813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com