HangfireIO / HangfireIO/Hangfire

[Question] Hangfire server shutdown is slow, how to debug which jobs are not cancelling?

Open
#2,328 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C#
Stars
10.1k
Forks
1.8k
Avg merge
1h 19m
Merged PRs (30d)
1

Description

Hiya,

I've noticed that sometimes in production the background service that runs Hangfire is slow to exit.
I'd like to find out:
- What jobs are running when the application receives a stop request
- What jobs are still running after, lets say 4seconds* after receiving the stop request.

All my jobs have cancellation tokens, so afaik sending a stop request should allow the app to stop quite quickly.
In production it doesn't sometime, so maybe there are places/jobs where the token is forgotten or not checked withing a large loop etc.

I'd like to find out which jobs take long to exit and investigate them.
Could somebody provide me some guidance how to log of diagnose slow to exit jobs?

I though about hooking into `IHostApplicationLifetime.ApplicationStopping` (`WindowsServiceLifetime` triggers when it receives a stop request) and log each running job. I'd like to do this by just directly asking this to hangfire, without any db calls but i'm not sure how to do this.

* I'm also trying to adhere to the [5s shutdown limit the SCM has.](https://learn.microsoft.com/en-us/windows/win32/services/service-control-handler-function)

Contributor guide

Open the contributing guide

Research direction

No repository file or test is named. Start by examining IHostApplicationLifetime.ApplicationStopping, WindowsServiceLifetime, and Hangfire's running-job and cancellation behavior; determine whether the existing APIs expose jobs still running at shutdown. Done means a documented or implemented way to identify jobs active at stop and still active after the four-second interval.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.