HangfireIO / HangfireIO/Hangfire
Handle Exceptions from inside Hangfire
- Dominant language
- C#
- Stars
- 10.1k
- Forks
- 1.8k
- Avg merge
- 1h 19m
- Merged PRs (30d)
- 1
Description
I have a scenario, I am scheduling a BackgroundJob to run after some random seconds on application start. After the job is performed then I am again scheduling the same BackgroundJob to run after some random seconds and it goes on and never stops.
What I want is if any exception occurs in Hangfire during this continuous process I want to shut-down my asp.net core app.
for example, while this process is happening I stop the SQL Server. So hangfire giving this exception that is obvious
> System.Data.SqlClient.SqlException: A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)
>
> System.ComponentModel.Win32Exception: The system cannot find the file specified
> --- End of inner exception stack trace ---
> at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection) at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource1 retry, DbConnectionOptions userOptions) at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource1 retry) at System.Data.SqlClient.SqlConnection.Open() at Hangfire.SqlServer.SqlServerStorage.CreateAndOpenConnection() at Hangfire.SqlServer.SqlServerStorage.UseConnection[T](Func`2 func) at Hangfire.SqlServer.SqlServerConnection.RemoveTimedOutServers(TimeSpan timeOut) at Hangfire.Server.ServerWatchdog.Execute(BackgroundProcessContext context) at Hangfire.Server.AutomaticRetryProcess.Execute(BackgroundProcessContext context)
So I don't know where to handle this exception, so that I can stop my application if this exception occurs.
Contributor guide
Research direction
No repository file or test is named. Start by tracing the Hangfire.Server.ServerWatchdog and AutomaticRetryProcess entry points shown in the stack trace, then inspect how SQL Server connection exceptions are surfaced to the ASP.NET Core application. Done would require a clearly defined and tested application-shutdown behavior, which the issue does not specify.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- backend
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 20/100