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:

    () => Console.WriteLine("Recurring!"),

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.

	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.