Maintenance Task for Hangfire

Virto Commerce uses Hangfire for Background jobs and Async workers.

Every C# code can be executed in Fire-and-forget jobs only once and almost immediately after creation:

var jobId = BackgroundJob.Enqueue(
    () => Console.WriteLine("Fire-and-forget!"));

You can create recurring jobs fire many times on the specified CRON schedule:

RecurringJob.AddOrUpdate(
    () => Console.WriteLine("Recurring!"),
    Cron.Daily);

But need to understand when you run the code with hangfire, the current context will be serialized, stored in persistent storage and executed on any active server.

Sometimes serialization can be a problem because a lot of data can be stored, which means we will need more CPU and more MS SQL resources.

We prepared SQL Scripts which allows analyzing the count of jobs and the size of serialized data.

SELECT 
	SUM(DATALENGTH(Arguments)) AS TotalSizeInBytes, 
	COUNT(*) AS Count, 
	JSON_VALUE(InvocationData,'$.t') AS JobName
FROM [HangFire].[Job]
GROUP BY JSON_VALUE(InvocationData,'$.t')
ORDER BY TotalSizeInBytes DESC, Count DESC

With the result, you can find unexpected usage of Background Job and improve it.

Hello, @OlegoO What’s the best way to clean up Hangfire tables periodically? They grow in size very fast. Do you guys have any script for the same? I found the below thread from Hangfire.io. Do you recommend truncating all the Hangfire tables?

@rahul By default, Hangfire should clean history for successful jobs older than 24 hours.

Could you provide more details about the job?
Do you see successful jobs older than 24 hours?
What is the count of operations?
Do you see any failed jobs?