Skip to content

Commit b197f15

Browse files
committed
Improved TaskScheduler polling
1 parent 7f72a74 commit b197f15

2 files changed

Lines changed: 54 additions & 42 deletions

File tree

src/Libraries/SmartStore.Services/Tasks/InitializeSchedulerFilter.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ public void OnAuthorization(AuthorizationContext filterContext)
4343

4444
try
4545
{
46-
logger = EngineContext.Current.Resolve<ILogger>();
47-
taskScheduler = EngineContext.Current.Resolve<ITaskScheduler>();
48-
4946
var taskService = EngineContext.Current.Resolve<IScheduleTaskService>();
5047
var storeService = EngineContext.Current.Resolve<IStoreService>();
5148
var eventPublisher = EngineContext.Current.Resolve<IEventPublisher>();

src/Libraries/SmartStore.Services/Tasks/TaskScheduler.cs

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public class DefaultTaskScheduler : DisposableObject, ITaskScheduler, IRegistere
2121
private bool _intervalFixed;
2222
private int _sweepInterval;
2323
private string _baseUrl;
24+
private bool _mustVerifyBaseUrl;
2425
private System.Timers.Timer _timer;
2526
private bool _shuttingDown;
2627
private int _errCount;
@@ -48,7 +49,8 @@ public string BaseUrl
4849
{
4950
CheckUrl(value);
5051
_baseUrl = value.TrimEnd('/', '\\');
51-
}
52+
_mustVerifyBaseUrl = true;
53+
}
5254
}
5355

5456
public void Start()
@@ -173,67 +175,80 @@ protected internal virtual void CallEndpoint(string url)
173175
req.Method = "POST";
174176
req.ContentType = "text/plain";
175177
req.ContentLength = 0;
178+
req.ServicePoint.Expect100Continue = false;
179+
req.Timeout = 10000; // 10 sec.
176180

177181
string authToken = CreateAuthToken();
178182
req.Headers.Add("X-AUTH-TOKEN", authToken);
179183

180-
req.GetResponseAsync().ContinueWith(t =>
181-
{
182-
if (t.IsFaulted)
184+
HttpWebResponse response = null;
185+
186+
try
187+
{
188+
response = (HttpWebResponse)req.GetResponse();
189+
_errCount = 0;
190+
_mustVerifyBaseUrl = false;
191+
192+
//using (var logger = new TraceLogger())
193+
//{
194+
// logger.Warning("TaskScheduler Sweep called successfully: {0}".FormatCurrent(response.GetResponseStream().AsString()));
195+
//}
196+
}
197+
catch (Exception ex)
198+
{
199+
HandleException(ex, url);
200+
_errCount++;
201+
if (_errCount >= 10)
183202
{
184-
HandleException(t.Exception, url);
185-
_errCount++;
186-
if (_errCount >= 10)
203+
// 10 failed attempts in succession. Stop the timer!
204+
this.Stop();
205+
using (var logger = new TraceLogger())
187206
{
188-
// 10 failed attempts in succession. Stop the timer!
189-
this.Stop();
190-
using (var logger = new TraceLogger())
191-
{
192-
logger.Information("Stopping TaskScheduler sweep timer. Too many failed requests in succession.");
193-
}
207+
logger.Information("Stopping TaskScheduler sweep timer. Too many failed requests in succession.");
194208
}
195209
}
196-
else
197-
{
198-
_errCount = 0;
199-
var response = t.Result;
200-
201-
//using (var logger = new TraceLogger())
202-
//{
203-
// logger.Debug("TaskScheduler Sweep called successfully: {0}".FormatCurrent(response.GetResponseStream().AsString()));
204-
//}
205-
210+
}
211+
finally
212+
{
213+
if (response != null)
206214
response.Dispose();
207-
}
208-
});
209-
}
215+
}
216+
}
210217

211-
private void HandleException(AggregateException exception, string url)
218+
private void HandleException(Exception exception, string url)
212219
{
213220
using (var logger = new TraceLogger())
214221
{
215222
string msg = "Error while calling TaskScheduler endpoint '{0}'.".FormatInvariant(url);
216-
var wex = exception.InnerExceptions.OfType<WebException>().FirstOrDefault();
217223

218-
if (wex == null)
224+
var wex = exception as WebException;
225+
226+
if (_mustVerifyBaseUrl)
219227
{
220-
logger.Error(msg, exception);
228+
if (wex == null || wex.Response == null)
229+
{
230+
// a network related error occurred. Fallback to localhost!
231+
_baseUrl = "http://localhost:52089/taskscheduler";
232+
_mustVerifyBaseUrl = false;
233+
234+
logger.Information("A netwotk related error occurred while calling TaskScheduler endpoint'{0}'. Will try with 'localhost' on next sweep.".FormatInvariant(url));
235+
}
236+
}
237+
238+
if (wex != null && wex.Response != null)
239+
{
240+
var response = wex.Response as HttpWebResponse;
241+
msg += " HTTP {0}, {1}".FormatCurrent((int)response.StatusCode, response.StatusDescription);
242+
logger.Error(msg);
221243
}
222244
else
223245
{
224-
using (var response = wex.Response as HttpWebResponse)
225-
{
226-
if (response != null)
227-
{
228-
msg += " HTTP {0}, {1}".FormatCurrent((int)response.StatusCode, response.StatusDescription);
229-
}
230-
logger.Error(msg);
231-
}
246+
logger.Error(msg, exception);
232247
}
233248
}
234249
}
235250

236-
private void CheckUrl(string url)
251+
private void CheckUrl(string url)
237252
{
238253
if (!url.IsWebUrl())
239254
{

0 commit comments

Comments
 (0)