Skip to content

Commit

Permalink
Add payload consideration when using Hangfire
Browse files Browse the repository at this point in the history
  • Loading branch information
filipetoscano committed Jan 24, 2025
1 parent fa67e51 commit f535771
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 5 deletions.
29 changes: 29 additions & 0 deletions examples/AsyncHangfire/Jobs/EmailSendJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,40 @@ public async Task BackgroundSendAsync( EmailMessage message, CancellationToken c
_logger.LogDebug( "Email to {To}: {Subject}", message.To, message.Subject );


/*
*
*/
if ( message.Attachments?.Count > 0 )
{
foreach ( var att in message.Attachments )
{
if ( att.Path?.StartsWith( "load:" ) == false )
continue;

_logger.LogDebug( "Attachment: Load {Filename} based on {LoadPath}", att.Filename, att.Path );

att.Content = await ContentLoadAsync( att.Path!, cancellationToken );
att.Path = null;
}
}


/*
* Send email
*/
var resp = await _resend.EmailSendAsync( message, cancellationToken );

_logger.LogInformation( "Sent with Id {EmailId}", resp.Content );
}


/// <summary />
private async Task<byte[]> ContentLoadAsync( string attachmentPath, CancellationToken cancellationToken )
{
await Task.Delay( 1_000, cancellationToken );

// TODO: Load based on attachmentPath

throw new NotImplementedException();
}
}
23 changes: 18 additions & 5 deletions examples/AsyncHangfire/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,27 @@ Explained
|------|-------------------------------------------------------------
| 1 | When hitting `/test`, the `Get` method of `Controllers/TestController.cs` runs.
| 2 | `BackgroundJob.Enqueue` will create an instance of the email send job.
| 3 | When viewing `/jobs`, one job will be enqueued for execution.
| 4 | Refresh `/jobs`, and the job will change to scheduled.
| 5 | Refresh `/jobs`, and the job will change to processing.

| 3 | Hangfire server will pick up from queue and invoke `EmailSendJob.BackgroundSendAsync` method.
| 4 | If an exception is thrown, Hangfire server will retry the job.
| 5 | Otherwise, job will be marked as Succeeded.


Considerations
---------------------------------------------------------------------

As per the [best practices](https://docs.hangfire.io/en/latest/best-practices.html) documentation,
it is advised to keep the
it is advised to keep the payload size small.

> 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.
As such, it is desireable to avoid inline attachments, since the
entire `byte[]` would be stored in the Hangfire job database.

One alternative of setting the value of `Content` property, would be to
re-use the `Path` property with special value such that the `EmailSendJob`
can then load the content prior to sending email. See `Jobs/EmailSendJob.cs`
for a skeleton of such code.

0 comments on commit f535771

Please sign in to comment.