Hi I have created a background job using hangfire
in MVC 5. I send a notification that the background job has started but I don't know how to send a notification when the background job is Finished because my ActionResults method has returned.
The Controller
public ActionResult RunMedia(int ID)
{
BackgroundJob.Enqueue<CrawlerSearchRequestModel>(x => x.StoreMedia(ID,"NaturalPerson"));
obj
{
success: true
};
return JSON(obj, JsonRequestBehavior.AllowGet)
}
The Ajax Call and Javascript notification
$(document).ready(function () {
$(document).on("click", "#submitButton", function () {
debugger;
var userId = $(this).attr('userid')
$.ajax({
type: 'POST',
url: "../LemonCreditAuth/NaturalPerson/RunMedia",
data: {
ID: userId
},
success: function (data) {
swal("Adverse Media!", "Adverse Media has started. After a 20 seconds please check the media case for updates", "success");
},
error: function (data, error) {
swal("Adverse Media!", "Error ocurred while trying to run adverse media", "warning");
}
});
});
Thanks
As others said you can use signalR or other protocols. To achive this, use IApplyStateFilter filter and implement OnStateApplied method of the interface.
public class YourAttribute : IApplyStateFilter{
public void OnStateApplied(ApplyStateContext context, IWriteOnlyTransaction transaction)
{
if (context.NewState.IsFinal && context.NewState.Name == "Succeeded")
//Send your message to client via signalR
}}