Is there any way to Update a delayed Job before it gets executed?

I have a delayed job which is scheduled to send email. Is there any way I can update the contents of the email after the job is scheduled and before it gets executed?

One way would be to persist the contents of the email in a database and pass the row Id as an argument to the delayed job. Then you can manipulate the content as much as you like.

@nirinchev This can be one way but I was thinking if there is a way in which the Job Data is fully contained into the Job definition. As the the Background Method gets serialized and persisted into the Job Store is there any way that can be updated before a delayed job gets executed?

I am not aware of such functionality within Hangfire. Naturally, you can write a SQL query and modify the arguments, but I’d advise against that. In general, @odinserj recommends small and simple job arguments. When designing your application, it’s good idea to make it job storage agnostic. Serializing huge arguments to SQL server storage is cheap, but if you decide to move to Redis for performance reasons, it can get pretty expensive, very fast.


@nirinchev As suggested I am now persisting the contents of the email in database and pass the row id as an argument to the delayed job.
Now, is there any option by which I can delete this row as soon as the related Job is flagged Completed ?
For the successfully executed jobs I don’t want to keep the job related data (contents of the email in my case) in the database once the email is sent.

Why don’t you do it in the background job itself? Once your email sending logic is completed, simply delete the row from the database.

Rahul, hi! :smiley:
How you are scheduling a job? if you’re using a BackgroundJob.Schedule(), this will return a string, this string is the job id created. With this id, you can update or delete the job.