Hi, I am basically coming back with the pausing question that has already been asked, but still trying to get a view to my problem - how to best implement it.
I want to use hangfire to push out events to external system, most of the time it happens instantaniously, but I want to use hangfire to handle situations where the extarnal system is unable to receive events (ie down for maintanance).
To clearly state what I need - I want to have some way to determine if it is possible to send out requests. If it is possible - send out immediatly. If not possible, queue all requests and start sending them out as soon as it becomes possible again.
So what I have came up with so far is that I have “another scheduled task” that checks if it is possible to send events and it sets a boolean:
private static bool _canSend = true;
public static void CheckStatus()
{
try
{
var request = new RestRequest(@"rest/path", Method.GET);
// Execute will throw an exception if can not send
new RestClient().Execute(request);
_canSend = true;
}
catch (Exception)
{
_canSend = false;
}
}
And I call it from HangfireBootstrapper
public class HangfireBootstrapper : IRegisteredObject
//.... skipped
public void Start()
{
//.... skipped
BackgroundJob.Schedule(() => Helper.CheckStatus(), new TimeSpan(0, 0, 30));
}
Now the method that has been used by hangfire is :
[AutomaticRetry(Attempts = 100)]
public static void SendRequest(StringBuilder request)
{
var restRequest = new RestRequest(RequestUri, Method.POST) { RequestFormat = DataFormat.Xml };
if (_canSend)
{
new RestClient().Execute(restRequest);
}
// I need to throw here, otherwise the request will be marked as "successfully processeced"
else
{
throw new Exception("Can't send currently");
}
}
the above implementation to some degree achieves what I want, but there are some shortcomings.
Currently I am seeing (and by the docs), the delays between retrys going bigger and bigger. That makes sense, but is it possible to programmatically requeue all failed requests? In english - if it becomes possible to send stuff again - start processing the queue asap.
What is the best way to implement this using hangfire?