hangfire作业的最短时间是多少

What is the minimum time for hangfire jobs

我用 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();
}

Configure 方法

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 Class

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

我怎样才能 运行 每秒钟工作一次?

默认的调度轮询间隔设置为15s.Maybe你可以参考,你可以试试WtFudgE说的

将默认 SchedulePollingInterval 更改为 1 秒

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