farhad
farhad

Reputation: 85

What is the minimum time for hangfire jobs

I create an Asp.Net Core 3.1 project with hangfire. I configured hangfire to run every second but it's run every 15 second or more. This is my ConfigureService

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

Configure Method

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;
    }
}

Result

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

How can i run job every seconds?

Upvotes: 3

Views: 2209

Answers (2)

Change the default SchedulePollingInterval to 1 seconds

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

Upvotes: 5

Yiyi You
Yiyi You

Reputation: 18159

The default schedule polling interval is set to 15s.Maybe you can refer to this,and you can try what WtFudgE says.

Upvotes: 0

Related Questions