Gaurav
Gaurav

Reputation: 162

How to increase hangfire job timeout

I have a .net core application which has hangfire 1.7.2 running.

So I have this job, which executes SQL stored procedure and its a long running task, can be of 30 minutes.

This gives me following error: Timeout expired. The timeout period elapsed prior to completion of the operation or the server is not responding. The statement has been terminated.

services.AddHangfire(configuration => 

configuration.UseSqlServerStorage(Configuration.GetConnectionString("HangfireConnection"), 
new SqlServerStorageOptions
    {
        SlidingInvisibilityTimeout = TimeSpan.FromMinutes(30),
        QueuePollInterval = TimeSpan.Zero,
        UsePageLocksOnDequeue = false,
        DisableGlobalLocks = false
    }));

Please help me out.

Upvotes: 2

Views: 14720

Answers (2)

Gaurav
Gaurav

Reputation: 162

I managed to fix this by increasing SQLCommand timeout which is by default 30 seconds.

 using (SqlCommand cmd = new SqlCommand(query, con))
 {
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandTimeout = 3600; // 1 hour
        await cmd.ExecuteNonQueryAsync();
 }

Upvotes: 5

Mattkwish
Mattkwish

Reputation: 770

The CommandTimeout property on SqlServerStorageOptions should be what you are looking for.

Increase this to be more than 30 minutes and your jobs will stop timing out.

(See source https://github.com/HangfireIO/Hangfire/blob/cf7bb08d24ee4953926b7717461bf8a23d895eb4/src/Hangfire.SqlServer/SqlServerConnection.cs)

Upvotes: 4

Related Questions