gpt4 book ai didi

c# - hangfire作业的最短时间是多少

转载 作者:行者123 更新时间:2023-12-04 13:12:21 24 4
gpt4 key购买 nike

我使用 hangfire 创建了一个 Asp.Net Core 3.1 项目。我将 hangfire 配置为每秒运行一次,但它每 15 秒或更长时间运行一次。这是我的ConfigureService

public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
services.AddScoped<IShowDate, ShowDate>();
services.AddHangfire(x =>
{
x.UseMemoryStorage();
});
services.AddHangfireServer();
}

配置方法

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}

app.UseHttpsRedirection();

RecurringJob.AddOrUpdate<IShowDate>(job => job.Print(), "*/1 * * * * *");

app.UseRouting();

app.UseAuthorization();
app.UseHangfireDashboard();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapHangfireDashboard();
});
}

ShowDate

public interface IShowDate
{
Task Print();
}
public class ShowDate : IShowDate
{
public async Task Print()
{
Console.WriteLine($"Example {DateTime.Now}");
await Task.CompletedTask;
}
}

结果

Example 9/8/2020 10:46:14 AM
Example 9/8/2020 10:46:29 AM
Example 9/8/2020 10:46:44 AM
Example 9/8/2020 10:46:59 AM
Example 9/8/2020 10:47:14 AM
Example 9/8/2020 10:47:29 AM
Example 9/8/2020 10:47:44 AM
Example 9/8/2020 10:47:59 AM
Example 9/8/2020 10:48:14 AM
Example 9/8/2020 10:48:29 AM
Example 9/8/2020 10:48:44 AM
Example 9/8/2020 10:48:59 AM
Example 9/8/2020 10:49:14 AM
Example 9/8/2020 10:49:29 AM
Example 9/8/2020 10:49:44 AM
Example 9/8/2020 10:49:59 AM

如何每秒钟运行一次作业?

最佳答案

将默认的 SchedulePollingInterval 更改为 1 秒

services.AddHangfireServer(option =>
{
option.SchedulePollingInterval = TimeSpan.FromSeconds(1);
});

关于c# - hangfire作业的最短时间是多少,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63788069/

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