using System; using System.Net; using System.Net.Http; using System.Threading; using System.Threading.Tasks; using Umbraco.Core; using Umbraco.Core.Configuration.UmbracoSettings; using Umbraco.Core.Logging; namespace Umbraco.Web.Scheduling { internal class KeepAlive : RecurringTaskBase { private readonly ApplicationContext _appContext; public KeepAlive(IBackgroundTaskRunner runner, int delayMilliseconds, int periodMilliseconds, ApplicationContext appContext) : base(runner, delayMilliseconds, periodMilliseconds) { _appContext = appContext; } public override bool PerformRun() { throw new NotImplementedException(); } public override async Task PerformRunAsync(CancellationToken token) { if (_appContext == null) return true; // repeat... // ensure we do not run if not main domain, but do NOT lock it if (_appContext.MainDom.IsMainDom == false) { LogHelper.Debug("Does not run if not MainDom."); return false; // do NOT repeat, going down } using (DisposableTimer.DebugDuration(() => "Keep alive executing", () => "Keep alive complete")) { string umbracoAppUrl = null; try { umbracoAppUrl = _appContext.UmbracoApplicationUrl; if (umbracoAppUrl.IsNullOrWhiteSpace()) { LogHelper.Warn("No url for service (yet), skip."); return true; // repeat } var url = umbracoAppUrl + "/ping.aspx"; using (var wc = new HttpClient()) { var request = new HttpRequestMessage(HttpMethod.Get, url); var result = await wc.SendAsync(request, token); } } catch (Exception e) { LogHelper.Error(string.Format("Failed (at \"{0}\").", umbracoAppUrl), e); } } return true; // repeat } public override bool IsAsync { get { return true; } } public override bool RunsOnShutdown { get { return false; } } } }