Note. The following information may be outdated after 1.0 release. Please see the official documentation first.
This release contains a bunch of new features, that complete the background job workflow (Deleted state), make it without additional latencies (MSMQ support) and inform you about failures in-time:
- Added - MSMQ queues support for SQL Server job storage.
- Added - “Deleted” state for jobs, when we don’t want to process them anymore.
- Added - “Requeue” and “Delete” buttons on a job page in HF Monitor.
- Added - Logging job failures: warning - there is a retry, error - no attempts left.
Changed - Set
InvisibleTimeoutback from 5 to 30 minutes.
RetryAttributeis deprecated. Use
There is a new state –
DeletedState, and some methods to perform the deletion –
IBackgroundJobClient.Delete. When you are using these methods, a job is not actually deleted, there is only state transition. Jobs in the deleted state expire after some delay (as succeeded jobs).
The operation does not provides guarantee that the job will not be performed. If you deleting a job that is performing right now, it will be performed anyway, despite calls to delete methods.
If you want to delete job despite its current state, do the following:
If you want to be able to handle only deletion from exact state, for example, only from FailedState (because it may change after you click and before it will be deleted), you can specify it:
Job details page now contains Requeue and Delete buttons (other pages also have these buttons). So, you can requeue and delete your jobs at any time:
MSMQ support for SQL Server storage
SQL Server job storage implementation does not require you to learn and install additional technologies, such as Redis, for projects to use Hangfire. However, it uses polling to get new jobs that increases latency between the creation and invocation process (see also this feature request).
The MSMQ implementation, that was introduced in Hangfire 0.8.1, replaces only the way Hangfire enqueues and dequeues jobs. It uses transactional queues to delete jobs only upon successful completion, that allows to process jobs reliably inside ASP.NET applications. MSMQ queues contain only job identifiers, other information is still persisted in the SQL Server database.
Advantages of using MSMQ
- No additional latency. It uses blocking calls to fetch jobs – they will be processed as soon as possible.
- Immediate re-queueing of terminated jobs. If the processing was terminated in the middle, it will be started again immediately after application restart. SQL Server implementation uses InvisibleTimeout to distinguish between long-running and aborted jobs.
MSMQ support for SQL Server job storage implementation, like other Hangfire extensions, is a NuGet package. So, you can install it using NuGet Package Manager Console window:
PM> Install-Package Hangfire.SqlServer.MSMQ
To use MSMQ queues, you should do the following steps:
- Create them manually on each host. Don’t forget to grant appropriate permissions.
- Register all MSMQ queues in current
To see the full list of supported paths and queues, check the MSDN article.
- Only transactional MSMQ queues supported for reability reasons inside ASP.NET.
- You can not use both SQL Server Job Queue and MSMQ Job Queue implementations in the same server (see below). This limitation relates to Hangfire.Server only. You can still enqueue jobs to whatever queues and watch them both in Hangfire.Monitor.
The following case will not work: the
critical queue uses MSMQ, and the
default queue uses SQL Server to store job queue. In this case job fetcher can not make the right decision.
Transition to MSMQ queues
If you have a fresh installation, just use the
UseMsmqQueues method. Otherwise, your system may contain unprocessed jobs in SQL Server. Since one Hangfire.Server instance can not process job from different queues, you should deploy two instances of Hangfire.Server, one listens only MSMQ queues, another – only SQL Server queues. When the latter finish its work (you can see this from Hangfire.Monitor – your SQL Server queues will be removed), you can remove it safely.
If you are using default queue only, do this:
If you use multiple queues, do this: