Best practices¶

Background job processing can differ a lot from a regular method invocation. This guide will help you keep background processing running smoothly and efficiently. The information given here is based off of this blog post.

Make job arguments small and simple¶

Method invocation (i.e. a job) is serialized during the background job creation process. Arguments are converted into JSON strings using the TypeConverter class. If you have complex entities and/or large objects; including arrays, it is better to place them into a database, and then pass only their identities to the background job.

Instead of doing this:

  1. public void Method(Entity entity) { }

Consider doing this:

  1. public void Method(int entityId) { }

Make your background methods reentrant¶

Reentrancy) means that a method can be interrupted in the middle of its execution and then safely called again. The interruption can be caused by many different things (i.e. exceptions, server shut-down), and Hangfire will attempt to retry processing many times.

You can have many problems, if you don’t prepare your jobs to be reentrant. For example, if you are using an email sending background job and experience an error with your SMTP service, you can end with multiple emails sent to the addressee.

Instead of doing this:

  1. public void Method()
  2. {
  3. _emailService.Send("person@example.com", "Hello!");
  4. }

Consider doing this:

  1. public void Method(int deliveryId)
  2. {
  3. if (_emailService.IsNotDelivered(deliveryId))
  4. {
  5. _emailService.Send("person@example.com", "Hello!");
  6. _emailService.SetDelivered(deliveryId);
  7. }
  8. }

To be continued…

原文:

http://docs.hangfire.io/en/latest/best-practices.html